From: Paul Halliday on
I have a query that may not always return a result for a value, I need
to reflect this with a "0". I am trying to overcome this by doing this
(the keys are ID's):

while ($row = mysql_fetch_row($statusQuery)) {

$cat = array(0=>0,1=>0,11=>0,12=>0,13=>0,14=>0,15=>0,16=>0,17=>0,19=>0);

switch ($row[1]) {
case 0: $cat[0] = $row[0]; break;
case 1: $cat[1] = $row[0]; break;
case 11: $cat[11] = $row[0]; break;
case 12: $cat[12] = $row[0]; break;
case 13: $cat[13] = $row[0]; break;
case 14: $cat[14] = $row[0]; break;
case 15: $cat[15] = $row[0]; break;
case 16: $cat[16] = $row[0]; break;
case 17: $cat[17] = $row[0]; break;
case 19: $cat[19] = $row[0]; break;
}

print_r($cat);
}

Which gives me this:

Array ( [0] => 15547 [1] => 0 [11] => 0 [12] => 0 [13] => 0 [14] => 0
[15] => 0 [16] => 0 [17] => 0 [19] => 0 )
Array ( [0] => 0 [1] => 0 [11] => 0 [12] => 0 [13] => 0 [14] => 0 [15]
=> 30 [16] => 0 [17] => 0 [19] => 0 )

The query only return 2 rows:

15 | 30
0 | 15547

What am I doing wrong? Is there a more elegant way to achieve what I want?

Thanks.

--
Paul Halliday
Ideation | Individualization | Learner | Achiever | Analytical
http://www.pintumbler.org
From: Joshua Kehn on

On Jul 30, 2010, at 2:36 PM, Paul Halliday wrote:

> I have a query that may not always return a result for a value, I need
> to reflect this with a "0". I am trying to overcome this by doing this
> (the keys are ID's):
>
> while ($row = mysql_fetch_row($statusQuery)) {
>
> $cat = array(0=>0,1=>0,11=>0,12=>0,13=>0,14=>0,15=>0,16=>0,17=>0,19=>0);
>
> switch ($row[1]) {
> case 0: $cat[0] = $row[0]; break;
> case 1: $cat[1] = $row[0]; break;
> case 11: $cat[11] = $row[0]; break;
> case 12: $cat[12] = $row[0]; break;
> case 13: $cat[13] = $row[0]; break;
> case 14: $cat[14] = $row[0]; break;
> case 15: $cat[15] = $row[0]; break;
> case 16: $cat[16] = $row[0]; break;
> case 17: $cat[17] = $row[0]; break;
> case 19: $cat[19] = $row[0]; break;
> }
>
> print_r($cat);
> }
>
> Which gives me this:
>
> Array ( [0] => 15547 [1] => 0 [11] => 0 [12] => 0 [13] => 0 [14] => 0
> [15] => 0 [16] => 0 [17] => 0 [19] => 0 )
> Array ( [0] => 0 [1] => 0 [11] => 0 [12] => 0 [13] => 0 [14] => 0 [15]s
> => 30 [16] => 0 [17] => 0 [19] => 0 )
>
> The query only return 2 rows:
>
> 15 | 30
> 0 | 15547
>
> What am I doing wrong? Is there a more elegant way to achieve what I want?
>
> Thanks.
>
> --
> Paul Halliday
> Ideation | Individualization | Learner | Achiever | Analytical
> http://www.pintumbler.org
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>

Paul-

Why not say:

$cat = array();
if(isset($row[1])
{
$cat[$row[1]] = $row[0];
}

print_r($cat);

Regards,

-Josh
From: Paul Halliday on
On Fri, Jul 30, 2010 at 3:44 PM, Joshua Kehn <josh.kehn(a)gmail.com> wrote:
>
> On Jul 30, 2010, at 2:36 PM, Paul Halliday wrote:
>
>> I have a query that may not always return a result for a value, I need
>> to reflect this with a "0". I am trying to overcome this by doing this
>> (the keys are ID's):
>>
>> while ($row = mysql_fetch_row($statusQuery)) {
>>
>>        $cat = array(0=>0,1=>0,11=>0,12=>0,13=>0,14=>0,15=>0,16=>0,17=>0,19=>0);
>>
>>        switch ($row[1]) {
>>            case 0: $cat[0] = $row[0]; break;
>>            case 1: $cat[1] = $row[0]; break;
>>            case 11: $cat[11] = $row[0]; break;
>>            case 12: $cat[12] = $row[0]; break;
>>            case 13: $cat[13] = $row[0]; break;
>>            case 14: $cat[14] = $row[0]; break;
>>            case 15: $cat[15] = $row[0]; break;
>>            case 16: $cat[16] = $row[0]; break;
>>            case 17: $cat[17] = $row[0]; break;
>>            case 19: $cat[19] = $row[0]; break;
>>        }
>>
>>        print_r($cat);
>>    }
>>
>> Which gives me this:
>>
>> Array ( [0] => 15547 [1] => 0 [11] => 0 [12] => 0 [13] => 0 [14] => 0
>> [15] => 0 [16] => 0 [17] => 0 [19] => 0 )
>> Array ( [0] => 0 [1] => 0 [11] => 0 [12] => 0 [13] => 0 [14] => 0 [15]s
>> => 30 [16] => 0 [17] => 0 [19] => 0 )
>>
>> The query only return 2 rows:
>>
>> 15 | 30
>> 0 | 15547
>>
>> What am I doing wrong? Is there a more elegant way to achieve what I want?
>>
>> Thanks.
>>
>> --
>> Paul Halliday
>> Ideation | Individualization | Learner | Achiever | Analytical
>> http://www.pintumbler.org
>>
>> --
>> PHP General Mailing List (http://www.php.net/)
>> To unsubscribe, visit: http://www.php.net/unsub.php
>>
>
> Paul-
>
> Why not say:
>
> $cat = array();
> if(isset($row[1])
> {
>    $cat[$row[1]] = $row[0];
> }
>
> print_r($cat);
>
> Regards,
>
> -Josh

I need the results that don't have values assigned though.

ex:

c = 0
h = 9
t = 0
f = 21

Even if the query returned only:

h = 9
f = 21
From: Joshua Kehn on

On Jul 30, 2010, at 3:03 PM, Paul Halliday wrote:

>>
>> Paul-
>>
>> Why are those values not defaulted to 0 in the database?
>>
>> Regards,
>>
>> -Josh
>>
>>
>
> They are defaulted, the query is grouping:
>
> select count(status) as count, status from table group by status order
> by status desc;

Paul-

Correct, so stuff with a status of 0 will still show up unless I'm missing something.

Regards,

-Josh