View Full Version : Proper use of a loop inside a while loop
Could someone please illustrate the best way to create a nested WHILE loop to do something like this? I am trying to learn best practices and the php & mySQL documentation is a bit abstruse. Thanks.
while($event = mysql_fetch_assoc($result)){
while startdate stays the same {
echo day title;
echo events for this day;
}
}
Is this what you want:
$startdateStatic = $startdate
while($event = mysql_fetch_assoc($result)){
while ($startdate==$startdateStatic) {
//comments
}
}
Where I put '//comments' I don't really understand what you want. I kinda do, but I can only do it with the fields.
Dear Nile: Forget the loop for now. Can you please tell me why this query calculates incorrectly? There are actually zero records where approved = 0 yet $num displays as "1". It should be "0".
$sql = "SELECT COUNT(*) FROM event WHERE approved = 0 AND startdate >= '" . $today . "'";
$result = mysql_query($sql,$connection) or die("Couldn't execute $sql query.");
$num = mysql_num_rows($result); ?>
<b><?php echo $num;?></b></td>
Sorry, I'm hardly any good at querys. And can't find out the problem. :(
OK no worries. How about this? I created a location table that fills a drop-down list on my form and for some reason it refuses to display a certain location, but it displays all the others no prob. I was wondering if it is because it is the only one that starts with a number, ie. 9th Life Sanctuary. Or maybe because it is the first one when sorted alphabetically. Could my code somehow skip the first record? Thanks. :)
$sql = "SELECT `l_id`, `location` FROM `location` WHERE `ok` = 1 ORDER BY location";
$result_loc = mysql_query($sql,$connection) or die("Couldn't execute $sql query.");
$row = mysql_fetch_array($result_loc);
while($row = mysql_fetch_row($result_loc)){ ?>
<option value="<?php echo $row[0];?>"><?php echo $row[1];?></option>
<?php } ?>
What certain location does it not show?
Does it have some sort of unique ID? Or will it just not display normally?
Can I also see the whole code and if you can a link to the page.
Dear Nile: I figured it out!!! I ascertained that it was because the code was skipping the first record. It had nothing to do with the record itself, only the fact that it was first. And what caused it was because I fetched the row twice. This is now correct:
$sql = "SELECT `l_id`, `location` FROM `location` WHERE `ok` = 1 ORDER BY location";
$result_loc = mysql_query($sql,$connection) or die("Couldn't execute $sql query.");
while($row = mysql_fetch_row($result_loc)){ ?>
<option value="<?php echo $row[0];?>"><?php echo $row[1];?></option>
<?php } ?>
Yahooooo!! Thanks. Maybe it is something similar with the mysql_num_rows($result);
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.