我的 tags
专栏是这样的:
第一行:sky - earth - sea
第二行:iron - silver - gold
第三行:apple - fruit - food
……等等
想要从每个项目创建一个 div,如下所示:
<div class='tagdown'>sky</div>
<div class='tagdown'>earth</div>
$st = $db->query("select tags from posts");
$arr = array();
$items = "";
while ($row = $st->fetch()) {
array_push($arr, explode(' - ', $row['tags']));
}
foreach($arr as $item) {
$items .= "<div class='tagdown'>" . $item . "</div>\n";
}
echo $items;
通知:Array to string conversion...
再试一次:
for ($i = 0; $i < count($arr); ++$i) {
$items .= "<div class='tagdown'>" . $arr[$i] . "</div>\n";
}
echo $items;
通知:Array to string conversion...
有什么帮助吗?
最佳答案
不要推送并再次遍历你的数组。只需在 while 循环中打印出数据。试试下面的代码:
$items = "";
while ($row = $st->fetch()) {
$arr = explode(' - ', $row['tags']);
$items .= "<div class='tagdown'>".implode("</div>\n<div class='tagdown'>",$arr)."</div>\n";
}
echo $items;
关于php - 从标签列中的每个项目创建 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49664528/