Page 1 of 1

Filling Multidimensional Array from MySQL

Posted: Thu Nov 03, 2005 3:03 pm
by korngold
Thanks for all your help so far! My project is actually progressing. If you've followed my mini-saga, I figured out that if I store my data in arrays, I can access it while the table is being created. I just don't know how to use a multidimensional array very well, but I'm thinking that is what I need for this project.

What I have is data in a database of this nature:

tblEvents:
event_title:text
start_date:date/time
end_date:date/time
confirmed:integer




What I need to create is an array that will store the information this way

Code: Select all

title           value (julian day count)         confirmed
event1      2454980                                -1


The value field is calculated using a for loop on the start date & end date and populating all Julian date counts in-between. My question is, how do I store these three fields in a multidimensional array, and how do I recall them later? I've found numerous sites with info on how to insert into a database (if user keys data in), but can't seem to find how to assign the array values through PHP as it goes through the recordset.

Any help is greatly appreciated!

Posted: Thu Nov 03, 2005 6:44 pm
by timvw

Code: Select all

$events = array();

..

while ($row = mysql_fetch_assoc($rs))
{
       // make new event
       $event = array();
       $event['title'] = $row['title'];
       $event['julian'] =  calculate_something_from($row['begin']);
       ...

       // add event to events
       $events[] = $event;
}

print_r($events);

Posted: Fri Nov 04, 2005 6:27 am
by korngold
TimVW,

JUST what I needed! Thank you very much! I think I actually understand how multi-d arrays work now, too. 8O


One more quick array question--is it possible to empty an array? I'm filling one "column" of my multi-d array with another array, which needs to be reset on each iteration of the main loop.

Posted: Fri Nov 04, 2005 7:06 am
by Chris Corbyn
korngold wrote:One more quick array question--is it possible to empty an array? I'm filling one "column" of my multi-d array with another array, which needs to be reset on each iteration of the main loop.
Among a handful of ways....

Code: Select all

$array = array (
    'foo',
    'bar',
    'bob'
);

print_r($array);

$array = array();

print_r($array);