从数据库中的逗号分隔字段中删除重复项

时间:2019-05-13 16:32:41

标签: php

我正在访问数据库中以逗号分隔的类别字段,这些字段作为重复数据。如何只显示唯一值?

这是我的代码。

<?php

include($_SERVER["DOCUMENT_ROOT"]."/dbconnect.php");

$sql = "SELECT DISTINCT cats FROM videos ORDER BY cats";

$result = mysqli_query($conn, $sql);

echo "<div class='row'>
          <div class='large-12 columns'>";
echo "<a href='index.php'><span class='filter-label info'>All</span></a>";

while($rows=mysqli_fetch_assoc($result)){

$cats = $rows['cats'];

$filter = explode(',', $cats);
foreach($filter as $filter) {

echo "<a href='index.php?cats=".$filter."'><span class='filter-label info'>".$filter."</span></a>";

}

}

echo "</div>
    </div>";
?> 

0 个答案:

没有答案