Php如何自动递增字符?
这是我的下拉菜单代码。我希望在每个父项中使用“-”字符,并在每个父项中自动递增此字符。 类似于此选择菜单: 我想你需要使用 例如:Php如何自动递增字符?,php,mysql,Php,Mysql,这是我的下拉菜单代码。我希望在每个父项中使用“-”字符,并在每个父项中自动递增此字符。 类似于此选择菜单: 我想你需要使用 例如: <?php try { $stmt2 = $db->query("SELECT cid, parent, name FROM category"); $row_count = $stmt2->rowCount(); if ($row_count) { $rows = $stmt2-
<?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>";
}
}
}
?>
foreach($items作为$item){
如果($item['parent']==0){
回声“;
$id=$item['cid'];
分项($id,1);
回声“;
}
}
回声“;
函数子($items,$id,$counter){
foreach($items作为$item){
如果($item['parent']=$id){
回显“.str_repeat”(“-”,$counter)”;
子项($items,$item['cid',$counter+1);
回声“;
}
}
}
@BilgehanCan那么,现在有什么问题吗?@BilgehanCan请查看编辑,我想现在应该可以了
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>";
}
}
}