4

This is what my forums currently look like: http://prntscr.com/73oicl

But, as you can see, the categories both say "community", which isn't supposed to happen. The "Test" subcategory should be right under the "Announcements & Updates" subcategory.

I know what the problem is, but I don't know how to fix it.

    function getForums($id) {
    $currentHost = "http://$_SERVER[HTTP_HOST]";
    $query = "SELECT * FROM forums, categories";
    try {
        global $db;
        $stmt = $db->prepare($query); 
        $stmt->execute();
        $result = $stmt->fetchAll();
        foreach($result as $row) {
            $category_title = $row['category_title'];
            $forum_id = $row['forum_id'];
            $forum_title = $row['forum_title'];
            $forum_topic_count = $row['forum_topic_count'];
            $forum_post_count = $row['forum_post_count'];
            $forum_last_topic_id = $row['forum_last_topic_id'];
            $forum_last_topic = $row['forum_last_topic'];
            $forum_last_date = $row['forum_last_date'];
            $forum_last_user = $row['forum_last_user'];
            $fixed_last_topic = substr($forum_last_topic,0,25).'...';
            echo '<div class="forum pleft">
                    <div class="forum-header">
                        <span class="header-text">'.$category_title.'</span>
                    </div>
                    <table>
                        <tr>
                            <td class="title"><a href="'.$currentHost.'/forums/view-forum/index.php?cid='.$category_id.'&fid='.$forum_id.'">'.$forum_title.'</a></td>
                            <td class="topics">'.$forum_topic_count.'</td>
                            <td class="posts">'.$forum_post_count.'</td>
                            <td class="lastpost"><a href="'.$currentHost.'/forums/view-thread/index.php?cid='.$id.'&fid='.$forum_id.'&tid='.forum_last_topic_id.'">'.$fixed_last_topic.'</a> by <a href="'.$currentHost.'/users/index.php?username='.$forum_last_user.'">'.$forum_last_user.'</a> at '.$forum_last_date.'</td>
                        </tr>
                    </table>
                </div>';
        }
    }
    catch(PDOException $ex) {
        die("error");
    }
}

As you can see, for each result its going to make a whole new forum div, which means all the Subcategories that are given for that Category aren't going to be together, and each subcategory is going to make a new forum, which I don't want. Is there a way I could like explode the echo to make it so if 2 or more subcategories are in 1 category it won't put out the new div?

Joel E
  • 103
  • 1
  • 7
  • Is there any relation between those two tables, `forums` and `categories`? – Ofir Baruch May 10 '15 at 16:22
  • no, there isn't. @Ofir Baruch – Joel E May 10 '15 at 19:12
  • So it should, otherwise - how would you know which forums belong to which category? Consider adding a new field `category_id` to your `forums` table which will contain the `ID` of the `category parent`. – Ofir Baruch May 11 '15 at 05:16
  • Oh I didn't think you asked it like that. In that case yes, there is, category_id already exists. I still need help with this though, as it makes a new div for each subcategory. – Joel E May 11 '15 at 18:55

1 Answers1

0

I wrote this without testing, so you may want to check for errors. This may not be the most efficient way, but it should work.

function getForums($id) {
    $currentHost = "http://$_SERVER[HTTP_HOST]";
    $query = "SELECT * FROM forums, categories ORDER BY categories ASC"; /* Order by categories to arrange same categories together */
    try {
        global $db;
        $stmt = $db->prepare($query); 
        $stmt->execute();
        $result = $stmt->fetchAll();
        $numrows = $stmt->fetch(PDO::FETCH_NUM); /* Check for total rows of records */

        $count = 0;
        $currentCategory = ""; // Declare container

        foreach($result as $row) {
            $category_title = $row['category_title'];
            $forum_id = $row['forum_id'];
            $forum_title = $row['forum_title'];
            $forum_topic_count = $row['forum_topic_count'];
            $forum_post_count = $row['forum_post_count'];
            $forum_last_topic_id = $row['forum_last_topic_id'];
            $forum_last_topic = $row['forum_last_topic'];
            $forum_last_date = $row['forum_last_date'];
            $forum_last_user = $row['forum_last_user'];
            $fixed_last_topic = substr($forum_last_topic,0,25).'...';

            /* If currentCat is empty OR is not similar to previous */
            if($currentCategory == "" || $currentCategory != $category_title){
                /* If currentcat is not empty AND not similar to previous, close */
                if($currentCategory != "" && $currentCategory != $category_title){
                    echo '</table></div>';
                }

                echo '<div class="forum pleft">
                    <div class="forum-header">
                        <span class="header-text">'.$category_title.'</span>
                    </div>
                    <table>
                        <tr>
                            <td class="title"><a href="'.$currentHost.'/forums/view-forum/index.php?cid='.$category_id.'&fid='.$forum_id.'">'.$forum_title.'</a></td>
                            <td class="topics">'.$forum_topic_count.'</td>
                            <td class="posts">'.$forum_post_count.'</td>
                            <td class="lastpost"><a href="'.$currentHost.'/forums/view-thread/index.php?cid='.$id.'&fid='.$forum_id.'&tid='.forum_last_topic_id.'">'.$fixed_last_topic.'</a> by <a href="'.$currentHost.'/users/index.php?username='.$forum_last_user.'">'.$forum_last_user.'</a> at '.$forum_last_date.'</td>
                        </tr>';
                $count++;

                /* Check if its last row, If yes, end */
                if($count == $numrows){
                    echo '</table></div>';
                }

                $currentCategory = $category_title; /*Set category title */
             }
             /* If next row belong to the same category, If yes, continue */
             else if($currentCategory == $category_title){
                echo '<tr>
                        <td class="title"><a href="'.$currentHost.'/forums/view-forum/index.php?cid='.$category_id.'&fid='.$forum_id.'">'.$forum_title.'</a></td>
                        <td class="topics">'.$forum_topic_count.'</td>
                        <td class="posts">'.$forum_post_count.'</td>
                        <td class="lastpost"><a href="'.$currentHost.'/forums/view-thread/index.php?cid='.$id.'&fid='.$forum_id.'&tid='.forum_last_topic_id.'">'.$fixed_last_topic.'</a> by <a href="'.$currentHost.'/users/index.php?username='.$forum_last_user.'">'.$forum_last_user.'</a> at '.$forum_last_date.'</td>
                    </tr>';
                $count++;

                /* Check if its last row, If yes, end */
                if($count == $numrows){
                    echo '</table></div>';
                }
            }
        }
    }
    catch(PDOException $ex) {
        die("error");
    }
}
clouty
  • 33
  • 1
  • 6