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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:40:23+00:00 2026-06-12T02:40:23+00:00

I have a data set which looks similar to: ——————————————————————— id | category |

  • 0

I have a data set which looks similar to:

---------------------------------------------------------------------
id | category | sub_category | category_href | sub_category_href    |
01 | cat_1    | sub_cat 1    | cat/cat1.php  | cat/cat1/sub_cat1.php|
02 | cat_1    | sub_cat 2    | cat/cat1.php  | cat/cat1/sub_cat2.php|
03 | cat_2    | sub_cat 1    | cat/cat2.php  | cat/cat2/sub_cat1.php|
04 | cat_2    | sub_cat 2    | cat/cat2.php  | cat/cat2/sub_cat2.php|
---------------------------------------------------------------------

What I want to do with the data is have a layout like this:

<div>
  <h2 class="title">Cat1</h2>
  <p>
   <ul class="links">
      <li><a href="cat/sub_cat_1.php" target="_top">sub_cat_1</a></li>
      <li><a href="cat/sub_cat_2.php" target="_top">sub_cat_2</a></li>
  </p>
</div>
<div>
  <h2 class="title">Cat2</h2>
  <p>
    <ul class="links">
        <li><a href="cat/sub_cat_1.php" target="_top">sub_cat_1</a></li>
        <li><a href="cat/sub_cat_2.php" target="_top">sub_cat_2</a></li>
    </p>
</div>

So my question is how would I do this with php so that I can change the categories and sub categories from my db. The following is what I have below but I need a second loop for the sub categories or it will only have one sub category per category. Could someone point me in the right direction for the sub_cat loop. Thank you

EDIT:

So my question is now that I have distinct categories how I can echo the appropriate sub categories?

Thanks

<?php
include('connect.php');
$result = mysql_query("SELECT DISTINCT category FROM categories")
    or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
    echo "<div>";
    echo "<h2 class='title'>" . $row['category'] . "</h2>";
    echo "<p>";
    echo "<ul class='links'>";
    $result1 = mysql_query("SELECT * FROM categories ")
        or die (mysql_error());
    while ($row = mysql_fetch_array($result1)) {
        echo "<li><a href='" . $row['sub_category_href'] . " target='_top'>" . $row['sub_category'] . "</a></li>";
    }
    echo "</ul>";
    echo "</p>";
    echo "</div>";
}
?>
  • 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-12T02:40:24+00:00Added an answer on June 12, 2026 at 2:40 am

    That code could be looked like this:

    <?php
    include('connect.php');
    $result = mysql_query("SELECT DISTINCT category FROM categories")
        or die(mysql_error());
    while ($row = mysql_fetch_array($result)) {
        echo "<div>";
        echo "<h2 class='title'>" . $row['category'] . "</h2>";
        echo "<p>";
        echo "<ul class='links'>";
        $result1 = mysql_query("SELECT * FROM categories WHERE category = {$row['category']}")
                or die (mysql_error());
        while ($row1 = mysql_fetch_array($result1)) {
            echo "<li><a href='" . $row1['sub_category_href'] . " target='_top'>" . $row1['sub_category'] . "</a></li>";
        }
        echo "</ul>";
        echo "</p>";
        echo "</div>";
    }
    ?>
    

    But I suggest you to optimize in this manner:

    <?php
    include('connect.php');
    $result = mysql_query("SELECT * FROM categories")
        or die(mysql_error());
    $cats = array();
    while ($row = mysql_fetch_array($result)) {
        $category = $row['category'];
        $sub_category = $row['sub_category'];
        $sub_category_href = $row['sub_category_href'];
        $cat_hrefs[$category][$sub_category] = $sub_category_href;
    }
    foreach ($cat_hrefs as $category => $sub_category_hrefs) {
        echo "<div>";
        echo "<h2 class='title'>" . $category . "</h2>";
        echo "<p>";
        echo "<ul class='links'>";
        foreach ($sub_category_hrefs as $sub_category => $sub_category_href) {
            echo "<li><a href='" . $sub_category_href . " target='_top'>" . $sub_category . "</a></li>";
        }
        echo "</ul>";
        echo "</p>";
        echo "</div>";
    }
    ?>
    

    You do not need to make three queries, because you can do it in one query, which is more optimal.

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

Sidebar

Related Questions

I have a data set which looks something like data<-c(0,1,2,3,4,2,3,1,4,3,2,4,0,1,2,0,2,1,2,0,4) frame<-as.data.frame(data) I now want
I have a set of data that looks similar to this: [ {name:item.key ,
I have a data set which consists of an ID and a matrix (n
I have the swiss data set provided by R, which has the following form:
I have a large set of data which I access via a generator/iterator. While
I have a set of data points in 3D space which apparently all fall
I have a mysql table field set as time type which stores data in
I have a set of controls bound to data, on which I would like
I have several objects set up in Core Data, one of which is Deck
I'm pretty new to Entity Frameworks. I have a set of Static Data which

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.