Jump to content

how do I dynamically populate a nested array with DB results


dazzathedrummer

Recommended Posts

Hi,

 

I found a really useful calendar script here:  http://keithdevens.com/software/php_calendar

I'd like to use it as a summary of dates booked in a database list, i've been trying for hours to populate the following nested array dynamically but im not getting anywhere.

 

this is the array:

<?php 
    $days = 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'),  
    ); 
    echo generate_calendar(2004, 1, $days, 3, '/weblog/archive/2004/Jan'); 
?>

 

the number is the day number, the first key in the nested array is a hyperlink and the second is a CSS class.

 

for the time being i'll keep the link and the css class the same - so they will always have the same value.

 

so far i've created a connection to my server and created a while loop that does $days = $row['db_date'] and I can echo out the day numbers that I'm looking for - I just cannot figure out how to add the nested elements???

 

any advice would be greatly appreciated.

 

This is how I would do this.

your database loop goes here 
{
$link="/weblog/archive/".date("Y", strtotime($yourdatefield)."/".date("M",strtotime($yourdatefield))."/".sprintf("%02",date("d", strtotime($yourdatefield))); 
$class="linked-day"; 
$days[date("d", strtotime($yourdatefield))]['link']=$link; 
$days[date("d", strtotime($yourdatefield))]['cssclass']=$class; 

// OR if you don't need the keynames 

$days[date("d", strtotime($yourdatefield)))]=array($link,$class); 
}

excellent!!!

 

I'm almost there, I can get the desired result but only for one date in the list (the last date), I dont think the while loop is looping properly.

$link="/weblog/archive/";
$class="linked-day"; 
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{ 
$days = (array($row['gl_date']=>array($link,$class)));
}

 

If I echo out the array using print_r all the dates are there:

$link="/weblog/archive/";
$class="linked-day"; 
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{ 
$days = print_r(array($row['gl_date']=>array($link,$class)));
}

 

any ideas?

 

 

you're overwriting the array each time through your loop so it's only giving you the last one.

try this

$link="/weblog/archive/";
$class="linked-day"; 
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{ 
$days[$row['gl_date']] = array($link,$class)));
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.