Jump to content

Still working on event calendar need help retriving events from database


bobby317

Recommended Posts

Ok I am creating a event calendar with php and mysql I have the calendar part working were it will create the table and populate it with the dates.  What I am having trouble with is retrieving the events using the date column in the database as an ID to retrieve the event name and display it under the correct date. My dates that are stored in the database are a php timestamp that I converted into the correct DATE mysql format with the FROM_UNIXTIME() function. I have my code below coded the way I think it should be but it is not displaying the results. My calendar still populates with everything but the query result. I am not sure what I am doing wrong and I know I am horrible at explaining stuff. Take a look at my code and see what you think. Thanks and please explain anything so I may learn. Also getting no errors.

 

<?php

//CSS style
print '<link href="eventMain.css" rel="stylesheet" type="text/css" />';

//This gets today's date  
$date = time ();  

//This puts the day, month, and year in seperate variables  
$day = date('d', $date);  
$month = date('m', $date);  
$year = date('Y', $date);  

//Here we generate the first day of the month  
$first_day = mktime(0,0,0,$month, 1, $year);   

//This gets us the month name  
$title = date('F', $first_day);

//Here we find out what day of the week the first day of the month falls on 
$day_of_week = date('D', $first_day);   

//Once we know what day of the week it falls on, we know how many blank days occure before it. If the first day of the week is a Sunday then it would be zero 
switch($day_of_week){  
case "Sun": $blank = 0; break;  
case "Mon": $blank = 1; break;  
case "Tue": $blank = 2; break;  
case "Wed": $blank = 3; break;  
case "Thu": $blank = 4; break;  
case "Fri": $blank = 5; break;  
case "Sat": $blank = 6; break;  
}  

//We then determine how many days are in the current month 
$days_in_month = cal_days_in_month(0, $month, $year);

//Here we start building the table heads  
echo "<table>"; 
echo "<tr><th colspan=\"7\" class=\"title\"> $title $year </th></tr>"; 
echo '<tr><td class="dayOfWeek">Sunday</td><td class="dayOfWeek">Monday</td><td class="dayOfWeek">Tuesday</td><td class="dayOfWeek">Wendsday</td><td class="dayOfWeek">Thursday</td><td class="dayOfWeek">Friday</td><td class="dayOfWeek">Saturday</td></tr>';  

//This counts the days in the week, up to 7 
$day_count = 1;  echo "<tr>";

//first we take care of those blank days 
while ( $blank > 0 )  {  
echo "<td> </td>";  
$blank = $blank-1;  
$day_count++; 
}

//sets the first day of the month to 1  
$day_num = 1;  

//count up the days, untill we've done all of them in the month 
while ( $day_num <= $days_in_month )  {

//Include files for conecting to database:
$dbc = mysql_connect('rwddesign.com:3306', 'rwddesi1_bobby31', 'jessica');
mysql_select_db('rwddesi1_test');

//Get each day of the month
$retriveDate = "$year $day_num $month ";

//Turns $retriveDate into timestamp
$retriveDateTs = strtotime($retriveDate);

//Query for selecting event names for each date in mounth
$query = "SELECT eventName FROM events WHERE date=FROM_UNIXTIME($retriveDateTs)";

//Start filling table with dates and events
if (date('d') != $day_num) {
	$result = mysql_query($query);
	echo "<td> $day_num<br />$result </td>";  
	$day_num++;  
	$day_count++;

//Style the current date for easy ID
} else {
	$result = mysql_query($query);
	echo "<td class=\"today\"> $day_num<br />$result </td>";
	$day_num++;  
	$day_count++;

}
  

//Make sure we start a new row every week 
if ($day_count > 7) { 
	echo "</tr><tr>"; 
	$day_count = 1; 
} 
}

//Finaly we finish out the table with some blank details if needed 
while ( $day_count >1 && $day_count <=7 )  {  
echo "<td> </td>";  
$day_count++;  
}  

//Close column and table
echo "</tr></table>";

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.