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 7019385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:10:34+00:00 2026-05-27T23:10:34+00:00

I’m trying to develop a hierarchical menu in my e-commerce website in which my

  • 0

I’m trying to develop a hierarchical menu in my e-commerce website in which my categories are dynamically shown in order to add more categories without having to touch the code.

So I’ve organized my database like that, with 3 different ranks for my categories:

I got 3 different ranks:

  • First rank: Informatics [id=1] Accessories[2] vêtements[3] Hifi ..

  • Second rank: Hardware[parent_key=1][id=10] software[parent_key=1][id=11] Men[parent_key=3][id=30] ..

  • Third rank: motherboard[parent_key=10][id=100] processor[parent_key=10][id=101] Windows7[parent_key=11][id=110] Shoes[parent_key=30][id=300] ..

So you’ve understood that the “parent_key” refers to the parent id of my category abd for each category of rank 1 I got several rank 2 categories and so on ..

For now, I’ve hard-coded my menu in something like this:

<div id="main_menu">
  <ul id="nav">
    <li class="current"><a href="<?php echo base_url();?>">Home</a></li>
    <li><a href="#">High Tech</a>
      <ul>
        <li><a href="#">Informatique</a>
          <ul>
            <li><a href="#">Hardware</a></li>
            <li><a href="#">Ecrans</a></li>
            <li><a href="#">Clavier</a></li>
            <li><a href="#">Souris</a></li>
            <li><a href="#">Imprimantes</a></li>

          </ul>
        </li>
        <li><a href="#">TV</a>
          <ul>
            <li><a href="#">LCD</a></li>
            <li><a href="#">Plasma</a></li>
            <li><a href="#">3D</a></li>
          </ul>
        </li>
        <li><a href="#">Appareils Photos</a></li>
        <li><a href="#">GPS</a></li>
        <li><a href="#">Smartphones</a></li>
        <li><a href="#">Lecteur MP3</a></li>
        <li><a href="#">Hi-Fi</a>
          <ul>
            <li><a href="#">Amplificateurs</a></li>
            <li><a href="#">Enceintes</a></li>
            <li><a href="#">Cables</a></li>
            <li><a href="#">Autres</a></li>
          </ul>
        </li>
      </ul>
    </li>
  </ul>
  <br class="clear" />
</div>

I’m coding in MVC, and I don’t know exactly how to build my model, my controller, and my view. I guess I’ll have to do some if/else and foreach loop but I can’t figure it out by myself.

If anyone wanna help to solve this problem, he is more than welcome 🙂

  • 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-05-27T23:10:35+00:00Added an answer on May 27, 2026 at 11:10 pm

    Okay, thanks for all you post, but I found a way to deal with my problem, here my personal solution :

    So I first begin by retrieving all the categories in a array : “allCategories”

    Then, while I build the menu, with while loops and if conditions, I get what I want :

    so we have, for each category

    • Category[‘cat_id’] which is the id of the category

    • Category[‘cat_title’] the name of the cat

    • Category[‘cat_order’] the rank of the category

    • Category[‘cat_parentkey’] the id of the parent of my categorie (Parent of motherboad: Hardware)

      $token1=TRUE;
      $token2=TRUE;
      
      foreach($allCategories as $Categories1){ //we are going to check all the cats :: on fait defiler toutes les catégories
      
          if($Categories1['cat_order']==1){ // if its rank 1 :: si le rang de la categorie est 1
              $key = $Categories1['cat_id']; // we save its ID
              echo '<li><a href="#">';
              echo $Categories1['cat_title'];
              echo '</a>'; // this is the loyaout to print the list, the /li comes further :: on fait la mise en page pour afficher la liste, le /li venant plus bas
      
              foreach($allCategories as $Categories2test){ // We gonna check if there is AT LEAST ONE categorie with an inferior rank, otherwise we do not print the <ul> which produce an ugly bar next to the menu :: on va tester si il existe AU MOINS UNE catégorie de rang inférieur, sinon on n'affiche pas de ul afin d'éviter une barre moche dans le menu
      
                  if($Categories2test['cat_order']==2 AND $Categories2test['cat_parentkey']==$key AND $token1==TRUE){ // We do a test with a token which, once we do 1 loop inside, tell us there is at least one cat with an inferior rank :: on fait donc un test avec un token qui, une fois qu'on passe dedans 1 fois, nous dis qu'i'il y a donc au moins un rang inféireur
      
                      echo '<ul>'; // layout of our menu, is printed only if there is inferioir cats :: mise en forme du sous menu, ne s'affiche donc qu si il ya une categorie de ranf inferieur.
      
                          foreach ($allCategories as $Categories2){ // One again, we check all the cats :: on fait défiler les catégories
                              if($Categories2['cat_order']==2 AND $Categories2['cat_parentkey']==$key){ // If there is at least one of rank 2 so .... :: si il y en a 1 de rang 2 alors ...
                                  $key2 = $Categories2['cat_id'];
                                  echo '<li><a href="#">';
                                  echo $Categories2['cat_title'];
                                  echo '</a>';
      
                                  foreach($allCategories as $Categories3test){
                                      if($Categories3test['cat_order']==3 AND $Categories3test['cat_parentkey']==$key2 AND $token2==TRUE){
                                          echo "<ul>";                                            foreach ($allCategories as $Categories3){
                                              if($Categories3['cat_order']==3 AND $Categories3['cat_parentkey']==$key2){
                                                  $key3 = $Categories3['cat_id'];
                                                  echo '<li><a href="#">';
                                                  echo $Categories3['cat_title'];
                                                  echo '</a>';
                                                  echo "</li>";
                                              }
                                          }
      
                                          echo "</ul>";
                                          $token2=FALSE; 
                                      }
                                  }
                              echo"</li>";
                              }
                          $token2=TRUE;
                          }
      
                      echo'</ul>';
                      $token1=FALSE; // We put our token to FALSE in order to avoid that loop for that particular rank1 category :: on met notre token à FALSE afin de ne plus refaire cette boucle pour cette catégorie de rang1
                  }
              }
              echo "</li>"; 
          }
          $token1=TRUE; // We put the token to TRUE in order to do that loop again for the other rank 1 categorie :: on remet le token à 0 afin de repasser dans la boucle pour la catégorie de rang 1 suivante
      }
      

      ?>

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post

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.