Forum Moderators: coopster
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.
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();
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.
:-)
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]
#$days[]=$DBDay=>array("/weblog/archive/2004/Jan/$DBDay",'linked-day');
//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.