3

Can't figure out how-to generate this menu using a while-loop.

This is an example of my code:

<ul id="nav">
<li><a href="#">Hoofdmenu 1</a>
<ul class="sub">
        <li><a href="#">Submenu 1.1</a></li>
        <li><a href="#">Submenu 1.2</a></li>
        <li><a href="#">Submenu 1.3</a></li>
        <li><a href="#">Submenu 1.4</a></li>
    </ul>
</li>

<li><a href="#">Hoofdmenu 2</a>
    <ul class="sub">
        <li><a href="#">Submenu 2.1</a></li>
        <li><a href="#">Submenu 2.2</a></li>
        <li><a href="#">Submenu 2.3</a></li>
        <li><a href="#">Submenu 2.4</a></li>
    </ul>
</li>
</ul>

My dbtable looks like:

paginas:
    id
    title
    content
    type

When type == id from the parent it should be the submenu. In my example this works, now I've got to make it dynamic. Brains ain't working atm.

Thanks for your help!

Used code to get data from db:

<ul id="nav">
<?php
include_once("ond/inc/php/connect.php");
$query = "SELECT * FROM paginas WHERE type = '0'";
$result = mysql_query($query);
while($row = mysql_fetch_object($result)){

echo '<li><a href="?ond='.$row->titel.'">'.$row->titel.'</a>';}
echo '<ul class="sub">';

$query2 = "SELECT * FROM paginas WHERE type = '".$row->id."'";
$result2 = mysql_query($query2);    
while($row2 = mysql_fetch_object($result2))
{
    echo '<li><a href="?ond='.$row2->titel.'">'.$row2->titel.'</a></li>';
}
echo '</ul>'; 
echo '</li>';

?>
</ul>
  • 2
    Brain not working? Have you been consuming your own produce again! :) Anyway, welcome to SO. This needs more info: What database library are you using? What code do you have to connect to the database and fetch the records? Post that code, and people can give you pointers on how to do it. If you have nothing yet, you should take a look at a decent PHP database programming tutorial first, and ask specific questions if you hit a snag. – Pekka Apr 07 '11 at 11:25
  • 3
    This looks pretty clear too me imo ;p anyway I'm using an mysql database. My php is decent bt my brain won't work today (alcohol, marihuana) –  Apr 07 '11 at 11:28
  • 1
    @Ganjafarmer well, as said, if you have nothing yet, you should get started with a basic database tutorial, e.g. [this one](http://www.phpro.org/tutorials/Introduction-to-PHP-PDO.html). Without a specific question, I can't see more here than "write my code for me" which is not SO's mission and purpose. – Pekka Apr 07 '11 at 11:30
  • 1
    Pekka you're right I'll post my php in a sec. –  Apr 07 '11 at 11:30
  • "Brains ain't working atm". I can't speak for everyone, but I for one can relate. You can't expect brains to work optimal all the time. :) Hopefully that will make you feel a bit less disappointed about yourself. In the meanwhile: can you show some PHP code of what you have so far? That makes it easier to help out. – Decent Dabbler Apr 07 '11 at 11:31
  • 1
    And alcohol and marihuana will probably not help your case either. ;-) – Decent Dabbler Apr 07 '11 at 11:33
  • 4 years later this actually got me another famous question badge. Can't believe I even asked this question x) ahahahaha –  Sep 25 '15 at 08:16

4 Answers4

1

I would do something like this:

First, grab your data out as an array and loop through it for each entry. Then run something like this:

$menuArray = array();

if (empty($type)) // If the entry has no "type", then it's a parent
{
    $menuArray[$type]['title'] = $title;
}
else // else, it's a child, so append it to the parent
{
    $menuArray[$type]['subitems'][] = $title;
}

Then, having $menuArray, loop through it to create the menu:

?><ul id="nav"><?php
foreach ($menuArray as $item)
{
    ?><li><a href="#">$item['title']</a><?php
    ?><ul class="sub"><?php

    foreach ($item['subitems'] as $subItem)
    {
        <li><a href="#">$subItem</a></li>
    }

    ?></ul><?php
    ?></li><?php
}
?></ul><?php
Gavin Anderegg
  • 6,281
  • 2
  • 25
  • 35
1

Next lines did the solution:

<ul id="nav">
<?php
include_once("ond/inc/php/connect.php");
$query = "SELECT * FROM paginas WHERE type = '0'";
$result = mysql_query($query);
while($row = mysql_fetch_object($result)){

echo '<li><a href="?ond='.$row->titel.'">'.$row->titel.'</a>';

$query2 = "SELECT * FROM paginas WHERE type = '".$row->id."'";
$result2 = mysql_query($query2);    
echo '<ul class="sub">';
while($row2 = mysql_fetch_object($result2))
{   
    echo '<li><a href="?ond='.$row2->titel.'">'.$row2->titel.'</a></li>';


}
    echo '</ul>';
echo '</li>';}



?>
</ul>
1
function load()
{
    global $conn;


    $query = "SELECT *  FROM sub_category WHERE main_category_id='1'";
    $result = mysqli_query($conn,$query);
    while($row = mysqli_fetch_assoc($result)){
        $cat_id=$row['sub_category_id'];

    echo '<li><a href="?id='.$row['sub_category_id'].'">'.$row['sub_category_name'].'</a>';

    $query2 = "SELECT *  FROM categories WHERE sub_category_id='$cat_id'";
    $result2 = mysqli_query($conn,$query2);    
    echo '<ul class="sub">';
    while($row2 = mysqli_fetch_assoc($result2))
    {   
        echo '<li><a href="?id='.$row2['category_id'].'">'.$row2['category_name'].'</a></li>';


    }
        echo '</ul>';
    echo '</li>';}


}
0

in this example parent_id column used and ul-li list build with references- it makes only 1 sql query and use recoursion for rendering output, this code can be simply changed for your needs

        <?php
 /**
  * Module for displaying data from items table
  */
class App_Modules_Tree  extends   App_AbstractModule {

    /**
        * array for storing data
        *
        * @var array
        */
    private $tree = array();
       /**
        * html - output of current module
        *
        * @var string
        */
        private $output = ''; 

      /**
       * Retreives data from table items.
       *
       * @return void
       */
        private function _getData()
    {
        $pdo = App_Registry::get('pdo');
        $levels = array();
        foreach ($pdo->query('SELECT * FROM items ORDER BY parent_id ASC',PDO::FETCH_OBJ) as $k=>$v){
                   // references
             $current =  &$levels[ $v->id ] ;
                 $current['parent_id'] = $v->parent_id;
                 $current['name'] = $v->name;
                 if (0 == $v->parent_id){
                 $this->tree[ $v->id ] = &$current;
                 } else {
             $levels[$v->parent_id ]['children'][$v->id] = &$current;
                 }
        }
    }   

    /*
    *App_AbstractModule::preRender overriding
        * @return void
    */
    protected  function preRender()
    {
        $this->_getData();
        }
      /**
       * recursively build html output.
       *
       * @return string
       */
    private function _render($arr)
    {
        $this->output.= '<ul>';
        foreach ($arr as $k=>$v)
        {
            $this->output.= '<li>'.$v['name'].'</li>';
            if( !empty($v['children'])){
                $this->_render($v['children']);
            }
        }
        $this->output.= '</ul>';
        return $this->output;
    }
    /*
    *App_AbstractModule::render overriding
        * @return string
    */
    protected  function render()
    {
            return $this->_render($this->tree);
    }

}
Fivell
  • 11,829
  • 3
  • 61
  • 99