Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>That code could be looked like this:</p> <pre><code>&lt;?php include('connect.php'); $result = mysql_query("SELECT DISTINCT category FROM categories") or die(mysql_error()); while ($row = mysql_fetch_array($result)) { echo "&lt;div&gt;"; echo "&lt;h2 class='title'&gt;" . $row['category'] . "&lt;/h2&gt;"; echo "&lt;p&gt;"; echo "&lt;ul class='links'&gt;"; $result1 = mysql_query("SELECT * FROM categories WHERE category = {$row['category']}") or die (mysql_error()); while ($row1 = mysql_fetch_array($result1)) { echo "&lt;li&gt;&lt;a href='" . $row1['sub_category_href'] . " target='_top'&gt;" . $row1['sub_category'] . "&lt;/a&gt;&lt;/li&gt;"; } echo "&lt;/ul&gt;"; echo "&lt;/p&gt;"; echo "&lt;/div&gt;"; } ?&gt; </code></pre> <p>But I suggest you to optimize in this manner:</p> <pre><code>&lt;?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 =&gt; $sub_category_hrefs) { echo "&lt;div&gt;"; echo "&lt;h2 class='title'&gt;" . $category . "&lt;/h2&gt;"; echo "&lt;p&gt;"; echo "&lt;ul class='links'&gt;"; foreach ($sub_category_hrefs as $sub_category =&gt; $sub_category_href) { echo "&lt;li&gt;&lt;a href='" . $sub_category_href . " target='_top'&gt;" . $sub_category . "&lt;/a&gt;&lt;/li&gt;"; } echo "&lt;/ul&gt;"; echo "&lt;/p&gt;"; echo "&lt;/div&gt;"; } ?&gt; </code></pre> <p>You do not need to make three queries, because you can do it in one query, which is more optimal.</p>
 

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