Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8345937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T06:50:58+00:00 2026-06-09T06:50:58+00:00

I have no clue as to why but sub-categories forums of my script aren’t

  • 0

I have no clue as to why but sub-categories “forums” of my script aren’t being groped to there respective category. Instead the sub-category forces the script to repeat the Category code.

As show in the image below there is a category Test, in that category theres Test and Bugs. However Bugs is slip into 2nd table which it should not be as both Test categories are one, but instead its made to look like there are two categories in the database called Test. What is wrong in my code that Bugs is being pushed into copy-Category “Test” instead of it being right under Test – Just a simple test forum?

enter image description here

And here is the code.

<?php

include 'connect.php';
include 'header.php';

$sql = "SELECT
            categories.cat_id,
            categories.cat_name,
            forums.forum_id,
            forums.forum_cat,
            forums.forum_name,
            forums.forum_desc,
            COUNT(forums.forum_id) AS forums
        FROM
            categories
        LEFT JOIN
            forums
        ON
            forums.forum_cat = categories.cat_id
        GROUP BY
            forums.forum_name, forums.forum_desc, forums.forum_id
        ORDER BY
            categories.cat_id ASC
        ";

$result = mysql_query($sql);

if(!$result)
{
    echo 'The categories could not be displayed, please try again later.';
}
else
{
    if(mysql_num_rows($result) == 0)
    {
        echo 'No categories defined yet.';
    }
    else
    {
        //prepare the table         
        while($row = mysql_fetch_assoc($result))
        {   

        echo '<table border="1">
              <tr>
                <th>' . $row['cat_name'] . '</th><th></th>
                </tr>';     
            echo '<tr>';

                echo '<td class="leftpart">';
                    echo '<h3><a href="viewforum.php?id=' . $row['forum_id'] . '">' . $row['forum_name'] . '</a></h3>' . $row['forum_desc'];
                echo '</td>';
                echo '<td class="rightpart">';

                //fetch last topic for each cat
                    $topicsql = "SELECT
                                    topic_id,
                                    topic_subject,
                                    topic_date,
                                    topic_cat
                                FROM
                                    topics
                                WHERE
                                    topic_cat = " . $row['forum_id'] . "
                                ORDER BY
                                    topic_date
                                DESC
                                LIMIT
                                    1";

                    $topicsresult = mysql_query($topicsql);

                    if(!$topicsresult)
                    {
                        echo 'Last topic could not be displayed.';
                    }
                    else
                    {
                        if(mysql_num_rows($topicsresult) == 0)
                        {
                            echo 'no topics';
                        }
                        else
                        {
                            while($topicrow = mysql_fetch_array($topicsresult))
                            echo '<a href="viewtopic.php?id=' . $topicrow['topic_id'] . '">' . $topicrow['topic_subject'] . '</a> at ' . date('d-m-Y', strtotime($topicrow['topic_date']));
                        }
                    }
                echo '</td>';
            echo '</tr>';
            echo '</br>';
        }
    }
}

include 'footer.php';
?>

Table structure

Categories

cat_id     | int(8) | primary | auto incr
cat_name   | var(255)

|2|Damm
|3|Hmm
|1|Test

Forums

forum_id   | int(8) | primary | auto_incr
forum_cat  | int(8) <-- forum cat "category" is just ID of category it belongs to
forum_name | var(255)
forum_desc | var(255)

|1|1|Test|Just a simple forum test
|2|3|Lol
|3|1|Bugs|Bugs and related go here

Topics

topic_id   | int(8) | primary | auto_incr
topic_subject | var(255)
topic_date    | datetime
topic_cat     | int(8) 
topic_by      | int(8)

|1|Test|2012-07-31 23:12:47|1|1

I personally tried different things but still can’t figure out why this is happening.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T06:51:00+00:00Added an answer on June 9, 2026 at 6:51 am

    You will have results along the lines of

    cat_id 1, forum_id 1
    cat_id 1, forum_id 2
    cat_id 1, forum_id 3
    cat_id 2, forum_id 4
    

    But you output everything in one loop, so what you need to do is remember the last header shown, and only show a new header if you’re in a new category.

    The below is an example, not perfect, but will give you a start

    // Start the table here - outside the loop
    echo '<table border="1">  
    
    // Here's how you track if you need the header
    $lastCatID = -1
    
    // Now loop
    while($row = mysql_fetch_assoc($result))  
    {     
        // Only show cat header on condition
        if ($lastCatID <> $row['cat_id']) {
            echo '
              <tr>  
                <th>' . $row['cat_name'] . '</th><th></th>  
                </tr>';
    
            // Note that you've shows this header so you don't show it again.
            $lastCatID = $row['cat_id'];
        }
    
        // Now output the rest       
        echo '<tr>';  
        echo '<td class="leftpart">';  
            etc....
        echo '</tr>';  
    }
    
    // Now close the table now you're all done looping.
    echo '</table>';
    

    Hope that helps. You should be able to expand from there to fit your styles.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I hate to be the village idiot, but I have no clue how Linq
I'm trying to install the stored procedure WhoIsActive , but have no clue where
I have read several tutorials but I still do not have any clue :-)
I got this as home exercise but I have no clue how to solve
I have an URL that I need to change, but I have no clue
My problem is very basic, but I have no clue how to solve it
This is maybe a simple question, but I don't have a clue the keyword
I have no clue why this is happening but maybe someone here knows. I
I am trying to make simple testing but failed because I have no clue
I'm trying to add package level annotations but I don't have a clue on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.