rowCount(); -6ren">
gpt4 book ai didi

Php如何自动增加字符?

转载 作者:行者123 更新时间:2023-11-29 02:45:52 25 4
gpt4 key购买 nike

<?php
try {
$stmt2 = $db->query("SELECT cid, parent, name FROM category");
$row_count = $stmt2->rowCount();
if ($row_count) {
$rows = $stmt2->fetchAll(PDO::FETCH_ASSOC);
}

} catch (PDOException $e) {
echo $e->getMessage();
}

$items = $rows;
$id = '';
echo "<select>";
foreach ($items as $item) {
if ($item['parent'] == 0) {
echo "<option><a href='#'>".$item['name']."</a>";
$id = $item['cid'];
sub($items, $id);
echo "</option>";
}
}
echo "</select>";

function sub($items, $id){
foreach ($items as $item) {
if ($item['parent'] == $id) {
$x = '-';
$x++;
echo "<option>".$x."<a href='#'>".$item['name']."</a>";
sub($items, $item['cid']);
echo "</option>";
}
}
}
?>

这是我的下拉菜单代码。我希望每个父项中都有这个“-”字符,并在每个父项中自动递增此字符。喜欢这个选择菜单:

enter image description here

最佳答案

我认为你需要使用 str_repeat()

例子:

foreach ($items as $item) {
if ($item['parent'] == 0) {
echo "<option><a href='#'>".$item['name']."</a>";
$id = $item['cid'];
sub($items, $id, 1);
echo "</option>";
}
}
echo "</select>";

function sub($items, $id, $counter){
foreach ($items as $item) {
if ($item['parent'] == $id) {
echo "<option>".str_repeat("-",$counter)."<a href='#'>".$item['name']."</a>";
sub($items, $item['cid'],$counter+1);
echo "</option>";
}
}
}

关于Php如何自动增加字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42271993/

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