2012-12-03 41 views
0

我注意到这是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; 
    } 
+0

除了下面的许多正确的反应加入更多的东西,你可能要考虑突破PHP来写你的HTML。你有这样的方式将是一个糟糕的时间来维护。 –

回答

4

运算符.=通常用于连接右侧和左侧参数,但在这种情况下,您没有左侧参数。你必须初始化变量,像这样:

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

if(...) { 
    while(...) { 
     $topics .= ... 
     $topic .= ... 
    } 
} 
+0

+1的答案谢谢 – theuserkaps

1

声明变量,你在这种情况下使用$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 
} 
+1

+1的答案谢谢 – theuserkaps

+0

@theuserkaps,你的欢迎:-) –

1

$topics .=是速记为$topics = $topics .,如果您在第一次使用变量时使用该变量,则该变量不被视为“已定义”。

所以你至少应该做到:

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

开始之前使用简写。

+0

+1的答案谢谢 – theuserkaps

0
$topic .= 'something'; 

相同

$topic = $topic . 'something'; 

这实际上意味着:

  1. 取$话题,
  2. 加入 '东西' 背后
  3. 把结果回$话题

如果在此行之前没有使用$ topic,则第一步失败,因为$ topic变量尚不存在。

第一次你把$主题的东西,使用简单的

= 

代替,那么你可以通过

.=