PHP菜单:用于父项的循环

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.

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 ?