php - 从标签列中的每个项目创建 div

标签 php mysql arrays

我的 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/

相关文章:

mysql - 一台 Windows 服务器上有两个 WordPress 站点(独立的数据库)?

C# 将 JSON 对象转换为数组

php - PHP中如何实现imagecreatefromstring的反向效果?

php - 当我必须默认使用它时,如何通过 Yii2 迁移创建一个新数据库?

php - 调整 Yii2 表单中的文本框大小

java - 在 mysql(或 JPA)中插入多行

PHP比较二维数组

c++ - 类型 'const char*' 的参数与类型 'char*' 的参数不兼容

PHP - 像 WHERE 子句一样的 foreach 循环

php - b"string"在 dd() 函数中是什么意思?