|
|
|
Array help.
|
Previous Topic
Next Topic
|
| Message |
Author |
Posted: Fri Jul 30, 2010 1:38 pm Subject: Array help. |
|
|
Paul Halliday
|
|
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 |
| Back to top |
|
 |
Posted: Fri Jul 30, 2010 1:45 pm Subject: Array help. |
|
|
Joshua Kehn
|
|
On Jul 30, 2010, at 2:36 PM, Paul Halliday wrote:
| Quote: | 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
|
Paul-
Why not say:
$cat = array();
if(isset($row[1])
{
$cat[$row[1]] = $row[0];
}
print_r($cat);
Regards,
-Josh
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php |
| Back to top |
|
 |
Posted: Fri Jul 30, 2010 1:55 pm Subject: Array help. |
|
|
Paul Halliday
|
|
On Fri, Jul 30, 2010 at 3:44 PM, Joshua Kehn <josh.kehn@gmail.com> wrote:
| Quote: |
On Jul 30, 2010, at 2:36 PM, Paul Halliday wrote:
| Quote: | 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
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php |
| Back to top |
|
 |
Posted: Fri Jul 30, 2010 2:07 pm Subject: Array help. |
|
|
Joshua Kehn
|
|
On Jul 30, 2010, at 3:03 PM, Paul Halliday wrote:
| Quote: | | Quote: |
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
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php |
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|