0

I have noticed this is a common error in php and i tried to search the net and i can not get my answer, I am trying to create a simpe forum and I am getting these errors

Notice: Undefined variable: topics in C:\xampp\htdocs\mysite\Students forum\view_category.php on line 83

Notice: Undefined variable: topic in C:\xampp\htdocs\mysite\Students forum\view_category.php on line 86

and this is the code on line 83

$topics .= "<table width='100%' style='border-collapse: collapse;'>";

and this is the code on line 86

$topic .= "<tr><td colspan='4'><hr /></td></tr>";

and this is my code

if (mysql_num_rows($res2) > 0) {
        // Appending table data to the $topics variable for output on the page
        $topics .= "<table width='100%' style='border-collapse: collapse;'>";
        $topics .= "<tr><td colspan='4'><a href='index.php'>Return To Forum Index</a>".$logged."<hr /></td></tr>";
        $topics .= "<tr style='background-color: #dddddd;'><td>Topic Title</td><td width='65' align='center'>Last User</td><td width='65' align='center'>Replies</td><td width='65' align='center'>Views</td></tr>";
        $topic .= "<tr><td colspan='4'><hr /></td></tr>";
        // Fetching topic data from the database
        while ($row = mysql_fetch_assoc($res2)) {
            // Assign local variables from the database data
            $tid = $row['id'];
            $title = $row['topic_title'];
            $views = $row['topic_views'];
            $date = $row['topic_date'];
            $creator = $row['topic_creator'];
            // Check to see if the topic has every been replied to
            if ($row['topic_last_user'] == "") { $last_user = "N/A"; } else { $last_user = getusername($row['topic_last_user']); }
            // Append the actual topic data to the $topics variable
            $topics .= "<tr><td><a href='view_topic.php?cid=".$cid."&tid=".$tid."'>".$title."</a><br /><span class='post_info'>Posted by: ".getusername($creator)." on ".convertdate($date)."</span></td><td align='center'>".$last_user."</td><td align='center'>".topic_replies($cid, $tid)."</td><td align='center'>".$views."</td></tr>";
            $topics .= "<tr><td colspan='4'><hr /></td></tr>";
        }
        $topics .= "</table>";
        // Displaying the $topics variable on the page
        echo $topics;
    } 
4

4 回答 4

4

The operator .= is typically used for concatenation of the right and left argument, but in this case, you do not have a left argument. You must instantiate the variable like so:

$topics = "<table width='100%' style='border-collapse: collapse;'>";
$topic = "<tr><td colspan='4'><hr /></td></tr>";

if( ... ) {
    while( ... ) {
        $topics .= ...
        $topic .= ...
    }
}
于 2012-12-03T23:09:29.823 回答
1

Declare the variable first before you use $topics .="anything"

$topics =''; 
$topics .= "whatever you want"; // must have been declared before

So in this case, it can be like

if (mysql_num_rows($res2) > 0) {
    $topics="<table width='100%' style='border-collapse: collapse;'>"
    $topics .= "<tr><td colspan='4'><hr /></td></tr>";
    // rest of your code
}
于 2012-12-03T23:10:36.880 回答
1

This $topics .= is a shorthand for $topics = $topics . and if you use it the first time you use a variable, the variable is not considered 'defined'.

So you should at least do:

$topics = '';
$topic = '';

Before you start using the shorthand.

于 2012-12-03T23:11:12.770 回答
0
$topic .= 'something';

Is the same as

$topic = $topic . 'something';

It effectively means:

  1. take $topic,
  2. join 'something' behind it
  3. put the result back in $topic

If $topic wasn't used anywhere before this line, the first step fails, since the $topic variable doesn't exist yet.

First time you put something in $topic, use simple

=

instead, then you can join more stuff to it via

.=
于 2012-12-03T23:27:38.487 回答