Jump to content

[SOLVED] Help please! While loop not looping!


bennyboywonder

Recommended Posts

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';
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.