gpt4 book ai didi

PHP计数问题?

转载 作者:行者123 更新时间:2023-11-29 01:47:20 25 4
gpt4 key购买 nike

出于某种原因,我的 $count 首先显示数字 1 而不是 0 我真的不知道发生了什么 我希望计数从 0 开始并先显示 0 然后显示 1、2 等等 有人可以帮我纠正这个问题问题?

PHP 代码。

function make_list ($parent = 0, $count = 0, $depth = 0) {
global $link;

if($count == 0){
echo '<ol class="cat-width">';
} else {
echo '<ol class="cat-depth">';
}

foreach ($parent as $id => $cat) {
if($cat['parent_id'] == '0'){
echo '<li><input type="checkbox" name="cat[]" id="cat-' . $cat['id'] . '" value="' . $cat['id'] . '" />
<label for="cat-' . $cat['id'] . '" class="label-headers">' . $cat['category'] . '</label>';
} else {
$indent = str_repeat('&nbsp; ', $depth * 3);
echo '<li>' . $indent . '<input type="checkbox" name="cat[]" id="cat-' . $cat['id'] . '" value="' . $cat['id'] . '" />
<label for="cat-' . $cat['id'] . '">' . $cat['category'] . '</label>';
}

if (isset($link[$id])) {
make_list($link[$id], $count+1, $depth+1);
}
echo '</li>';
}
echo '</ol>';
}

$dbc = mysqli_query($mysqli,"SELECT * FROM categories ORDER BY parent_id, category ASC");
if (!$dbc) {
print mysqli_error();
}

$link = array();

while (list($id, $parent_id, $category, $depth) = mysqli_fetch_array($dbc)) {
$link[$parent_id][$id] = array('parent_id' => $parent_id, 'id' => $id, 'category' => $category, 'depth' => $depth);
}

make_list($link[0], $depth+1);

最佳答案

第一次调用 make_list 应该是:

make_list($link[0],0, $depth+1);

而不是

make_list($link[0], $depth+1);

您试图只为 $count 提供默认值,而没有为 $depth 提供默认值。这是不可能的。

您的调用 make_list($link[0], $depth+1); 实际上是将 $depth+1 分配给 $count并为 $depth

使用默认值 0

关于PHP计数问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3528616/

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