Forum Moderators: coopster
<?php
include 'db.inc.php';
$x ='2005-05-01';
$z ='2005-05-30';
$result = @mysql_query("SELECT id, maincourse, veggie, veggie2, fruit, bread, dessert, drink, DATE_FORMAT(day, '%a %b, %D') as date_string FROM meal_menus_bbjj WHERE day>='$x' AND day<='$z' ORDER BY day");
if (!$result) {
exit('<p>Error performing query: ' . mysql_error() . '</p>');
}
echo date(w,mktime(0,0,0,2,20,2005));
for($i=0;$i<5;++$i){
$row = mysql_fetch_array($result);
$dbdate=$row["date_string"];
echo $dbdate . ' red<br>';
}
echo '<p>';
for($i=0;$i<5;++$i){
$row = mysql_fetch_array($result);
$dbdate=$row["date_string"];
echo $dbdate . ' blue<br>';
}
echo'</p>';
?>
here is the output:
0Mon May, 2nd red
Tue May, 3rd red
Wed May, 4th red
Thu May, 5th red
Fri May, 6th red
Mon May, 9th blue
Tue May, 10th blue
Wed May, 11th blue
Thu May, 12th blue
Mon May, 16th blue
ohh no someting isn't right. There is no DB entry for Friday so that won't work.
So then I though aboult having an "if" check that the next day realy is the next day and if it isn't then insert a blank. but then it would increment past that one so it wouldn't get printed where it is supose to go.
Is there a way to keep the mysql_fetch_array() from incrementing, or step it back?
Another idea I had was to add a function before it starts that checks the db to make sure all mon-fri have entries (there shouldn't be a blank day) then return an error if a day is missing.
Now if I do that will I have to have 2 query or can I reset the mysql_fetch_array()?
Sarah
[b]$week=array("Mon","Tue","Wed","Thu","Fri");[/b] for($i=0;$i<5;$i++){ $row = mysql_fetch_array($result); $dbdate=$row["date_string"]; $tday=substr($dbdate,0,3); if ($tday==$week[$i]) { $showday=1; } else { if ($i<5) { $i++; } if ($tday==$week[$i]) { $showday=1; } else { if ($i<5) { $i++; } if ($tday==$week[$i]) { $showday=1; } else { if ($i<5) { $i++; } if ($tday==$week[$i]) { $showday=1; } else { if ($i<5) { $i++; } if ($tday==$week[$i]) { $showday=1; } } } } } if ($showday) { echo $dbdate . ' red<br>'; } } IMHO it is a great idea to have entries for every day, even if they are just the day/date with no meal for that day rather than leaving gaps. But if you have to have gaps, at least the above should get your weeks separated ... no matter if there are 5 or 1 day in the db for that week.
Is there a way to keep the mysql_fetch_array() from incrementing, or step it back?Another idea I had was to add a function before it starts that checks the db to make sure all mon-fri have entries (there shouldn't be a blank day) then return an error if a day is missing.
Now if I do that will I have to have 2 query or can I reset the mysql_fetch_array()?
I found this function helpfull with resetting the query array
mysql_data_seek($result, 0);