gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 11:31:12 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com