PHP Classes
elePHPant
Icontem

File: test.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Arthur Borisow  >  Walker  >  test.php  >  Download  
File: test.php
Role: Example script
Content type: text/plain
Description: Shows how to use the Walker class
Class: Walker
Convert an array into hierarchic data
Author: By
Last change: Shows the example of building the menu with the help of ul's and li's
Date: 4 years ago
Size: 2,018 bytes
 

Contents

Class file image Download
<?php
   
require 'walker.class.php';
   
$cats = array(
       
0 => array(
           
'id' => '1',
           
'name' => 'Cat 1',
           
'parentId' => 6
       
),
       
1 => array(
           
'id' => '2',
           
'name' => 'Cat 2',
           
'parentId' => 1
       
),
       
2 => array(
           
'id' => '3',
           
'name' => 'Cat 3',
           
'parentId' => 1
       
),
       
3 => array(
           
'id' => '4',
           
'name' => 'Cat 4',
           
'parentId' => 3
       
),
       
4 => array(
           
'id' => '5',
           
'name' => 'Cat 5',
           
'parentId' => 10
       
),
       
5 => array(
           
'id' => '6',
           
'name' => 'Cat 6',
           
'parentId' => 0
       
),
       
6 => array(
           
'id' => '7',
           
'name' => 'Cat 7',
           
'parentId' => 5
       
),
       
7 => array(
           
'id' => '8',
           
'name' => 'Cat 8',
           
'parentId' => 6
       
),
       
8 => array (
           
'id' => 9,
           
'name' => 'Cat 9',
           
'parentId' => 5,
        ),
       
9 => array (
           
'id' => 10,
           
'name' => 'Cat 10',
           
'parentId' => 0,
        ),
       
10 => array (
           
'id' => 11,
           
'name' => 'Cat 11',
           
'parentId' => 9,
        ),
    );
   
   
  
   
//as well as the set of objects
   
   
foreach ($cats as &$cat) {
       
$cat = (object)$cat;
    }
   
   
    class
Walker_Test extends Walker {
        protected function
_wrapAllElements($content) {
            return
'<ul>' . $content . '</ul>';
        }
        protected function
_wrapElement($element, $depth) {
            return
'<li>' . $element->name . '</li>';
        }
        protected function
_wrapChildren($content) {
            return
'<li><ul>' . $content . '</ul></li>';
        }
    }

   
$w = new Walker_Test($cats, 'id', 'parentId');
    echo
$w;

?>