hurt
hurt

Reputation: 77

PHP count problem?

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

Upvotes: 2

Views: 244

Answers (4)

Ayaz Alavi
Ayaz Alavi

Reputation: 4835

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.

Upvotes: 1

codaddict
codaddict

Reputation: 454960

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

Upvotes: 3

andrem
andrem

Reputation: 421

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.

Upvotes: 0

Galen
Galen

Reputation: 30170

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.

Upvotes: 0

Related Questions