Note that there are some explanatory texts on larger screens.

plurals
  1. POWhile inside While from different MySQL table
    text
    copied!<p>i have 2 tables:</p> <ol> <li><strong>Cat</strong></li> </ol> <p>id,cat_name</p> <ol start="2"> <li><strong>SubCat</strong></li> </ol> <p>id, subcat_name, under_cat</p> <p>i want to make a list that loads the sub category into the correct Category the problem is with the second loop, i cant make it work what am i doing wrong?</p> <pre><code> &lt;? $query = "SELECT * FROM `Cat`"; $result = $mysqli-&gt;query($query); while ($row = $result-&gt;fetch_assoc()){ echo '&lt;li&gt;&lt;a href="#"&gt;' . $row['cat_name'] . '&lt;/a&gt;&lt;ul&gt;'; $query2 = "SELECT * FROM `SubCat` WHERE under_cat = '". $row['cat_id'] ."'"; $result2 = $mysqli-&gt;query($query2); while ($row2 = $result-&gt;fetch_assoc()){ echo '&lt;li&gt;&lt;a href="#"&gt;' . $row2['subcat_name'] . '&lt;/a&gt;&lt;/li&gt;'; } echo '&lt;/ul&gt;&lt;/li&gt;'; } ?&gt; </code></pre> <p><strong>EDIT:</strong></p> <p><img src="https://i.stack.imgur.com/8EEPC.jpg" alt="enter image description here"> two lists from same category</p> <p>this is the code:</p> <pre><code> &lt;? $query = "SELECT * FROM Cat LEFT OUTER JOIN SubCat ON SubCat.under_cat = Cat.cat_id;"; $result = $mysqli-&gt;query($query); while ($row = $result-&gt;fetch_assoc()){ echo '&lt;li&gt;&lt;a href="#"&gt;' . $row['cat_name'] . '&lt;/a&gt;&lt;ul&gt;'; if($row['cat_id'] === $row['under_cat']){ echo '&lt;li&gt;&lt;a href="#"&gt;'.$row['subcat_name'].'&lt;/a&gt;&lt;/li&gt;'; </code></pre> <p>}</p> <pre><code> echo '&lt;/ul&gt;&lt;/li&gt;'; } ?&gt; </code></pre>
 

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