Filling Multidimensional Array from MySQL

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
korngold
Forum Newbie
Posts: 11
Joined: Wed Nov 02, 2005 11:49 am

Filling Multidimensional Array from MySQL

Post 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!
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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);
korngold
Forum Newbie
Posts: 11
Joined: Wed Nov 02, 2005 11:49 am

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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);
Post Reply