Created
June 19, 2013 11:30
-
-
Save hungneox/5813595 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$list = array( | |
array('id'=>1,'name'=>'Fashion','parent_id'=>null), | |
array('id'=>2,'name'=>'Men','parent_id'=>1), | |
array('id'=>3,'name'=>'Women','parent_id'=>1), | |
array('id'=>4,'name'=>'Jacket Men','parent_id'=>2), | |
array('id'=>5,'name'=>'Jacket Women','parent_id'=>3), | |
array('id'=>6,'name'=>'Bag','parent_id'=>null), | |
array('id'=>7,'name'=>'Bag Men','parent_id'=>6), | |
array('id'=>8,'name'=>'Winter jacket Men','parent_id'=>4), | |
); | |
echo <<<HTML | |
<ul> | |
<li> | |
Fashion | |
<ul> | |
<li>Men | |
<ul> | |
<li>Jacket Men</li> | |
</ul> | |
</li> | |
<li>Women | |
<ul> | |
<li>Jacket Women</li> | |
</ul> | |
</li> | |
</ul> | |
</li> | |
<li> | |
Bag | |
</li> | |
</ul> | |
HTML; | |
function create_list($list, $parent_id = null){ | |
$html = '<ul>'; | |
foreach($list as $row){ | |
if($row['parent_id']== $parent_id){ | |
$html .= '<li>'.$row['name'] . create_list($list, $row['id']) .'</li>'; | |
} | |
} | |
$html .= '</ul>'; | |
return $html; | |
} | |
$tree = create_list($list); | |
print '<hr/>' . $tree; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment