noreason noreason - 5 months ago 24
MySQL Question

PHP MySQL 7 Day , weekly, Mon-Fri Calendar with pagination?

I am trying to figure out the best approach for designing a "7 day calendar" that will consist of an HTML table with the columns "Name, Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday". The HTML table rows will be filled with data from my database, ie. Name will have a persons Name, Sunday will show what that person needs to do on Sunday, ie "Brush teeth", etc etc. It's very similar to event calendars, except what I am looking to accomplish doesn't require the hourly view, just a simple 7 day, Sunday to Saturday view.

My database currently consists of "Name, EventDetails, and EventDate".

My HTML table columns consists of the columns "Name - Sunday - Monday - Tuesday - ..."

My Logic: Each time the page loads, the script will query the database and see if there are any EventDate entries that equal one of the 7 days of the week currently being viewed. If an EventDate matches, it will list itself in a row that matches the corresponding HTML table column of that date. Clicking "Previous Week" or "Next Week" would change to another week and should restart the script, but this time it will be using a different list of days to check against.

Anyone care to share some examples of what they can come up with to accomplish this?

Here is what I came up with so far... The problem with it is that if there are more than one event under a person, it makes a new row for each event whereas I'm working on getting it to list each event in one row.

<table border='1'>

function getCurrentWeek()
$weekArray = array();

// set the current date
$date = date("m/d/Y"); //'03/08/2011';

$ts = strtotime( $date );

// calculate the number of days since Monday
$dow = date('w', $ts);
$offset = $dow - 0;
if ($offset < 0) $offset = 6;

// calculate timestamp for the Monday
$ts = $ts - $offset*86400;

// loop from Monday till Sunday
for ($i=0; $i<7; $i++, $ts+=86400){
$temp_date = date("Y-m-d", $ts); // Reformat the dates to match the database
array_push( $weekArray, $temp_date );



$currentWeek = getCurrentWeek();

// Loop through the data array
while($row = mysql_fetch_array($result)){

$eventDate= $row['EventDate'];

// Loop through the currentWeek array to match a date from the database from a date in the current week
foreach ($currentWeek as $weekDay){

// If there is a matching date...
if ($eventDate == $weekDay) {
echo "<tr><td>".$row['Name']."</td>";

foreach ($currentWeek as $weekDay2){
if ($eventDate == $weekDay2)
echo "<td>".$row['EventName']."</td>";
echo "<td></td>";

echo "</tr>";





To select a week you could do something like

set @weekday:= dayofweek(@adate);  
set @StartOfWeek:= date_sub(@adate,INTERVAL @weekday DAY);  
set @EndOfWeel:= date_add(@adate,INTERVAL (7- @weekday) DAY);  

Then to select the week I'd do

SELECT * FROM TableWithEvents  
WHERE TableWithEvents.EventDate 
      BETWEEN date_sub(@adate,interval @weekday day) 
      AND date_add(@date,INTERVAL (7-@weekday) DAY);

Note that using @adate - @weekday will not work, you must use date_sub/date_add with the silly interval syntax. It does work rather nice when adding months, where it correctly adds the number of days in a month or with years where it knows about leap years (but I digress).

For the pagination you can use the above SELECT with limit start, end; like so:

SELECT * FROM sometable WHERE some_where_thingy LIMIT 0,20;

O and don't forget to add an index to the EventDate field.
And I would recommend adding an autoincrement primary key named id to the table with events.
That way you can uniquely link to that particular event in some other table, like so:

Table FavEvents:   
- id: integer (autoinc primary)  
- Event_id: integer (link to your event)  
- FanName: varchar(x) (name of user you loves event or what ever)

Then you can select "bill"s fav events like so:

SELECT * FROM FavEvents  
INNER JOIN Events ON (FavEvents.Event_id =  
WHERE FavEvents.FanName = "bill"

I never use PHP so can't help you there, good luck.