【问题标题】:Display dynamic table data in 3 columns在 3 列中显示动态表数据
【发布时间】:2017-10-05 12:19:15
【问题描述】:

我想在 3 列中显示 mysql 表数据。我已经尝试过这段代码。但这对我没有帮助。我使用了 var_dump 并收到了这个输出。

array(1) { [0]=> string(5) "Gucci" } 
array(1) { [0]=> string(16) "Business Insider" } 
array(1) { [0]=> string(10) "Health.com" }
array(1) { [0]=> string(5) "Prada" } 
array(1) { [0]=> string(6) "Orbitz" } 
array(1) { [0]=> string(4) "Time" }
array(1) { [0]=> string(9) "dotTravel" } 
array(1) { [0]=> string(7) "Expedia" } 

注意:未定义的偏移量:第 75 行 C:\xampp\htdocs\Store_Brand\catscheck.php 中的 8 空

我也使用了 echo 并收到了这个输出:

Notice: Array to string conversion in C:\xampp\htdocs\Store_Brand\catscheck.php on line 75

MYSQL

<?php
$result = mysqli_query($con,"SELECT sm_brand_name FROM store_manufacture");
$data = Array();
while ($row = mysqli_fetch_row($result))
    $data[] = $row;

for ($i = 0; $i < count($data) / 3; $i++){ // Line 75
    echo '<table><tr>';
    for ($j = 0; $j < 3; $j++){
        echo  '<td>' . var_dump ($data[ $i + $j * 3])  . '</td>';
    }
    echo '</tr><tr>';
}
echo '</tr></table>';
?>

【问题讨论】:

  • 如果您向我们展示您向我们展示的 16 行中的第 75 行,这将很有用。
  • @RiggsFolly 更新了我的代码

标签: php html mysql arrays apache


【解决方案1】:

自从我上次看到它以来,你改变了你的帖子。但我是根据你原来的问题写的。很粗糙,但至少你可以看到发生了什么......

<table>

<?php 
$con    = mysqli_connect(DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
$sql    = "SELECT * FROM yourTable";
$result = mysqli_query($con, $sql);

$i = 0;

while ($row = mysqli_fetch_row($result)) {

    if($i == 0) {
        echo '<tr><td>' . $row[1] . '</td>';
        $i++;
    } elseif ($i == 1) {
        echo '<td>' .$row[1] . '</td>';
        $i++;
    } elseif($i == 2) {
        echo '<td>' . $row[1] . '</td></td>';
        $i = 0;
    }

}
?>

</table>

请注意,如果您在查询中使用用户提供的数据,则需要使用准备好的语句,这是与本次不同的主题。

【讨论】:

  • 您忘记关闭导致布局问题的最后一个 tr 标签
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-10
  • 2012-12-08
  • 2020-03-23
  • 1970-01-01
  • 1970-01-01
  • 2016-10-30
相关资源
最近更新 更多