简体   繁体   中英

PHP tree level nested menu recursive function

I'm trying to display a set of data in tree level

For example this is my database record

在此处输入图片说明

I wish to display it like

在此处输入图片说明

and so on.

Of course above picture is manually key in by myself

  <?php require 'tree.php'; $tree = array( 'wwq' => array( 'Project4' => array( 'phase1', ), 'Project23' => array( 'phase23', ), 'Test1' => array( 'test1', ), 'Projecttest' => array( 'phasetest', 'testtest', ) ), ); echo treeOut($tree); ?> 

and my function is

 <?php function treeOut($row_Recordset1){ $markup = ''; foreach ($row_Recordset1 as $branch => $twig){ $markup .= '<li>' . ((is_array($twig)) ? $branch . treeOut($twig) : $twig). '</li>'; } return '<ul>' . $markup . '</ul>'; } ?> 

So, my question is how I can make the array to for loop instead of I key in every data.

Thankss if anyone could help me!! If any question can ask me below comment.

If it is about how you can prepare array in required format before passing it to treeOut() function then try adding these line before calling treeOut() :

$tree = array();

$sql = "SELECT * FROM tbl_name ORDER BY companyName ASC, projectName ASC, phaseName ASC";
$result = mysql_query($connection, $sql);

while($data = mysql_fetch_array($result)){
    $tree[ $data['companyName'] ][ $data['projectName'] ][] = $data['phaseName'];
}

echo treeOut($tree);

Hope this is what you're looking for and will help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM