bennyboywonder Posted February 2, 2007 Share Posted February 2, 2007 The following code should output xml for each entry in my blog, except the first while loop is only running once, even if there is more than one entry to output. This is weird, because it is *identical* to the while loop used on my main blog which works perfectly. Any help, much appreciated... <?php include 'opensql.php'; include '../../extphp/functions.php'; if($_GET['action'] == "readblog") { header("Content-Type: text/xml"); echo "<blog><posts>"; if(isset($_GET['month']) ) $monthpage = $_GET['month']; else $monthpage = date('m'); if(isset($_GET['year'] ) ) $yearpage = $_GET['year']; else $yearpage = date('Y'); if ($monthpage < 12) { $nextmonth = $monthpage + 1; $nextyear = $yearpage; } else { $nextmonth = "1"; $nextyear = $yearpage + 1; } $query = "SELECT COUNT(Eid) AS numrows FROM entries WHERE EntryDate >= '$yearpage-$monthpage-01' AND EntryDate < '$nextyear-$nextmonth-01'"; $results = mysql_query($query) or die('Error, query (count entries in month) failed'); $rows = mysql_fetch_array($results); $entrycount = $rows['numrows']; if ($entrycount < 1) { if ($monthpage > 1) $monthpage--; else { $monthpage = "12"; $yearpage--; } $query = "SELECT COUNT(Eid) AS numrows FROM entries WHERE EntryDate >= '$yearpage-$monthpage-01' AND EntryDate < '$nextyear-$nextmonth-01'"; $results = mysql_query($query) or die('Error, query (count entries in month) failed'); $rows = mysql_fetch_array($results); $entrycount = $rows['numrows']; } $query = "SELECT COUNT(Eid) AS offsetrows FROM entries WHERE EntryDate >= '$nextyear-$nextmonth-01'"; $result = mysql_query($query) or die('Error, query (count offset) failed'); $rows = mysql_fetch_array($result); $offset = $rows['offsetrows']; If(isset($_POST['entry'])) $query = "SELECT Eid, EntryTime, EntryDate, EntryTitle, MusicTitle, MusicArtist, Body, Onfeed FROM entries WHERE Eid = '".$_POST['entry']."'"; Else $query = "SELECT Eid, EntryTime, EntryDate, EntryTitle, MusicTitle, MusicArtist, Body, Onfeed FROM entries ORDER BY EntryDate DESC, EntryTime DESC LIMIT $offset, $entrycount"; $result = mysql_query($query) or die('\nError query (read post or posts) failed'); while($row = mysql_fetch_row($result)) { include 'processpostquery.php'; ?> <post id="<?php echo $postid ?>" onfeed="<?php echo $onfeed ?>"> <when> <time> <hour><?php echo $time_hour ?></hour> <minute><?php echo $time_minute ?></minute> <second><?php echo $time_seconds ?></second> </time> <date><?php echo $postdate ?></date> </when> <title><?php echo $posttitle ?></title> <music> <title><?php echo $postmusictitle ?></title> <artist><?php echo $postmusicartist ?></artist> </music> <body><?php echo $postbody ?></body> <?php $query = "SELECT COUNT(Cid) AS numrows FROM comments WHERE EntryID=".$postid; $result = mysql_query($query) or die('Error query (count comments) failed'); $row = mysql_fetch_row($result); $commentcount = $row[0]; if ($commentcount < 1) echo "<comments></comments></post>"; else { echo "<comments empty='0'>"; $query = "SELECT CommentDate, CommentTime, EntryName, EntryEmail, CommentBody, Admin, Ip, Cid FROM comments WHERE EntryID = $postid ORDER BY Cid DESC"; $result = mysql_query($query) or die ('Error query (load comments) failed'); while($row = mysql_fetch_row($result)) { include 'processcommentsquery.php'; ?> <comment id="<?php echo $cid ?>" isAdmin="<?php echo $isadmin ?>"> <when> <time> <hour><?php echo $time_hour ?></hour> <minute><?php echo $time_minute ?></minute> <second><?php echo $time_seconds ?></second> </time> <date> <day><?php echo $commentday ?></day> <month><?php echo $commentmonth ?></month> <year><?php echo $commentyear ?></year> </date> </when> <who> <?php if($user == "isadmin") echo "<ip>".$commentip."</ip>"; ?> <email><?php echo $commentemail ?></email> <name><?php echo $commentname ?></name> </who> <body><?php echo $commentbody ?></body> </comment> <?php } echo "</comments></post>"; } } echo "</posts>"; echo "</blog>"; } include 'closesql.php'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/36846-solved-help-please-while-loop-not-looping/ Share on other sites More sharing options...
JasonLewis Posted February 2, 2007 Share Posted February 2, 2007 add this outside of your while loop. $i = 0; then right at the end of your while loop, inside it, add this: echo $i."<br>"; $i++; and see how much times it is actually running. you could even check how many rows there are from the query you are running. Quote Link to comment https://forums.phpfreaks.com/topic/36846-solved-help-please-while-loop-not-looping/#findComment-175790 Share on other sites More sharing options...
bennyboywonder Posted February 3, 2007 Author Share Posted February 3, 2007 unfortunately I already did both those things. The query definitely supplies at least two rows, and the while loop is definitely only looping once. GRRRR Quote Link to comment https://forums.phpfreaks.com/topic/36846-solved-help-please-while-loop-not-looping/#findComment-175967 Share on other sites More sharing options...
yzerman Posted February 3, 2007 Share Posted February 3, 2007 which while loop? ??? Quote Link to comment https://forums.phpfreaks.com/topic/36846-solved-help-please-while-loop-not-looping/#findComment-175976 Share on other sites More sharing options...
bennyboywonder Posted February 3, 2007 Author Share Posted February 3, 2007 oh, sorry, the first one. The second one works fine Quote Link to comment https://forums.phpfreaks.com/topic/36846-solved-help-please-while-loop-not-looping/#findComment-175989 Share on other sites More sharing options...
yzerman Posted February 3, 2007 Share Posted February 3, 2007 you never closed the while loop tag then: your code: while($row = mysql_fetch_row($result)) { include 'processpostquery.php'; ?> should be: while($row = mysql_fetch_row($result)) { include 'processpostquery.php'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/36846-solved-help-please-while-loop-not-looping/#findComment-175993 Share on other sites More sharing options...
bennyboywonder Posted February 3, 2007 Author Share Posted February 3, 2007 you never closed the while loop tag then: your code: while($row = mysql_fetch_row($result)) { include 'processpostquery.php'; ?> should be: while($row = mysql_fetch_row($result)) { include 'processpostquery.php'; } ?> No, the closing brace is much further down. Quote Link to comment https://forums.phpfreaks.com/topic/36846-solved-help-please-while-loop-not-looping/#findComment-176369 Share on other sites More sharing options...
yzerman Posted February 3, 2007 Share Posted February 3, 2007 Right, but you closed the PHP statement with ?> meaning that the loop never gets closed. its like writing <?php if (!isset($var)) { ?> echo 'blah'; <?php } else { echo 'ha'; } ?> which just wont work Quote Link to comment https://forums.phpfreaks.com/topic/36846-solved-help-please-while-loop-not-looping/#findComment-176372 Share on other sites More sharing options...
kenrbnsn Posted February 3, 2007 Share Posted February 3, 2007 Going in and out of PHP has no affect on when a loop is closed. Ken Quote Link to comment https://forums.phpfreaks.com/topic/36846-solved-help-please-while-loop-not-looping/#findComment-176383 Share on other sites More sharing options...
yzerman Posted February 3, 2007 Share Posted February 3, 2007 Ahh, didnt know that, thanks ken Quote Link to comment https://forums.phpfreaks.com/topic/36846-solved-help-please-while-loop-not-looping/#findComment-176384 Share on other sites More sharing options...
bennyboywonder Posted February 4, 2007 Author Share Posted February 4, 2007 *bump* still need help here guys... Quote Link to comment https://forums.phpfreaks.com/topic/36846-solved-help-please-while-loop-not-looping/#findComment-176967 Share on other sites More sharing options...
boo_lolly Posted February 4, 2007 Share Posted February 4, 2007 ever heard of the 'tab' key? <?php include 'opensql.php'; include '../../extphp/functions.php'; if($_GET['action'] == "readblog"){ header("Content-Type: text/xml"); echo "<blog><posts>"; if(isset($_GET['month'])) $monthpage = $_GET['month']; else $monthpage = date('m'); if(isset($_GET['year'])) $yearpage = $_GET['year']; else $yearpage = date('Y'); if($monthpage < 12){ $nextmonth = $monthpage + 1; $nextyear = $yearpage; }else{ $nextmonth = "1"; $nextyear = $yearpage + 1; } $query = "SELECT COUNT(Eid) AS numrows FROM entries WHERE EntryDate >= '$yearpage-$monthpage-01' AND EntryDate < '$nextyear-$nextmonth-01'"; $results = mysql_query($query) or die('Error, query (count entries in month) failed'); $rows = mysql_fetch_array($results); $entrycount = $rows['numrows']; if($entrycount < 1){ if($monthpage > 1) $monthpage--; else{ $monthpage = "12"; $yearpage--; } $query = "SELECT COUNT(Eid) AS numrows FROM entries WHERE EntryDate >= '$yearpage-$monthpage-01' AND EntryDate < '$nextyear-$nextmonth-01'"; $results = mysql_query($query) or die('Error, query (count entries in month) failed'); $rows = mysql_fetch_array($results); $entrycount = $rows['numrows']; } $query = "SELECT COUNT(Eid) AS offsetrows FROM entries WHERE EntryDate >= '$nextyear-$nextmonth-01'"; $result = mysql_query($query) or die('Error, query (count offset) failed'); $rows = mysql_fetch_array($result); $offset = $rows['offsetrows']; if(isset($_POST['entry'])) $query = "SELECT Eid, EntryTime, EntryDate, EntryTitle, MusicTitle, MusicArtist, Body, Onfeed FROM entries WHERE Eid = '".$_POST['entry']."'"; else $query = "SELECT Eid, EntryTime, EntryDate, EntryTitle, MusicTitle, MusicArtist, Body, Onfeed FROM entries ORDER BY EntryDate DESC, EntryTime DESC LIMIT $offset, $entrycount"; $result = mysql_query($query) or die('\nError query (read post or posts) failed'); while($row = mysql_fetch_row($result)){ include 'processpostquery.php'; ?> <post id="<?php echo $postid ?>" onfeed="<?php echo $onfeed ?>"> <when> <time> <hour><?php echo $time_hour ?></hour> <minute><?php echo $time_minute ?></minute> <second><?php echo $time_seconds ?></second> </time> <date><?php echo $postdate ?></date> </when> <title><?php echo $posttitle ?></title> <music> <title><?php echo $postmusictitle ?></title> <artist><?php echo $postmusicartist ?></artist> </music> <body><?php echo $postbody ?></body> <?php $query = "SELECT COUNT(Cid) AS numrows FROM comments WHERE EntryID=".$postid; $result = mysql_query($query) or die('Error query (count comments) failed'); $row = mysql_fetch_row($result); $commentcount = $row[0]; if($commentcount < 1) echo "<comments></comments></post>"; else{ echo "<comments empty='0'>"; $query = "SELECT CommentDate, CommentTime, EntryName, EntryEmail, CommentBody, Admin, Ip, Cid FROM comments WHERE EntryID = $postid ORDER BY Cid DESC"; $result = mysql_query($query) or die ('Error query (load comments) failed'); while($row = mysql_fetch_row($result)){ include 'processcommentsquery.php'; ?> <comment id="<?php echo $cid ?>" isAdmin="<?php echo $isadmin ?>"> <when> <time> <hour><?php echo $time_hour ?></hour> <minute><?php echo $time_minute ?></minute> <second><?php echo $time_seconds ?></second> </time> <date> <day><?php echo $commentday ?></day> <month><?php echo $commentmonth ?></month> <year><?php echo $commentyear ?></year> </date> </when> <who> <?php if($user == "isadmin") echo "<ip>".$commentip."</ip>"; ?> <email><?php echo $commentemail ?></email> <name><?php echo $commentname ?></name> </who> <body><?php echo $commentbody ?></body> </comment> <?php } echo "</comments></post>"; } } echo "</posts>"; echo "</blog>"; } include 'closesql.php'; ?> it is MUCH easier to locate a problem when you have organized your code. do you see the error now? Quote Link to comment https://forums.phpfreaks.com/topic/36846-solved-help-please-while-loop-not-looping/#findComment-176993 Share on other sites More sharing options...
bennyboywonder Posted February 4, 2007 Author Share Posted February 4, 2007 ever heard of the 'tab' key? it is MUCH easier to locate a problem when you have organized your code. do you see the error now? Um, I just copied and pasted the code from my editor. It was tabbed in my editor... and no, I don't see it. Should I? Quote Link to comment https://forums.phpfreaks.com/topic/36846-solved-help-please-while-loop-not-looping/#findComment-177015 Share on other sites More sharing options...
sasa Posted February 4, 2007 Share Posted February 4, 2007 you use same variable name ($result) in both while loop try to make names diferent ($result1 and $result2) Quote Link to comment https://forums.phpfreaks.com/topic/36846-solved-help-please-while-loop-not-looping/#findComment-177016 Share on other sites More sharing options...
bennyboywonder Posted February 4, 2007 Author Share Posted February 4, 2007 you use same variable name ($result) in both while loop try to make names diferent ($result1 and $result2) YAY! fixed the problem, thanks! Quote Link to comment https://forums.phpfreaks.com/topic/36846-solved-help-please-while-loop-not-looping/#findComment-177018 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.