doubei2231 2015-03-19 16:39
浏览 30
已采纳

PHP菜单:用于父项的循环

I'm trying to work my way with PHP and create a PHP menu that uses items stored in database and a PHP code to construct the menu items in the right order. The values on the database can be changed so i want to keep the parent items exported first then the nested items added below.

I want to use a loop (do-while? i guess) to get the data from the database so i can get the menu arranged correctly with the nested items and outputted with a class that I want to use.

My database table stores the title and url for each item and the nested items contain the id of the parent items, the parent items has id 0 set as parent and it looks like this:

--------------------
| id     | int(11) |  
--------------------
| title  | varchar |  
--------------------
| url    | varchar |  
--------------------
| parent | int(11) |  
--------------------

Here is my PHP code:

try {
    $stm = $db->prepare('SELECT * FROM menu');
    $stm->execute();
    $result = $stmt->fetchAll();

     // requested loop / function

    }

The output should look like this:

$menu = menu::create()
    ->add('Homepage', '/', menu::create()
    ->add('Item1', '/item1/', menu::create()
        ->add('Subitem1', '/subitem1/')
        ->add('Subitem1', '/subitem1/')
    ->add('Item2', '/item2/', menu::create()
        ->add('Subitem3', '/subitem3/')
        ->add('Subitem4', '/subitem4/')

Please suggest how to get this output with the database items... Thank you.

  • 写回答

1条回答 默认 最新

  • douhao7677 2015-03-19 17:48
    关注

    There must be some adjustments to do but I hope this will give you a lead

    function    sort_parent($parent)
    {
        $j=0;
        $flag = true;
        $temp=0;
        while ( $flag )
        {
            $flag = false;
            for( $j=0;  $j < count($parent)-1; $j++)
            {
                if ($parent[$j]->id > $parent[$j+1]->id)
                {
                    $temp = $parent[$j]->id;
                    $parent[$j] = $parent[$j+1]->id;
                    $parent[$j+1]->id = $temp;
                    $flag = true;
                }
            }
        }
        return ($parent);
    }
    
    function    sort_children($children)
    {
        $j=0;
        $flag = true;
        $temp=0;
        while ( $flag )
        {
            $flag = false;
            for( $j=0;  $j < count($children)-1; $j++)
            {
                if ($children[$j]->parent > $children[$j+1]->parent)
                {
                    $temp = $children[$j]->parent;
                    $children[$j] = $children[$j+1]->parent;
                    $children[$j+1]->parent = $temp;
                    $flag = true;
                }
            }
        }
        return ($children);
    }
    
    try {
        //First we fetch the parents
        $stm = $db->prepare('SELECT * FROM menu WHERE parent = 0');
        $stm->execute();
        $parent = $stmt->fetchAll();
    
        // Then the childrens. Note: In some versions of SQL this operator may be written as !=
        $stm = $db->prepare('SELECT * FROM menu WHERE parent <> 0');
        $stm->execute();
        $children = $stmt->fetchAll();
    }
    
        // We sort parent and children with a basic buble sort
        $parent = sort_parent($parent);
        $children = sort_object($children);
    
        // To finish as Parent and Children are in the same order we can do that
        foreach ($parent as $parent_menu_item) {
            $menu = menu::create() -> add($parent_menu_item->title, $parent_menu_item->url);
            for ($j=0; $children[$i]->parent == $parent_menu_item->id; j++)
            {
                $menu = menu::create() -> add($children[$j]->title, $children[$j]->url);
            }
        }
    

    Does it help ?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探