PHP随着记录数量的增加动态增加#行

时间:2012-08-08 16:53:38

标签: php html-table row

我正在尝试从MySQL数据库中获取数据。我希望在数据库中增加数据增加行数,但标题行应该只有一行。像典型的表一样,但这里只需要动态增加#row。

    <?php 
    mysql_connect ("localhost","root","root123"); 
    mysql_select_db ("iterrordb"); 

    $sql = "select * from record"; 
       $result = mysql_query ($sql); 
        $num=mysql_numrows($result);
        while ($row = mysql_fetch_array($result)) 
              { 
              $RecID= $row["RecordID"]; 
              $DispName= $row["Name"]; 
              $date= $row["date"]; 
              $fleet= $row["phone"]; 
              $issue= $row["issue"]; 
              $issueRelatedTo= $row["issuRelatedTo"]; 
              } 
        //HTML CODE is start from here

        echo "<table border=\"1\" bordercolor=\"#0066FF\" style=\"background-color:#CCFFFF\" width=\"50%\" cellpadding=\"1\" cellspacing=\"0\">";
        echo"<tr>";
        echo"<td>Record ID</td>";
        echo"<td>Name</td>";
        echo"<td>Date</td>";
        echo"<td>Phone</td>";
        echo"<td>Issue</td>";
        echo"<td>Issue Related To</td>";
        echo"</tr>";
        echo"<tr>";     
        for ($i=0; $i<6; $i++)  
            {
                echo"<td> $row[$i]</td>";
            }
        echo"</tr>";

        echo"</table>";

?>

2 个答案:

答案 0 :(得分:1)

将代码重写为

<?php 
mysql_connect ("localhost","root","root123"); 
mysql_select_db ("iterrordb"); 

$sql = "select * from record"; 
   $result = mysql_query ($sql); 
    $num=mysql_numrows($result);
    //HTML CODE is start from here

    echo "<table border=\"1\" bordercolor=\"#0066FF\" style=\"background-color:#CCFFFF\" width=\"50%\" cellpadding=\"1\" cellspacing=\"0\">";
    echo"<tr>";
    echo"<td>Record ID</td>";
    echo"<td>Name</td>";
    echo"<td>Date</td>";
    echo"<td>Phone</td>";
    echo"<td>Issue</td>";
    echo"<td>Issue Related To</td>";
    echo"</tr>";

        while ($row = mysql_fetch_array($result)) 
          { 
          $RecID= $row["RecordID"]; 
          $DispName= $row["Name"]; 
          $date= $row["date"]; 
          $fleet= $row["phone"]; 
          $issue= $row["issue"]; 
          $issueRelatedTo= $row["issuRelatedTo"]; 
          echo"<tr>"; 
         for ($i=0; $i<6; $i++)  
        {
            echo"<td> $row[$i]</td>";
        }
        echo"</tr>";
       }
    echo"</table>";

 ?>

以下代码在当前上下文中似乎不相关,但无论如何我没有将其删除

      $RecID= $row["RecordID"]; 
      $DispName= $row["Name"]; 
      $date= $row["date"]; 
      $fleet= $row["phone"]; 
      $issue= $row["issue"]; 
      $issueRelatedTo= $row["issuRelatedTo"]; 

答案 1 :(得分:1)

我不确定你的意思。但是如果你想在查询结果中包含行数,那么这就可以了:

$num_rows = mysql_num_rows($result);

如果你想知道“while”循环中当前行号是什么,只需添加一个迭代器。

如果要动态更新html表格。使用jQuery:

$('#my_table').empty().load("/examples/getTableRows.php");

您可以像每5秒钟一样调用此函数来更新表格。