Page 1 of 1

Dynamically naming an associative array index string

Posted: Wed Aug 20, 2003 5:48 pm
by Swede78
I'm trying to loop through a set of Category ID numbers and count how many there are for each Category ID. The following code doesn't work, I know that there's gotta be a way to do this, maybe I'm approaching this all wrong or maybe it's just a syntax problem.

$CID = mysql_result($result, 0, 'CategoryID');
$ArrayCount[$CID] += $Quantity;

This code is within a loop that pulls the Category ID from another array. Everything works fine up to the point that I try to assign a number ($Quantity) to an array ($ArrayCount) that uses a variable as the index string (in bold).

I get the error: "Undefined variable: ArrayCount in file.php on line xx."

Let me know if this doesn't make sense.

Posted: Wed Aug 20, 2003 7:22 pm
by McGruff
It sounds like you've got E_ALL error reporting - which is good. No, not just good - essential really.

When you use a .= or += etc assignment operator, you have to first initialise the variable to avoid the error message (php doesn't like being told to += to a var which hasn't been declared).

Adding:

$ArrayCount = null;

.. somewhere before the first += line ought to fix it.

PS: you can completely ignore this if you like but I personally always name variables lower case - $array_count for example rather than $ArrayCount. The reason being to avoid confusion with functions named camel-back style: functionName() eg.

Style "rules" such as this are always a matter of personal preference - and I don't follow them all - but if you end up working with other programmers at some point it's best to follow a standard, one example being the PEAR coding style: http://pear.php.net/manual/en/standards.php.

Posted: Thu Aug 21, 2003 10:07 am
by Swede78
Another way of repressing the error message is to add a "@" in front of it. This only works for non-fatal errors.

@$ArrayCount[$CID] += $Quantity;

But, either way, I'm still getting the result that it's not creating that variable correctly. I think it's more to do with putting a variable inside a variable.

Here's a little more to the code, maybe that will help people understand what I'm trying to do.

Code: Select all

foreach($list as $item => $quantity)
        {  
            $query = "SELECT CategoryID FROM table WHERE ItemID = '$item'";
            $result = mysql_query($query) or die(mysql_error());
            $totalRows = mysql_num_rows($result);
            if( $totalRows > 0 )
            {
                $CID = mysql_result($result, 0, 'CategoryID');
                @$array_count[$CID] += $quantity;
            }
        }
        foreach($array_count as $category => $number)
        {
             // etc.
Basically, it should pull a "Category ID" from each item in the $list array. Let's say that it pulls 5 of these (100,200,300,200,400). I want to end up with an array with these values:

$array_count['100'] = 1
$array_count['200'] = 2
$array_count['300'] = 1
$array_count['400'] = 1

I think there's a better way to do this, but I can't find anything in the posts.

Posted: Thu Aug 21, 2003 10:12 am
by greenhorn666

Code: Select all

$array_count = array();
if( $totalRows > 0 ) {
    $CID = mysql_result($result, 0, 'CategoryID');        
    if(isset($array_count[$CID]))
        $array_count[$CID] += $quantity;
    else
        $array_count[$CID] = $quantity;
}
If I got you right, this should solve your problem...
Clean code ;)

Posted: Thu Aug 21, 2003 10:44 am
by Swede78
Thank you, worked like a charm!

Now I see what I was doing wrong. I was thinking that my problem was that I couldn't put a $var inside the [ ] of an associative array like that.

I just had to check if that particular array key was set before assigning a value to it. Thanks again, greenhorn666.