php - 将查询结果显示在表格中

标签 php mysql

我有一个 MySQL 查询,有超过 50 个返回结果。现在我需要在 3 行 3 列的表格中显示结果。

类似这样的:

<table>
    <tr>
        <td>Content</td>                    
        <td>Content</td>                    
        <td>Content</td>                                        
    </tr>   
    <tr>
        <td>Content</td>                    
        <td>Content</td>                    
        <td>Content</td>                                        
    </tr>   
    <tr>
        <td>Content</td>                    
        <td>Content</td>                    
        <td>Content</td>                                        
    </tr>
</table>

我用 PHP 尝试过如下:

$q = "SELECT name, address, content FROM mytable"; 
$r = mysqli_query($dbc, $q); 

while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
    $name   = $row['name'];
    $address = $row['address'];
    $content = $row['content'];

    //Create new output
    $output  = "<p>$name</p>";
    $output .= "<p>$address</p>";
    $output .= "<p>$content</p>";

    //Add output to array
    $mycontent[] = $output;     
}

然后我在表中打印内容数组,如下所示:

<tr>
    <td><?php echo $mycontent[0]; ?></td>                   
    <td><?php echo $mycontent[1]; ?></td>                   
    <td><?php echo $mycontent[2]; ?></td>                   
</tr>   
<tr>
    <td><?php echo $mycontent[3]; ?></td>                   
    <td><?php echo $mycontent[4]; ?></td>                   
    <td><?php echo $mycontent[5]; ?></td>                                       
</tr>   
<tr>
    <td><?php echo $mycontent[6]; ?></td>                   
    <td><?php echo $mycontent[7]; ?></td>                   
    <td><?php echo $mycontent[8]; ?></td>                                           
</tr>

使用这段代码,我只能显示9个内容。我的问题是我想显示更多内容。我将使用pagination显示内容;例如 0-9、10-18、19-27 等。

NOTE: I can do the pagination part.

我希望有人能给我正确的方向。

谢谢。

最佳答案

尝试这样的事情:

 echo "<table>";
 while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
 $name   = $row['name'];
 $address = $row['address'];
 $content = $row['content'];
 echo "<tr><td>".$name."</td><td>".$address."</td><td>".$content."</td></tr>";
 }
 echo "</table>";

此示例将在表中打印所有查询结果。 如果你只想限制某些结果,那么限制sql查询。 例如:

 $q = "SELECT name, address, content FROM mytable limit 50"; 

要获取 TD 中的每个内容、名称、地址以及 TR 中的 3 个 mycontent(内容、名称、地址),请尝试以下操作:

$c= mysql_query("select COUNT(name) from mytable");
$n=mysql_fetch_array($c);
$maxname= $n[0];
$i=0;
$tr=0;
echo "<table>";
while ($tr < $maxname)
{ 
echo "<tr>";
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC) and $i<$tr) {
$name   = $row['name'];
$address = $row['address'];
$content = $row['content'];
echo "<td>".$name." | ".$address." | ".$content."</td>";
$i++;
}
echo "</tr>";
$tr= $tr+3;
}
echo "</table>";

关于php - 将查询结果显示在表格中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15450327/

相关文章:

php - 如何使用 Ajax 和 PHP 从动态文本框中插入数据

php - 如果有假期,则计算结束日期,将日期向前推进

php - 即使启用了 GD 支持,也无法在本地主机上显示图形

php - 如何使用php从mysql访问图像到html表

mysql - 尝试从我即时求和的列中获取累积总和

mysql - 聚合现有查询以处理多行

php - 是否应该在服务器端使用 XML,而在客户端使用 JSON?

php - Codeigniter 在 View 问题中显示私有(private)消息?

PHP 时区数据库损坏错误

php - 将 LIMIT 转换为 TOP