Tables should never be used for layout, but I couldn't find a better way
<p>So I'm generating an html table through my php to arrange my content.
Unfortunately, the code is ugly, hard to work with, and as we all know, tables should NEVER EVER be used for layout.</p>

<p>The problem is that based on the content I wanted to put on my page from my database, I couldn't figure out any other option.</p>

<p>I would prefer to use divs or lists here, but I can't figure out how best to do it. If the content wasn't dynamic, it'd be easy, but it's a little more complicated than that.</p>

<p>Here's my php:</p>

<pre><code>&lt;?php
    $sql = 'SELECT * FROM titles';
    $result = $dbh-&gt;prepare($sql);
    $entries_per_row = 2;

    $i = 1;
    $j = 1;
    foreach ($dbh-&gt;query($sql) as $row) {           
        echo "&lt;table class=\"category_list\"&gt;\r\n".
            "&lt;tr&gt;\r\n".
            "&lt;td colspan=\"".$entries_per_row."\"&gt;\r\n".
            "&lt;a id=\"sh".$i."\" href=\"#\" class=\"showhide\"&gt;-&lt;/a&gt;&amp;nbsp;&lt;span class=\"title\"&gt;".$row['TitleName']."&lt;/span&gt;\r\n".
            "&lt;/td&gt;\r\n".
            "&lt;/tr&gt;\r\n";

        $sql2 = 'SELECT * FROM categories WHERE TitleID = '.$row['TitleID'];
        $rows2 = array();
        foreach ($dbh-&gt;query($sql2) as $row2) {
            $rows2[] = $row2;
        }

        $rows2 = array_chunk($rows2, $entries_per_row);

        foreach ($rows2 as $row2) {
            echo "&lt;tr id=\"".$i."tr".$j."\"&gt;\r\n";

            foreach($row2 as $element) {
                if ($element['CategoryName'] != '' &amp;&amp; $element['CategorySummary'] != '') {
                    echo "&lt;td class=\"cat\"&gt;\r\n".
                        "&lt;a class=\"cat_title\" href=\"display.php?cat=".$element['CategoryName']."&amp;page=1\"&gt;".$element['CategoryName']."&lt;/a&gt;&lt;br /&gt;\r\n".
                        "&lt;span class=\"cat_sum\"&gt;".$element['CategorySummary']."&lt;/span&gt;\r\n".
                        "&lt;/td&gt;\r\n";
                }
            }
            $j = $j + 1;
            echo "&lt;/tr&gt;\r\n";
        }

        $i = $i + 1;
        echo "&lt;/table&gt;\r\n";
    }
?&gt;
</code></pre>

<p>Here's the HTML that it outputs (indenting included):</p>

<pre><code>&lt;table class="category_list"&gt;
    &lt;tr&gt;
        &lt;td colspan="2"&gt;
            &lt;a id="sh1" href="#" class="showhide"&gt;-&lt;/a&gt;&amp;nbsp;&lt;span class="title"&gt;Test Title 1&lt;/span&gt;
        &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr id="1tr1"&gt;
        &lt;td class="cat"&gt;
            &lt;a class="cat_title" href="display.php?cat=Test Category 1&amp;page=1"&gt;Test Category 1&lt;/a&gt;&lt;br /&gt;
            &lt;span class="cat_sum"&gt;This is a test category. It will be deleted soon.&lt;/span&gt;
        &lt;/td&gt;
        &lt;td class="cat"&gt;
            &lt;a class="cat_title" href="display.php?cat=Test Category 2&amp;page=1"&gt;Test Category 2&lt;/a&gt;&lt;br /&gt;
            &lt;span class="cat_sum"&gt;This is a test category. It will be deleted soon.&lt;/span&gt;
        &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr id="1tr2"&gt;
        &lt;td class="cat"&gt;
            &lt;a class="cat_title" href="display.php?cat=Test Category 3&amp;page=1"&gt;Test Category 3&lt;/a&gt;&lt;br /&gt;
            &lt;span class="cat_sum"&gt;This is a test category. It will be deleted soon.&lt;/span&gt;
        &lt;/td&gt;
    &lt;/tr&gt;
&lt;/table&gt;

&lt;table class="category_list"&gt;
    &lt;tr&gt;
        &lt;td colspan="2"&gt;
            &lt;a id="sh2" href="#" class="showhide"&gt;-&lt;/a&gt;&amp;nbsp;&lt;span class="title"&gt;Test Title 2&lt;/span&gt;
        &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr id="2tr3"&gt;
        &lt;td class="cat"&gt;
            &lt;a class="cat_title" href="display.php?cat=Test Category 4&amp;page=1"&gt;Test Category 4&lt;/a&gt;&lt;br /&gt;
            &lt;span class="cat_sum"&gt;This is a test category. It will be deleted soon.&lt;/span&gt;
        &lt;/td&gt;
        &lt;td class="cat"&gt;
            &lt;a class="cat_title" href="display.php?cat=Test Category 5&amp;page=1"&gt;Test Category 5&lt;/a&gt;&lt;br /&gt;
            &lt;span class="cat_sum"&gt;This is a test category. It will be deleted soon.&lt;/span&gt;
        &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr id="2tr4"&gt;
        &lt;td class="cat"&gt;
            &lt;a class="cat_title" href="display.php?cat=Test Category 6&amp;page=1"&gt;Test Category 6&lt;/a&gt;&lt;br /&gt;
            &lt;span class="cat_sum"&gt;This is a test category. It will be deleted soon.&lt;/span&gt;
        &lt;/td&gt;
    &lt;/tr&gt;
&lt;/table&gt;

&lt;table class="category_list"&gt;
    &lt;tr&gt;
        &lt;td colspan="2"&gt;
            &lt;a id="sh3" href="#" class="showhide"&gt;-&lt;/a&gt;&amp;nbsp;&lt;span class="title"&gt;Test Title 3&lt;/span&gt;
        &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr id="3tr5"&gt;
        &lt;td class="cat"&gt;
            &lt;a class="cat_title" href="display.php?cat=Test Category 7&amp;page=1"&gt;Test Category 7&lt;/a&gt;&lt;br /&gt;
            &lt;span class="cat_sum"&gt;This is a test category. It will be deleted soon.&lt;/span&gt;
        &lt;/td&gt;
        &lt;td class="cat"&gt;
            &lt;a class="cat_title" href="display.php?cat=Test Category 8&amp;page=1"&gt;Test Category 8&lt;/a&gt;&lt;br /&gt;
            &lt;span class="cat_sum"&gt;This is a test category. It will be deleted soon.&lt;/span&gt;
        &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr id="3tr6"&gt;
        &lt;td class="cat"&gt;
            &lt;a class="cat_title" href="display.php?cat=Test Category 9&amp;page=1"&gt;Test Category 9&lt;/a&gt;&lt;br /&gt;
            &lt;span class="cat_sum"&gt;This is a test category. It will be deleted soon.&lt;/span&gt;
        &lt;/td&gt;
        &lt;td class="cat"&gt;
            &lt;a class="cat_title" href="display.php?cat=Test Category 10&amp;page=1"&gt;Test Category 10&lt;/a&gt;&lt;br /&gt;
            &lt;span class="cat_sum"&gt;This is a test category. It will be deleted soon.&lt;/span&gt;
        &lt;/td&gt;
    &lt;/tr&gt;
&lt;/table&gt;
</code></pre>

<p>If anyone can suggest some php code that'll arrange this correctly using divs or lists, I'd greatly appreciate it!</p>
