Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8648823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:24:16+00:00 2026-06-12T13:24:16+00:00

I have a problem with the generated multi-level menu. I would like each menu

  • 0

I have a problem with the generated multi-level menu. I would like each menu level select another color (li class=menucolor). However, I managed to only the first and 2nd level do. I can’t separate the second level from the third level. Please help.

SQL:

CREATE TABLE IF NOT EXISTS `menu` (
`id` int(11) NOT NULL auto_increment,
`id_parent` int(11) NOT NULL default '0',
`link` varchar(255) NOT NULL,
`order` int(11) NOT NULL default '0',
`title` varchar(255) collate utf8_polish_ci NOT NULL default '',
PRIMARY KEY  (`id`)
);


INSERT INTO `menu` (`id`, `id_parent`, `link`, `order`, `title`) VALUES
(1, 0, index.php, 3, 'Index'),
(3, 1, link1.php, 1, 'Art1'),
(4, 1, link2.php, 2, 'Art2'),
(5, 4, link2.php, 6, 'Other art'),
(6, 4, link2.php, 1, 'Art AAA'),
(7, 4, link2.php, 1, 'Art BBB'),
(8, 4, link2.php, 1, 'Art CCC'),

AND CODE:

<ul id="menu_left">
<?php
$menuArray = array();
$query = mysql_query('  select id, id_parent, link, order, title
                        from menu
                        order by order asc
              ');

while($row = mysql_fetch_array($query))
{
  $menuArray[] = array( 'id'           => $row['id'],
                        'id_parent'    => $row['id_parent'], 
                        'link'         => $row['link'],
                        'order'        => $row['order'], 
                        'title'        => $row['title']
              );
}

function menu($id)
{
  global $menuArray;
  $hasChildren = false;
  $resultArray = array();

  if (empty($menuArray))
  {
    return;
  }

  foreach ($menuArray as $value)
  {
    if ($value['id_parent'] == $id)
    {
      $resultArray[] = $value;

    }
  }

  foreach ($resultArray as $value)
  {
    if ($hasChildren === false)
    {
      $hasChildren = true;
      if ($value['id_parent'] == 0)
      {
      $j++
        ?>
        <!-- ul first -->
        <?php
      }
      else
      {
        ?>
       <!-- <ul> -->
        <?php
      }
    }
    if($j=='1') {
    echo '<li class="menucolor0">';
    }
    elseif($j!='1') {
    echo '<li class="menucolor1">';
    }
    ?>
    <a href=<?php echo $value['link']; ?>"><?php echo $value['title']; ?></a> 
    <?php
    menu($value['id']);
    ?> 
    </li>
    <?php
  }
  if ($hasChildren === true)
  {
  <!-- </ul> -->
  }
}

menu(0); 
?>    
        </ul>  
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T13:24:18+00:00Added an answer on June 12, 2026 at 1:24 pm

    Any direct solution to this would be very slow, I suggest you update your table as such:

    <?php
    
    $db = new PDO('mysql:dbname=databasename', 'username', 'password',
        array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
    
    $db->prepare('ALTER TABLE `menu` ADD COLUMN `level` TINYINT NOT NULL DEFAULT 0;')->execute();
    
    $db->prepare('UPDATE `menu` SET `level` = 1 WHERE `id_parent` = 0;')->execute();
    
    $select = $db->prepare('SELECT `id` FROM `menu` WHERE `level` = :level');
    $update = $db->prepare('UPDATE `menu` SET `level` = :level WHERE `id_parent` = :parent');
    
    for( $i = 1; $i < 100; $i++ ) {
        $select->execute(array('level' => $i));
        while( ($current = $select->fetch(PDO::FETCH_COLUMN)) !== false )
            $update->execute(array('level' => $i + 1, 'parent' => $current));
    }
    

    and here is how you display the menu in the desired order:

    <?php
    // connect to database using PDO
    $db = new PDO('mysql:dbname=databasename', 'username', 'password',
        array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
    
    
    function createsubmenu( $item )
    {
        global $db;
    // prepare a query to get subitems of the current $item (those with parent = $item)
        $subsel = $db->prepare('SELECT id, link, title, level  FROM menu WHERE id_parent = :parent ORDER BY `order`');
    // run the query
        $subsel->execute(array('parent' => $item));
        $text = '';
    // fetch one row at a time, when no more rows are available $i
    // will be false and while ends
        while( ($i = $subsel->fetch(PDO::FETCH_OBJ)) !== false ) {
    // generate code for the current $item
    // will recursively call createsubmenu to add possibly existing subitems
            $text .= '<li class="menucolor' . ($i->level - 1) . '">'
                .'<a href="' . htmlspecialchars($i->link) . '">' . htmlspecialchars($i->title) . '</a>'
                . createsubmenu($i->id) . '</li>';
        }
    // there were no items for the current call
        if( $text == '' )
            return '';
    // items were found, wrap them in an unordered list
        return '<ul>' . $text . '</ul>';
    }
    
    // call the function for toplevel items (the ones having parent 0)
    echo createsubmenu(0);
    

    don’t forget to replace databasename, username and password in both scripts

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a drop-down/multi-level CSS menu on a page. The menu however doesn't appear
I have a graph of multi-level dependecies like this, and I need to detect
I have a problem with the multi-select box. I tried to select multiple values
I have a problem with generating an XML that needs to be generated automatically
I have a problem while pasting my contents (or text) generated by Java code
Problem: I have a multipage TIFF image (generated with tiffutil) that contains the same
I have a problem with showing generated PDF. The pdf is saved in dataBase.
I have problem converting .NET C# AES 128 Encryption to iOS (iPhone). Everything generated
My problem.. I have a PHP array that looks like this: [1013] => [1154]
I have a form that is generated via ajax based of a multi-file uploader

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.