Forum Moderators: coopster

Message Too Old, No Replies

Adding Values to an array through a loop

         

Digital Worker

1:40 pm on Feb 26, 2007 (gmt 0)

10+ Year Member



Hi,

I'm trying to achieve the following:

days1 = array(2=>array('/weblog/archive/2004/Jan/02','linked-day'), 3=>array('/weblog/archive/2004/Jan/03','linked-day'), 8=>array('/weblog/archive/2004/Jan/08','linked-day'), 22=>array('/weblog/archive/2004/Jan/22','linked-day'), 26=>array('/weblog/archive/2004/Jan/26','linked-day'),);

Now what I am trying to do is do this dynamically via SQL query. The trouble is though, is that when I do a print_r() it only stores the last value of the SQL query. And so its not appending to the array when it loops through the SQL query.

CODE:

$result=mysql_query("SELECT distinct DAY(story_date) as Day
FROM tbl_news
WHERE tbl_news.ncid=10
and YEAR(story_date)='2007'");

while (list($DBDay)=mysql_fetch_row($result))
{
echo $DBDay.'<br>';
$days=array($DBDay=>array("/weblog/archive/2004/Jan/$DBDay",'linked-day'));
}
print_r($days);

OUTPUT:

11
12
14
18
23
24
25
29
1
15
16
Array ( [16] => Array ( [0] => /weblog/archive/2004/Jan/16 [1] => linked-day ) )

I did try to do the following:

$days=array($DBDay=>array("/weblog/archive/2004/Jan/$DBDay",'linked-day'));

But no joy!

Can anyone shed some light on this?

Thanks

DW.

Birdman

1:52 pm on Feb 26, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try using array_push() [php.net]. What you are doing currently is overwriting the array on each loop.

Another way is to use this construct:

$days[]=$DBDay=>array("/weblog/archive/2004/Jan/$DBDay",'linked-day');

If you use the latter, be sure to define the array before the loop starts:

$days = array();

Digital Worker

2:28 pm on Feb 26, 2007 (gmt 0)

10+ Year Member



Hi Birdman,

Thanks for your response:

I tried the following:

$days = array();
while (list($DBDay)=mysql_fetch_row($result))
{
#echo $DBDay.'<br>';
#$days[]=$DBDay=>array("/weblog/archive/2004/Jan/$DBDay",'linked-day');
$days=array($DBDay=>array("/weblog/archive/2004/Jan/$DBDay",'linked-day'));
#array_push($days,$DBday=>array("/weblog/archive/2004/Jan/$DBDay",'linked-day');
}

As you can see I have tried both, but I get errors using both methods:

Parse error: parse error, unexpected T_DOUBLE_ARROW

Any further ideas?

Thanks.

:-)

Birdman

2:42 pm on Feb 26, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Okay, after re-reading your post, I understand better what you want.

If you put the day variable in the brackets it should work for you:

$days[$DBDay]=array("/weblog/archive/2004/Jan/$DBDay",'linked-day');

Actually, I wouldn't mix single and double quotes that way...this is better:

$days[$DBDay]=array('/weblog/archive/2004/Jan/'.$DBDay,'linked-day');

[edited by: Birdman at 2:43 pm (utc) on Feb. 26, 2007]

vincevincevince

2:48 pm on Feb 26, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Instead of:

#$days[]=$DBDay=>array("/weblog/archive/2004/Jan/$DBDay",'linked-day');

Use:

//before the loop
$recordcount=0;
$keys=Array("/weblog/archive/2004/Jan/$DBDay",'linked-day');
//in the loop
$recordcount++;
foreach ($keys as $n=>$k) $days[$recordcount][$k]=$DBDay[$n];

Sometimes when an error keeps happening, find another way to write it. You might have a buggy PHP, you might not understand the command, or you might be trying to use something that's only in newer PHP versions.

Digital Worker

3:28 pm on Feb 26, 2007 (gmt 0)

10+ Year Member



Thanks to both Birdman & vincevincevince for your responses :-)

Birdman that did work!

Didn't think to populate $DBDay in the $days array!

Dig.