Jump to content

[SOLVED] Populate a Calendar page with MySQL data


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...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.