2

For some reason my $count displays the number 1 first instead of 0 I really don't know what's going on I want count to start at 0 and display 0 first then 1, 2 and so on can someone help me correct this problem?

PHP code.

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);
0

4 Answers 4

3

The first call to make_list should be:

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

rather than

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

You are trying to give default value to $count only without giving default value to $depth. That is not possible.

Your call make_list($link[0], $depth+1); is actually assigning $depth+1 to $count and using the default value of 0 for $depth

Sign up to request clarification or add additional context in comments.

2 Comments

This is on the last line of the code in case anyone is wondering. Scroll down :)
right but if he wants to manage arguments manually then needs to use func_get_args in the function to get list of all arguments.
1

try using null or 0 as second parameter and do check out func_get_args() http://php.net/manual/en/function.func-get-args.php. you wont need to set function parameters in this case. just call this function to get all the arguments you passed and manipulate it manually.

Comments

0

You're adding 1 to depth when calling the function. This means that inside the make_list function depth with only be 0 if called with a value of -1.

Comments

0

Your $count is nowhere displayed. Do you mean then 1. 2. ... from the <ol> ? As far as I can see it always starts with 1.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.