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

The Archive Base Latest Questions

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

I’m really just dipping my toes into the PHP and MySQL waters. I’ve made

  • 0

I’m really just dipping my toes into the PHP and MySQL waters. I’ve made this work the way I want it to, but I know it’s ugly and I’d like to know how it would be done by someone who actually knows what they’re doing.

    function displayCategoryMenu($db){  
       echo '<ul class="class">';
       foreach($db->query('SELECT * FROM categoryDb, thingDb WHERE category = category AND categoryId= "aaa0001"  ORDER BY category, thingName') as $row){
          if($categoryName!== $row['categoryName']){
            $categoryName= $row['CategoryName'];
            echo '<li class="category">'.$categoryName.'</li>';
          }
          echo'<li><a class="fader" href="?&amp;page='.$row['thingId']">'.$row['thingName'].'</a></li>';
      }
      echo '</ul>';

       echo '<ul class="class">';
       foreach($db->query('SELECT * FROM categoryDb, thingDb WHERE category = category AND categoryId= "aaa0002"  ORDER BY category, thingName') as $row){
          if($categoryName!== $row['categoryName']){
            $categoryName= $row['CategoryName'];
            echo '<li class="category">'.$categoryName.'</li>';
          }
          echo'<li><a class="fader" href="?&amp;page='.$row['thingId']">'.$row['thingName'].'</a></li>';
      }
      echo '</ul>';
      $db=null;
    }

This basically results in an unordered list menu, where the first child acts as a header:

    <ul>
       <li>category1Name</li>
       <li><a>thing</a></li>
       <li><a>thing</a></li>
       <li><a>thing</a></li>
    </ul>
    <ul>
       <li>category2Name</li>
       <li><a>thing</a></li>
       <li><a>thing</a></li>
       <li><a>thing</a></li>
    </ul>

or:

Category1

  • Thing
  • Thing
  • Thing

Category2

  • Thing
  • Thing
  • Thing

and so forth…

The only difference in each ul’s code is the “categoryId” (aaa0001, aaa0002, etc…) I can only imagine that I’m going about this the hard way, with the ugliest code that I can muster. I’d love to see how it should be done.

  • 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-06T13:12:49+00:00Added an answer on June 6, 2026 at 1:12 pm

    You should store the list of category ids somewhere in an array, an iterate over that while using a variable in place of the category ID. Something like :

    function displayCategoryMenu($db){  
        $categoryIds = array('aaa0001', 'aaa0002');
    
        foreach ($categoryIds as $categoryId) {
            echo '<ul class="class">';
            foreach($db->query('SELECT * FROM categoryDb, thingDb WHERE category = category AND categoryId= "' . $categoryId . '"  ORDER BY category, thingName') as $row) {
                if($categoryName !== $row['categoryName']){
                    $categoryName = $row['CategoryName'];
                    echo '<li class="category">' . $categoryName . '</li>';
                }
                echo '<li><a class="fader" href="?&amp;page=' . $row['thingId'] . '">' . $row['thingName'] . '</a></li>';
            }
            echo '</ul>';
        }
    }
    

    Here the array $categoryIds contain the list of IDs you have to display, you can generate it dynamically or pass it as a function parameter as you wish, it’s easily expandable. The foreach loop iterate over each element of this array and do exactly the same the HTML display and SQL query job for each of the ID denoted by the $categoryId variable in the loop.

    Sidenote about your $db = null; line : if you don’t pass the $db function parameter as a reference, modifying its value in your function the way yo did it will not have any influence on the original variable, this line is unnecessary here.

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

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I want to construct a data frame in an Rcpp function, but when I
I have just tried to save a simple *.rtf file with some websites and
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text

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.