2012-08-08 127 views
0

我试图从MySQL数据库中获取数据。我想增加数据库中的数据增加行数,但标题行应该只有一个。像典型的表格,但这里只需要动态增加#row。PHP随着记录数量的增加动态增加#行

<?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>"; 

?> 

感谢您的阅读。

回答

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"]; 
+0

谢谢纳瓦兹。这是有效的。击掌... – 2012-08-08 18:18:37

1

我不知道你是什么意思。但是,如果你想在你的查询结果行的数量,这将这样的伎俩:

$num_rows = mysql_num_rows($result); 
如果你想知道什么是你的“而”循环,只需添加一个迭代器的当前行数

如果你想动态更新你的html表格。使用jQuery:

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

你可以像每5秒钟一样调用这个函数来更新表。

相关问题