Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php如何自动递增字符?_Php_Mysql - Fatal编程技术网

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>";
        }
    }        
}