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

  • Home
  • SEARCH
  • 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 9086641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:32:19+00:00 2026-06-16T21:32:19+00:00

I’ve got this code and I don’t know how to order my query function

  • 0

I’ve got this code and I don’t know how to order my query

function get_All_Categories(){
//new instance of mysqli
    $mysqli = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME);
    //if it doesn't work give an error
    if (!$mysqli) {
        die('There was a problem connecting to the database.');
    }
    $queryCats ="SELECT Catid, Catname
                 FROM ProductCats
                 ORDER BY Catid";
    $querySubCats ="SELECT Subcatname, Parentid
                    FROM ProductSubCats, ProductCats
                    WHERE ProductSubCats.Parentid = ProductCats.Catid
                    ORDER BY Subcatname;";
    if ($catResults = $mysqli->query($queryCats)){
        if (!$catResults) {
            echo 'Could not run query: ' . mysql_error();
            exit;
        }
        else{
            $subCatResults = $mysqli->query($querySubCats);
        }
        while ($rows = $catResults->fetch_assoc()) {
            echo '<div class = "categoryCluster">';
            echo '<a href="../index.php?Cat='.$rows["Catname"].'">'.$rows["Catname"].'</a>';
            echo '<br>';
            while($rowsSub = $subCatResults->fetch_assoc()){
                echo '<div class="subCategoryCluster">';
                echo '&emsp;'.'<a href="../index.php?Cat='.$rows["Catname"].'&'.'Subcat='.$rowsSub["Subcatname"].'">'.$rowsSub["Subcatname"].'</a>';
                echo '<br>';
                echo '</div>';
            }
            echo '</div>';
        }
    }
    $mysqli->close();
}

Which displays this output:

Header 1
 Subcat 1
 Subcat 2
 OtherSubcat 1
Header 2
Header 3
Header 4
Header 5
Header 6

But my problem is that subcat 1 and subcat 2 have the parentid of 1, which conforms to header 1, however othersubcat 1 has the parentid of 5. Which actually conforms to header 5.
Yeh in my loop it is displaying right after the other 2.

Now I know why this is wrong, it is because the while loop in the while loop is ordering it to appear last, and sticking it in there.

But I have absolutely no idea how to rework this so that it would display:

Header 1
 Subcat 1
 Subcat 2
Header 2
Header 3
Header 4
Header 5
 OtherSubcat 1
Header 6

Sample table:

>     ProductSubCats
>     Subcatid  |  Subcatname   | Parentid
>     1         | Subcat 1      | 1
>     2         | Subcat 2      | 1
>     3         | Othersubcat 3 | 5
> 
> 
> 
> ProductCats
>     Cat id   |   Catname
>     1        | Header 1
>     2        | Header 2
>     3        | Header 3
>     4        | Header 4
>     5        | Header 5
>     6        | Header 6

Thank you for your help

  • 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-16T21:32:20+00:00Added an answer on June 16, 2026 at 9:32 pm

    For what you are trying to do, following off what @Pasha Bitz said.

    Try the following:

    $categories = array();
            $sub_categories = array();  
            while ($rows = $catResults->fetch_assoc()) {
                $categories[$rows['Catid']] = $rows['Catname'];
            }
            while($rows = $subCatResults->fetch_assoc()){
                if (!isset($sub_categories[$rows['Parentid']])) {
                    $sub_categories[$rows['Parentid']] = array();
                }
                $sub_categories[$rows['Parentid']][] = $rows['Subcatname'];
            }
            foreach ($categories as $cat_id => $name) {
                echo '<div class = "categoryCluster">';
                echo '<a href="../index.php?Cat='.$name.'">'.$name.'</a>';
                echo '<br>';
                if (isset($sub_categories[$cat_id])) {
                    foreach($sub_categories[$cat_id] as $cat_id => $sub_cat_name) {
                        echo '<div class="subCategoryCluster">';
                        echo '&emsp;'.'<a href="../index.php?Cat='.$name.'&'.'Subcat='.$sub_cat_name.'">'.$sub_cat_name.'</a>';
                        echo '<br>';
                    }
                }
            }
    

    That should resolve this for you, study the code so you understand what is going on here.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I know there's a lot of other questions out there that deal with this
Does anyone know how can I replace this 2 symbol below from the string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
this is what i have right now Drawing an RSS feed into the php,

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.