Click here to Skip to main content
15,881,044 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
Ive been having a very tedious problem lately where Im not able to display the topics in a forum category. I have been able to display all categories in a forum-style table but when I click on one of the categories I am getting the following error but I dont understand where in my code it is going wrong:

Notice: Undefined index: topic_cat in .../category.php on line 44

I want to get the topics that are linked with a specific category (topic_cat links to cat_id). So for cat_id=2, I want all topics that are assigned topic_cat=2.

Below is my code, can anyone see any problems? Note: this is the code that is currently being processed:

PHP
else
        {
            if(mysql_num_rows($topicResult) == 0)
            {
                echo 'There are no topics in this category yet.';
            }


And this is my code:

PHP
  else
    {
        //display category data
        while($row = mysql_fetch_assoc($result))
        {
            echo '<h2>Topics in ′' . $row['cat_name'] . '′ category</h2>';
        }
     
        //do a query for the topics
        $topicSql = "SELECT 
                    topic_id,
                    topic_subject,
                    topic_date,
                    topic_cat
                FROM
                    topics
                WHERE
 44-->      topic_cat = '" . mysql_real_escape_string($_GET['topic_cat']) . "'";
         
        $topicResult = mysql_query($topicSql);
         
        if(!$topicResult)
        {
            echo 'The topics could not be displayed, please try again later.';
        }
        else
        {
            if(mysql_num_rows($topicResult) == 0)
            {
                echo 'There are no topics in this category yet.';
            }
            else
            {
                //prepare the table
                echo '<table border="1">
                      <tr>
                        <th>Topic</th>
                        <th>Created at</th>
                      </tr>';
                     
                while($row = mysql_fetch_assoc($topicResult))
                {              
                    echo '<tr>';
                        echo '<td class="leftpart">';
                            echo '<h3><a href="topic.php?topic_id=' . $row['topic_id'] . '">' . $row['topic_subject'] . '</a><h3>';
                        echo '</td>';
                        echo '<td class="rightpart">';
                            echo date('d-m-Y', strtotime($row['topic_date']));
                        echo '</td>';
                    echo '</tr>';
                }
            }
        }
    }
}
Posted

1 solution

this may be probably because the get variable takes the value passed in the url... like products.php?cat_id=2

so you can get cat_id=2 in this example...

may be you are not passing any value so the $_GET in line 44 is not getting any value...

you can check that via a issset function, if isset($_GET['topic_cat']) returns null it is not set and hence you can set it to default value to that you can see the results.
 
Share this answer
 
Comments
jba1991 5-Jul-14 9:07am    
Okay cool. So where in the code would I put the isset function? does it replace any existing code?
ShubhamSaxena 6-Jul-14 15:20pm    
if(isset($_GET['cat_id']))
$cat_id = $_GET['cat_id'];
else
$cat_id = 2 //let 2 be the default category

Now perform the sql query for the category by using the $cat_id to display the topics

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900