Forum Moderators: coopster
Here is my complete solution.
First I created a function that checks the DB to make sure there is an entry for every day M-F and none for Sat. or Sun.
<?php
function checkmysql($query, $month, $year){
$result = mysql_query($query);
if (!$result) {
return 0; //database error
}
$pdate=0; //previouse day
$pday=0; //the day of the last day of the previous month
$g=1; //sets value for new week catch to make sure friday's data wasn't missing
while ($row = mysql_fetch_array($result)) {
$dbday=$row["day_string"]; //day of the week
$dbdate=$row["date_string"]; //numerical date
if($dbday==0 OR $dbday==6){ //if any entries are for a saterday or sunday
return 1; //contains invalid dates
} $firstdayofmonth=date(w,mktime(0,0,0,$month,1,$year));
if($dbdate!=1 AND ($firstdayofmonth!=0 OR $firstdayofmonth!=6) AND $pdate=0){
return 2.1; //missing data
}
if($dbdate==1){
$pday=$dbday-1;
}elseif($pdate==0){
$pdate=$dbdate-1;
$pday=$dbday-1;
}
if($pday==5){
$pday=0;
$pdate=$pdate+2;
}
if($dbdate!=$pdate+1){
return 2.2;
}
++$pday;
++$pdate;
}
return $result;
}
Then I print it out opening and closing the rows on every monday and friday.
It works for May but I haven't checked it for any of the other months yet.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>
<body>
<?php
include 'db.inc.php';
if(isset($_GET['month'])):
$month=$_GET['month'];
$year=$_GET['year'];
$weekday=date(w,mktime(0,0,0,$month,1,$year)); //day of the week the first falls on
$fromwhere = $_GET['fromwhere'];
$lastday=32;
while(!checkdate($month,$lastday,$year)){
$lastday=$lastday-1;
}
$x = date(ymd,mktime(0,0,0,$month,01,$year));
$z = date(ymd, mktime(0,0,0,$month,$lastday,$year));
$query="SELECT day, DATE_FORMAT(day, '%w') as day_string, DATE_FORMAT(day, '%e') as date_string, maincourse, veggie, veggie2, fruit, bread, dessert, drink FROM $fromwhere WHERE day>='$x' AND day<='$z' ORDER BY day";
$result = checkmysql($query, $month, $year);
switch($result){
case'0':
exit('<p>Error performing query: ' . mysql_error() . '</p>');
break;
case'1':
exit ('contains invalid data');
break;
case '2':
exit('missing data');
break;
}
?>
<Table border='2px' width='100%'> <tr><th>Monday</th><th>Tuesday</th><th>Wensday</th> <th>Thursday</th> <th>Friday</th></tr>
<?php
$pdate=0; //previouse day
$pday=0; //the day of the last day of the previous month
mysql_data_seek($result, 0);
$g=$weekday; //day first falls on
$h=1;
if($g!=6){
while($g>$h){
echo "<td></td>";
++$h;
} }
while ($row = mysql_fetch_array($result)) {
$dbday=$row["day_string"]; //day of the week
$dbdate=$row["date_string"]; //numerical date
if($dbday == 1){ //if monday opens new row
echo '<tr>';
}
echo '<td>'; //starts the cell
echo $dbdate . '<br />'; //prints # date
for ($i=3;$i<11;++$i){ //prints the menu while filtering out empty entries
if($row[$i]!= "")
echo $row[$i] . '<br />';
}
echo '</td>'; //closes cell
if($dbday==5){ //checks to see if it is friday.. if so closes row
echo '</tr>';
}
}
?> </tr>
</table>
<?php else: ?>
<form name= "make calander" action="<?php echo $_SERVER['PHP_SELF'];?>" method="GET">
Select Menu For:<p> Month
<select name="month">
<option value="1">January</option>
<option value="2">February</option>
<option value="3">March</option>
<option value="4">April </option>
<option value="5" selected>May</option>
<option value="6" >June</option>
<option value="7">July</option>
<option value="8">August</option>
<option value="9">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select> Year
<select name="year">
<option value="2005" selected>2005</option>
<option value="2006">2006</option>
</select> Place:
<select name="fromwhere">
<option value="meal_menus_d">Decatur County</option>
<option value="meal_menus_bbjj" selected>Bartholomew, Brown, Jackson, Jennings Counties</option>
</select>
</p>
<input type ="submit" value="SUBMIT"/>
</form>
<?php endif;?>
</body>
</html>