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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:36:27+00:00 2026-05-30T10:36:27+00:00

I’m pretty new to php but i’m learning pretty fast, I have a question

  • 0

I’m pretty new to php but i’m learning pretty fast, I have a question I hope you pro’s may be able to help me with. I’ve done some googling and can’t find the answer.

I’m trying to do a dynamic dropdown menu with two levels – I have set up a database and have installed the data, the first level has a primary key and category name, the next level has a primary key, foriegn key and a subcategory name.

I have created a do while loop for the categories which works super! Now I’m trying to insert the sub categories. I’ll trying to summarise what I am trying to acheive.

Do while loop of the categories in between each catergory run another loop display all of the sub categories where the primary key of the category matches the foriegn key of the sub categories. Then continue.

e.g.

Category
  Sub category
  Sub category
  Sub category 
Category
  Sub category
Category
  Sub category
  Sub category

and so on….

Any advice on how this would be coded?

I hope this makes sense.

Thank you in advance!

Kindest Regards.

<?php 

// Query member data from the database and ready it for display

$category_sql = "SELECT * FROM tm_product_category";
$category_query = mysql_query($category_sql) or die(mysql_error());
$categorylist = mysql_fetch_assoc($category_query);
?>
<?php 

//Connect to the database through our include 
include_once "connect_to_mysql.php";
// Query member data from the database and ready it for display

$subcategory_sql = "SELECT * FROM tlk_sub_cat";
$subcategory_query = mysql_query($subcategory_sql) or die(mysql_error());
$subcategorylist = mysql_fetch_assoc($subcategory_query);
?>








<?php do { ?>

    <ul><li><a href="category.php?pk_cat_id=<?php echo $categorylist['pk_cat_id'];?>"><?php echo  $categorylist['category']; ?></a></ul>

      <?php

 if ($categorylist['pk_cat_id'] == $subcategorylist['fk_cat_id'])
    {
     do { ?>

           <li><a href="subcategory.php?pk_cat_id=<?php echo $subcategorylist['pk_sub_cat_id'];?>"><?php echo $subcategorylist['txt_sub_cat']; ?></a></li>

          <?php } while ($subcategorylist = mysql_fetch_assoc($subcategory_query));
        ; } 
    ?>

    <?php } while ($categorylist = mysql_fetch_assoc($category_query));

    ?>

All i’m getting ATM is;

Category
List of all of the subcategories
Category
Space
Category
Space
Category
Space
until categories run out.

  • 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-30T10:36:29+00:00Added an answer on May 30, 2026 at 10:36 am

    You could execute a single query with a JOIN, such as INNER JOIN, then iterate through the returned set of records. If the previous foreign key is different than the current foreign key, you are on a new primary record, {do magic} related to primary record and then {do magic} for the current sub record, otherwise, just {do magic} for the current sub record.

    edit:

    SELECT tm_product_category.*, tlk_sub_cat.* 
    FROM tm_product_category INNER JOIN tlk_sub_cat ON
    tm_product_category.pk_cat_id = tlk_sub_ca.fk_cat_id
    

    You reference each tables’ fields by prepending the table name and a period. AFAIK with mysql, you can leave off the table name if the field is unique, but usually leave in for completeness. MySQL website has docs talking about other join types.

    • 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 want to count how many characters a certain string has in PHP, but
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
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) 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
I have a jquery bug and I've been looking for hours now, I can't
Seemingly simple, but I cannot find anything relevant on the web. What is the

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.