Iterating through rows

Hi,

I am trying to display a forum by the category the forum it exists. So what i need to do is display the forum category and display all the forums within that category and so on…

This is the page i am working on:

http://www.freemanholland.com/kidsintranet/public_html/coffee-break

I have the following code:


<div id="forum-layout">
                <table class='tabulardata' cellspacing='0' rules='all'>
                    <tr>
                        <th align="left">Forum</th>
                        <th>Threads</th>
                        <th class="stat-width">Posts</th>
                        <th>Recently Posted</th>
                    </tr>
                <?
                    $prevRow = null;
                    $i = 1;
                    while ($row = mysql_fetch_object($forums)) {
                       if ($prevRow !== null) {
                            if($row->name != $saveForumCategory) {
                                echo "<tr><td colspan='4' class='tdborder'><strong>$row->name</strong></td></tr>";
                                $cycle = false;
                                $saveForumCategory = $row->name;
                            }
                       } else {
                                echo "<tr><td colspan='4' class='tdborder'><strong>$row->name</strong></td></tr>";
                                $cycle = false;
                                $saveForumCategory = $row->name;
                       }
                ?>
                    <tr>
                        <td class="td-forumname"><a href='<?=__SITE_PATH?>coffee-break/topics'><?=$row->theName?></a><br/><?=$row->body?></td>
                        <td class='tdstats'>1</td>
                        <td class='tdstats'>1</td>
                        <td align="center">21 Apr 2010 01:11:58 PM</td>
                    </tr>
                    <?
                         $prevRow = $row;
                         $i++;
                         $i = $i % 2;
                    }
                ?>
                </table>
            </div>
        </div>

I nearly have it working but as you can see the “Forum rules” category is displayed twice because there are 2 forums within it. So i need to have the forum category showing only once… Any ideas where i am going wrong?

This is the method i am using:


    public function selectAllForums(){

        $sql = "SELECT *, i.ID as theID, i.name as theName FROM forums i
                INNER JOIN forum_category c ON c.ID = i.catID WHERE i.deleted = 0
                ORDER BY i.date_added ASC";
        $result = mysql_query($sql);
        return $result;
    }

Can someone please help me out?

Thanks

I think you might make your life a bit easier by processing your query results before they get passed to your template. Notice that you duplicate code in your template processor and that you have a bunch of previous record stuff. All of that is needed because you are basically converting a flat record to a tree structure.

Consider adding a bit of code to yourSelectAllForums function. The function should return an array that looks something like:


$data = array(
  'cat1' => array('forum1','forum2','forum3'),
  'cat2' => array('forum8','forum9')
);

Now your template processor loops once over $data to get the category names then does a nested loop to process each forum. The code will be much cleaner and easier to style.

I like it this way. But since you’re using a table, to get empty space between two rows you’d have to add an extra row, and style it as to not show any borders.

Thanks Guido :wink:

I am trying to split up the first and second forum, so basically the “Games and activities” forum i want there to be a gap at the top to split the forums up in a sense.

I have tried putting in line breaks and styling but it doesn’t working, can you point me in the right direction if possible?

Thanks

Add the category id (or name, whatever you want the categories to be ordered by) to the ORDER BY, so it first orders by category, and then by date.