Jump to content

[SOLVED] Populate a Calendar page with MySQL data


revraz

Recommended Posts

I'm trying to figure out the best way to populate a Calendar page with data from a database.  I have the code that will generate the Table with Days of the week, but I'm at a loss on how to populate this with data from a database.

 

Do I build the calendar and then for every day that I generate a cell, query the DB or should I load the DB in an Array and compare that to the date of the cell?

When I've had to do this I used an array.  In fact I'll have two arrays.  One contains the days of the month (ie. cellArr[1]) for displaying the actual days on the calendar. As it loops through, my second array's key is the day of the month as a number, and its element is the data I want displayed.  So when I'm looping through the cell array to show the actual cells I just do something like $dataArr[cellArr[$i]] to display the data I want.

 

Does that make sense?

Thats almost exactly how I was thinking I should do it, but the only problem I can forsee is I will have multiple entries per day for different times.  I really tried to think of a way to make the array key the same as the day, but I dont see how with multiple entries like that.  I will probably only have 2 entries per day, but my project may require me to have more.

 

So maybe I should do my query based on per date and create an array for each date?  I am not sure if that is efficient or not, what do you think?

No stick with that line of thought.  Where in the example I gave for the $dataArr array, just make it multi-dimensional.  That way you can foreach through that array and display multiple pieces of data for that day.  For instance:

 

foreach($dataArr[cellArr[$i]] as $data)
{
      echo $data.'<br/>';
}

 

Something like that, do you follow?  Multidimensional array is essentially an array of arrays, but you do not want to have to manage multiple separate arrays.

  • 1 month later...

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.