Note that there are some explanatory texts on larger screens.

plurals
  1. PODo while loop and Do while loop if the foreign key is equal to the primary key
    primarykey
    data
    text
    <p>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.</p> <p>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.</p> <p>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.</p> <p>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.</p> <p>e.g.</p> <pre><code>Category Sub category Sub category Sub category Category Sub category Category Sub category Sub category </code></pre> <p>and so on....</p> <p>Any advice on how this would be coded?</p> <p>I hope this makes sense.</p> <p>Thank you in advance!</p> <p>Kindest Regards. </p> <pre><code>&lt;?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); ?&gt; &lt;?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); ?&gt; &lt;?php do { ?&gt; &lt;ul&gt;&lt;li&gt;&lt;a href="category.php?pk_cat_id=&lt;?php echo $categorylist['pk_cat_id'];?&gt;"&gt;&lt;?php echo $categorylist['category']; ?&gt;&lt;/a&gt;&lt;/ul&gt; &lt;?php if ($categorylist['pk_cat_id'] == $subcategorylist['fk_cat_id']) { do { ?&gt; &lt;li&gt;&lt;a href="subcategory.php?pk_cat_id=&lt;?php echo $subcategorylist['pk_sub_cat_id'];?&gt;"&gt;&lt;?php echo $subcategorylist['txt_sub_cat']; ?&gt;&lt;/a&gt;&lt;/li&gt; &lt;?php } while ($subcategorylist = mysql_fetch_assoc($subcategory_query)); ; } ?&gt; &lt;?php } while ($categorylist = mysql_fetch_assoc($category_query)); ?&gt; </code></pre> <p>All i'm getting ATM is;</p> <p>Category List of all of the subcategories Category Space Category Space Category Space until categories run out.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload