PDA

View Full Version : Proper use of a loop inside a while loop



kuau
08-02-2008, 10:14 PM
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;
}
}

Nile
08-03-2008, 02:33 AM
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.

kuau
08-03-2008, 02:49 AM
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>

Nile
08-03-2008, 02:50 AM
Sorry, I'm hardly any good at querys. And can't find out the problem. :(

kuau
08-03-2008, 03:16 AM
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 } ?>

Nile
08-03-2008, 03:23 AM
What certain location does it not show?

kuau
08-03-2008, 03:24 AM
9th Life Hawaii

Nile
08-03-2008, 03:28 AM
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.

kuau
08-03-2008, 03:48 AM
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);