多个'Echos'出现在桌子上方

时间:2012-08-28 13:15:56

标签: php mysql html

在我的程序中,我创建了一个可滚动的表,一切正常,只要显示滚动条,返回正确的数据等等。

有什么奇怪的,我无法找到原因,在表格的标题上方,“echo”这个词重复了7次,每个记录都返回一次。我的代码中显然存在一些缺陷,但我无法找到它,感谢任何帮助

    <?php
    echo "<div class='scrollableContainer'>";
    echo "<div style=' height: 125px; width: 535px; font-size: 10px; overflow: auto;'>";
    echo "<table class='referrals scrollable'>";
    echo "<thead><tr> 
            <th class='date'>Date</th>
            <th class='name'>Student Name</th>
            <th class='email'>Email</th>
            <th class='phone'>Phone</th>
            <th class='status'>Status</th>
    echo </tr></thead>";
            $query2 = "select * from students where referred_by ='$referral_id_to_use'";
            $result2 = mysql_query($query2);

            $num_rows = mysql_num_rows($result2);
            if($num_rows>0){
                 while  ($row = mysql_fetch_array($result2))
                  {
                       extract($row);
                         $student_name=$first_name.' '.$last_name;      
                 if($current_status=1)
                     {
                        $student_status="Active";
                     }else{
                        $student_status="Inactive";
                     }
        echo "<tr>\n
              <td class='date'><div>$date_joined</div></td>
              <td class='name'><div>$student_name</div></td>
              <td class='email'><div>$emailaddress</div></td>
              <td class='phone'><div>$phone_number</div></td>   
              <td class='status'><div>$student_status</div></td>
        echo </tr>";
            }
        }       
    echo "</table>";
    echo "</div>";
    echo "</div>";
    ?>

同样,额外的回声线出现在表头上方。

谢谢

2 个答案:

答案 0 :(得分:4)

您在echo</td>之间使用</tr>作为数据。

由于那里不允许使用文本,浏览器会错误地恢复并将其移动到允许的位置(在表格之前)。

<td class='status'><div>$student_status</div></td> echo </tr>";

从源中删除“echo”一词。

通过检查生成的HTML并通过validator运行生成的HTML,而不是仅仅将PHP与浏览器中的渲染进行比较,可以更轻松地获取此类事物。

答案 1 :(得分:2)

这是:

echo "<thead><tr> 
        <th class='date'>Date</th>
        <th class='name'>Student Name</th>
        <th class='email'>Email</th>
        <th class='phone'>Phone</th>
        <th class='status'>Status</th>
echo </tr></thead>";

最后一行。在@ Quentin的回答中也可以看到身体。