php mysql数据库检索一行三条记录

标签 php mysql

我正在使用 php 和 mysql 检索数据库记录

while($row = mysqli_fetch_array($query)){
            $id = $row["id"];
            $title = $row["title"];
            $search_output .= "<div class='container'>
    <div class='col-md-12' style='margin: 20px 40px 20px 40px;'>

                <div class='content-heading'>
                    <h2>$title</h2>
                </div>
                <div class='content-tags'>

                    <a href='#'>#song</a>
                </div>
            </div>
        </div>";
                } // close while loop

正如 $query 中所示,我选择了要检索数据的表并将其放入 $search_output 中,以便每个检索到的记录都具有相同的形式,我的问题接近 get data from backend into three columns in one row

我想要一行中的三个记录。但不同之处在于 $search_output 不是一个数组,我只想要 $search_output 代表一条记录, 显示为一行中的三个记录。因为我需要它看起来像 2 行,每行 3 条记录(列),而我只有一个变量,即 $search_output

我试图写这个来展示我想要的东西,但它不起作用

<table>


 <?php


 $r = 0;
 $columns = 3;
 foreach($search_output as $s){
    if($r%$columns == 0) $html .="<tr>";
    $html .= "<td>";
     echo $search_output;
     $html .="</td>";
     if ($r%$columns == $columns || $r++ == count($search_output)-1) $html .= "</tr>";
}



 ?>

</table>

这是屏幕截图:enter image description here

但我写的时候看起来是这样的

<table><tr><td> <?php echo $search_result; ?> </td></tr></table>

有什么想法吗?

谢谢

最佳答案

您使用的 BOOTSTRAP 错误。

容器 DIV 之后,首先缺少 DIV。

如果你想要三列,那么 col-md-12 必须是 col-md-4

12 = 全列,4 = 三列 (12/3 = 4) 查看 Bootstrap 文档以了解更多详细信息。

  <div class='container'>
   <div class='row'>
    <div class='col-md-4'></div>
    <div class='col-md-4'></div>
    <div class='col-md-4'></div>
   </div>
  </div>

$search_output = "<div class='container'><div class='row'>";
while($row = mysqli_fetch_array($query)){
        $id = $row["id"];
        $title = $row["title"];
        $search_output .= "
<div class='col-md-4' style='margin: 20px 40px 20px 40px;'>
            <div class='content-heading'>
                <h2>$title</h2>
            </div>
            <div class='content-tags'>
                <a href='#'>#song</a>
            </div>
        </div>";
            } 
$search_output .= '</div></div>';

但这仅在从数据库获取3行时才有效

关于php mysql数据库检索一行三条记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33035430/

相关文章:

php - PHP 和 Webmatrix 的 500 错误显示 "Friendly"错误页面

javascript - 互联网浏览器 : HTML entities in URL

PHP file_get_contents 很慢并返回 500 Internal Server Error

javascript - 如何使用ajax从数据库中检索特定数据

mysql - 为什么这个查询不使用键(MySQL 5.7 空间)?

mysql - 这个 MySQL 查询有问题吗?

php - 在清理数据时,SQL LIKE 查询是否需要特别注意?

php - 如何使用 preg_replace 将表格宽度更改为 100%

mysql - 使用 Catalyst 使用 DBIC 将文件存储在数据库中

mysql - 比较来自 DateTimeField-Mysql 查询的时间