Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*************************
- // GENERATED NESTED LIST
- // created by : Cahya DSN
- // created date : 2012-09-14
- **************************/
- function GenerateDataArray($arr, $parent = 0)
- {
- $pages = Array();
- foreach($arr as $page)
- {
- if($page['parent'] == $parent)
- {
- $page['sub'] = isset($page['sub']) ? $page['sub'] : GenerateDataArray($arr, $page['id_category']);
- $pages[] = $page;
- }
- }
- return $pages;
- }
- // loop the multidimensional array recursively to generate the HTML
- function GenerateDataHTML($nav)
- {
- $html = '';
- foreach($nav as $page)
- {
- $html .= '<ul><li>';
- $html .= '<a href="' . $page['link'] . '">' . $page['title'] . '</a>';
- $html .= GenerateDataHTML($page['sub']);
- $html .= '</li></ul>';
- }
- return $html;
- }
- $data=array(
- array('id_category'=>1,'title'=>'Kategori 1','parent'=>0,'link'=>'#','sort'=>1),
- array('id_category'=>2,'title'=>'Kategori 1.1','parent'=>1,'link'=>'#','sort'=>1),
- array('id_category'=>3,'title'=>'Kategori 1.2','parent'=>1,'link'=>'#','sort'=>2),
- array('id_category'=>4,'title'=>'Kategori 1.2.1','parent'=>3,'link'=>'#','sort'=>1),
- array('id_category'=>5,'title'=>'Kategori 2','parent'=>0,'link'=>'#','sort'=>2),
- array('id_category'=>6,'title'=>'Kategori 3','parent'=>0,'link'=>'#','sort'=>3),
- array('id_category'=>7,'title'=>'Kategori 3.1','parent'=>6,'link'=>'#','sort'=>1),
- array('id_category'=>8,'title'=>'Kategori 3.2','parent'=>6,'link'=>'#','sort'=>2)
- );
- $dataarray = GenerateDataArray($data);
- echo GenerateDataHTML($dataarray);
Add Comment
Please, Sign In to add comment