0

Hy,

The title says it al, i want to make a menu with submenu's.

So something like this:

  • parent1
    1. sub2
      1. sub 3
  • parent2

I've got this already working, but the code doesn't look nice.

So how can i improve my code, or does you know it beter, post it then.

This is the code:

<?php

$le_content .= '<div id="main_menu">';


$sQuery_get_all_parents = " SELECT
                            menu.id,
                            menu.`name`,
                            menu.url,
                            menu.parent_id
                            FROM `menu`
                            WHERE
                            menu.actif = 1 AND
                            menu.parent_id = 0
                            ORDER BY
                            menu.volgorde ASC";
$rResultaat_get_all_parents = mysql_query($sQuery_get_all_parents) or die(mysqlError($sQuery_get_all_parents));

$menu_parents = array();
$menu_child_1 = array();
$counter = 0;
$counter_child_1 = 0;
$counter_child_2 = 0;

while($row = mysql_fetch_assoc($rResultaat_get_all_parents)){
    $menu_parents[$counter]['id'] = $row['id'];
    $menu_parents[$counter]['name'] = $row['name'];
    $menu_parents[$counter]['url'] = $row['url'];
    $menu_parents[$counter]['parent_id'] = $row['parent_id'];
    $counter++;
}

$le_content .= '<ul id="main_menu_ul">';
foreach ($menu_parents as $menu_entry => $value) {
    $le_content .= '<li><a href="'.ROOT_HREF.$value['url'].'" >'.$value['name'].'</a>';
    $sQuery_get_first_children = "  SELECT
                                    menu.id,
                                    menu.`name`,
                                    menu.url,
                                    menu.parent_id
                                    FROM `menu`
                                    WHERE
                                    menu.actif = 1 AND
                                    menu.parent_id = ".$value['id']."
                                    ORDER BY
                                    menu.volgorde ASC";
    $rResultaat_get_first_children = mysql_query($sQuery_get_first_children) or die(mysqlError($sQuery_get_first_children));
    $row_count = mysql_num_rows($rResultaat_get_first_children);

    if($row_count != false){
        $le_content .= '<ol>';
        while($row = mysql_fetch_assoc($rResultaat_get_first_children)){
            $menu_child_1[$counter_child_1]['id'] = $row['id'];
            $menu_child_1[$counter_child_1]['name'] = $row['name'];
            $menu_child_1[$counter_child_1]['url'] = $row['url'];
            $menu_child_1[$counter_child_1]['parent_id'] = $row['parent_id'];
            $counter_child_1++;
        }

        foreach ($menu_child_1 as $menu_entry_child_1 => $value_child_1) {
            $le_content .= '<li><a href="'.ROOT_HREF.$value_child_1['url'].'" >'.$value_child_1['name'].'</a>';
            $sQuery_get_second_children = " SELECT
                                            menu.id,
                                            menu.`name`,
                                            menu.url,
                                            menu.parent_id
                                            FROM `menu`
                                            WHERE
                                            menu.actif = 1 AND
                                            menu.parent_id = ".$value_child_1['id']."
                                            ORDER BY
                                            menu.volgorde ASC";
            $rResultaat_get_second_children = mysql_query($sQuery_get_second_children) or die(mysqlError($sQuery_get_second_children));
            $row_count = mysql_num_rows($rResultaat_get_second_children);
            if($row_count != false){
                $le_content .= '<ol>';
                while($row = mysql_fetch_assoc($rResultaat_get_second_children)){
                    $menu_child_2[$counter_child_2]['id'] = $row['id'];
                    $menu_child_2[$counter_child_2]['name'] = $row['name'];
                    $menu_child_2[$counter_child_2]['url'] = $row['url'];
                    $menu_child_2[$counter_child_2]['parent_id'] = $row['parent_id'];
                    $counter_child_2++;
                }
                foreach ($menu_child_2 as $menu_entry_child_2 => $value_child_2) {
                    $le_content .= '<li><a href="'.ROOT_HREF.$value_child_2['url'].'" >'.$value_child_2['name'].'</a></li>';
                }
                $le_content .= '</ol>';
            }

            $le_content .= '</li>';
        }
        $le_content .= '</ol>';
    }
    $le_content .= '</li>';
}

$le_content .= '</ul></div>';

?>

in the end, i echo $le_content.....

1
  • Please consider switching to MySQLi or PDO. The mysql_* functions are old, soon to be deprecated, and don't provide solid counter-measures against SQL injection attacks. Commented Jul 2, 2012 at 14:26

2 Answers 2

4
  1. Query all menu item
  2. Build a hierarchical array of them (create a recursive function to do it) every menuitem should have a property for it's children collection
  3. Create another recursive function to write the output
Sign up to request clarification or add additional context in comments.

Comments

0

Alright, i've searched around, and came with an solution... Making the query better. i've followed this tutorial and that works verry well:

sql and categories

Thanks for all the help ;)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.