echo as a list, loop within a loop
<p>I have a data set which looks similar to:</p>

<pre><code>---------------------------------------------------------------------
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|
---------------------------------------------------------------------
</code></pre>

<p>What I want to do with the data is have a layout like this:</p>

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

<p>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</p>

<p>EDIT:</p>

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

<p>Thanks</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 ")
        or die (mysql_error());
    while ($row = mysql_fetch_array($result1)) {
        echo "&lt;li&gt;&lt;a href='" . $row['sub_category_href'] . " target='_top'&gt;" . $row['sub_category'] . "&lt;/a&gt;&lt;/li&gt;";
    }
    echo "&lt;/ul&gt;";
    echo "&lt;/p&gt;";
    echo "&lt;/div&gt;";
}
?&gt;
</code></pre>
