需要修复的变量

时间:2015-09-07 23:23:27

标签: php html

我试图在一个变量甚至被定义之前回应它,所以字面上我得到这个错误“UNDEFINED VARIABLE:tpp”

<tr>
    <td colspan='6'>
        <h4><small>Trusted Players List <?php echo $tpp ?> Total)</small></h4>
    </td>
</tr>

当我将该行与echo“”一起设置时,它可以工作。但我希望它在我设置它的地方[顶部]

以下是我需要帮助的代码

<table class="table table-bordered">
    <thead>
        <tr>
            <td colspan='6'>
                <h4><small>Trusted Players List <?php echo $tpp ?> Total)</small></h4>
            </td>
        </tr>
        <td>Number</td>
        <td>Username</td>
        <td>Last Login</td>
    </thead>
    <?php
        $query = $koneksi->prepare("SELECT `user`, `TP`, `LastOnlineDate` FROM `playerdata` WHERE `banned`=0 AND `TP`=1");
        $query->execute();
            if($query->rowCount() == 0)
        {
            echo "<tr><td colspan='6'><small>No rows found</small></td></tr>";
        }
        $tpp = 0;
        while($data = $query->fetch())
        {
            $tpp++;
            echo "<tr><td>".$tpp."</td>";
            echo "<td>".$data['user']."</td>";
            echo "<td>".$data['LastOnlineDate']."</td></tr>";
        }
        ?>
</table>

任何帮助都将受到如此赞赏

提前感谢。

1 个答案:

答案 0 :(得分:2)

$tpp未定义,因为它在创建之前被调用,请尝试此操作。

 <?php

  $tpp = 0;
  $content = "";

  $query = $koneksi->prepare("SELECT `user`, `TP`, `LastOnlineDate` FROM `playerdata` WHERE `banned`=0 AND `TP`=1");
  $query->execute();
  while($data = $query->fetch())
  {
     $tpp++;
     $content .= "<tr><td>".$tpp."</td>";
     $content .= "<td>".$data['user']."</td>";
     $content .= "<td>".$data['LastOnlineDate']."</td></tr>";
  }

?>

<table class="table table-bordered">
  <thead>
    <tr>
      <td colspan='6'><h4><small>Trusted Players List <?php echo $tpp ?> Total)</small></h4></td>
    </tr>
    <td>Number</td>
    <td>Username</td>
    <td>Last Login</td>
  </thead>

 <?php

        if($query->rowCount() == 0)
        {
            echo "<tr><td colspan='6'><small>No rows found</small></td></tr>";
        }


        echo $content;

        ?>
</table>

我决定在顶部运行循环,将所有数据放在$content内,然后将echo $content放在要显示内容的位置。这使您仍然可以运行计数器并显示在表格的顶部。