我注意到这是php中的一个常见错误,我试图在网上搜索,但找不到答案,我正在尝试创建一个simpe论坛,我得到了这些错误
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这是第83行的代码
$topics .= "<table width='100%' style='border-collapse: collapse;'>";这是第86行的代码
$topic .= "<tr><td colspan='4'><hr /></td></tr>";这是我的代码
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;
} 发布于 2012-12-04 07:09:30
运算符.=通常用于连接右参数和左参数,但在本例中,您没有左参数。必须按如下方式实例化该变量:
$topics = "<table width='100%' style='border-collapse: collapse;'>";
$topic = "<tr><td colspan='4'><hr /></td></tr>";
if( ... ) {
while( ... ) {
$topics .= ...
$topic .= ...
}
}发布于 2012-12-04 07:10:37
在使用$topics .="anything"之前首先声明变量
$topics ='';
$topics .= "whatever you want"; // must have been declared before所以在这种情况下,它可以像这样
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-04 07:11:13
此$topics .=是$topics = $topics .的简写,如果您第一次使用变量时使用它,则该变量不会被视为“已定义”。
所以你至少应该这样做:
$topics = '';
$topic = '';在你开始使用速记之前。
https://stackoverflow.com/questions/13693388
复制相似问题