Hi there,
You're going to have to read up on how to retrieve data from a database.
Also, best start tidying up your code ... on looking through, there are a few missing tags etc and indenting your code really helps to stop this happening.
Here's some tips.
1) Indent your code
2) Try using a bit of CSS - cleans things up nicely
3) Use the $_GET array
Anyway, this was a nice little job to do for myself ... so here's my version.
<?php
if(isset($_GET['prm']) and $_GET['prm'] > 0){
$m = $_GET['prm'] + $_GET['chm'];
} else {
$m = date("m");
}
$d = date("d"); // Finds today's date
$y = date("Y"); // Finds today's year
$no_of_days = date('t',mktime(0,0,0,$m,$d,$y)); // This is to calculate number of days in a month
$mn = date('M',mktime(0,0,0,$m,$d,$y)); // Month is calculated to display at the top of the calendar
$yn = date('Y',mktime(0,0,0,$m,$d,$y)); // Year is calculated to display at the top of the calendar
$j = date('w',mktime(0,0,0,$m,1,$y)); // This will calculate the week day of the first day of the month
$query = "SELECT date FROM events WHERE id = '1'";
$result = mysql_query($query);
// Build an events array
$event_dates = array();
// Cycle through the results like this ...
while($resultset = mysql_fetch_array($result)){
$event_dates[] = $resultset['date'];
}
?>
<html>
<head>
<title></title>
<style type="text/css">
* { font-family: Tahoma; }
TABLE#cal TH, TABLE#calendar TD { border: 1px solid #C0C0C0; }
TABLE#cal TH { width: 50px;}
TABLE#cal TD.title { text-align: center; vertical-align: top; background-color:#FFFF00; }
</style>
</head>
<body>
<table id="cal" align="center" bordercolor="#FFFF00" border="1" cellpadding="0" cellspacing="0">
<tr>
<td class="title">
<a href="php_calendar.php?prm=<?php echo $m; ?>&chm=-1"><</a>
</td>
<td colspan="5" class="title">
<?php echo $mn.' '.$yn; ?>
</td>
<td class="title">
<a href="php_calendar.php?prm=<?php echo $m; ?>&chm=1">></a>
</td>
</tr>
<tr>
<th>Sun</th>
<th>Mon</th>
<th>Tue</th>
<th>Wed</th>
<th>Thu</th>
<th>Fri</th>
<th>Sat</th>
</tr>
<?php
// Get the number of weeks (or rows)
$no_of_weeks = ceil(($j + $no_of_days)/7);
// Cycle through the weeks
for($week_no = 0; $week_no < $no_of_weeks; $week_no++){
echo '<tr>';
// Cycle through the days (Sun=0 ... Sat=6)
for($day_no = 0; $day_no < 7; $day_no++){
// Get the 'day of the month' ... this
// could actually be negative or bigger than
// the number of days in the month, in which
// case echo out an empty cell.
$day = 1 + $week_no*7 + $day_no - $j;
if($day <= 0 || $no_of_days < $day){
echo '<td> </td>';
} else {
$date = $mn.'/'.$day.'/'.$yn;
// Check if this date is an event
$class = in_array($date, $event_dates)? ' bgcolor="#CCCCCC"': '';
echo '<td'.$class.'>'.$day.'<br> </td>';
}
}
echo '<tr>';
}
?>
</table>
</body>
</html>
By the way, I wasn't not sure if you had dealt with the possible empty cells at the end.
Paul 😉