【发布时间】:2017-08-06 20:25:56
【问题描述】:
我正在从 db 中选择一些数据,并尝试将它们显示在 html 表上。 问题是,当我刷新页面时,我的 html 表上的所有数据都重复了。我一直在尝试谷歌/修复此问题,但三天没有结果。
<?php
//select and display all post on a table.
$query="SELECT DISTINCT * FROM posts order by post_id";
$results=mysqli_query($conn, $query);
while($row= mysqli_fetch_assoc($results)){
$data[] = array_unique($row);
echo "<tr>";
foreach($data as $result)
{
foreach($result as $key=>$value)
{
if($key=="post_content"){
continue;
} if($key=="post_img"){
$value="<img width='100px' src='../imgs/$value' class='img-responsive'>";
}
echo "<td>".$value."</td>";
}
echo "</tr>";
}
}
?>
【问题讨论】:
-
您的代码看起来有点连线。为什么要在结果集上创建
array_unique?如果你做出不同的事情,你可以直接遍历你的结果。 -
我使用了array_unique,因为我想确保我不会从数组本身获取那些重复的数据,但即使使用了该函数,当我刷新页面时我仍然会不断得到重复的数据。
标签: php html mysql duplicates html-table