Jump to content

[SOLVED] Loop not generating proper values.


kjtocool

Recommended Posts

This loop is giving me problems, hoping another set of eyes can help.  Basically, the variable $pageNumber is refusing to increment, and the variable $comment_start is empty upon echo.

 

Here is the code:

 

<?php
// $num_comments is 12 for this run.
if ($num_comments > 10) {
$comments_remaining = $num_comments;
$pages = "";
$pageNumber = 1;
$comment_start = 0;

while ($comments_remaining > 0) {
	if ($pageNumber = 1) {
		$pages .= '<a href="http://www.*****.com/articleIndex.php?tid=' . $article_ID . '&commentStart=' . $comment_start . '">' . $pageNumber . '</a>';
	}
	else {
		$pages .= ', <a href="http://www.*****.com/articleIndex.php?tid=' . $article_ID . '&commentStart=' . $comment_start . '">' . $pageNumber . '</a>';
	}

	echo "PN: " . $pageNumber . " CR: " . $comments_remaining . " CS: " . $comment_Start;

	$comments_remaining = $comments_remaining - 10;
	$pageNumber = $pageNumber + 1;
	$comment_start = $comment_start + 10;
}

echo '<table width="70%" border="0" align="center" cellpadding="0" cellspacing="1">';
echo   '<tr>';
echo	 '<td align="right"><span class="style10">Page: ' . $pages . '</span></td>';
echo   '</tr>';
echo '</table><br />';
}
?>

 

Here is the output of the echo statement in the while loop:

 

PN: 1 CR: 12 CS: PN: 1 CR: 2 CS:

 

The $pages variable displays properly, though with the incorrect values.

 

Hoping an extra set of eyes can spot what I'm doing wrong.

Link to comment
Share on other sites

try

<?php
// $num_comments is 12 for this run.
if ($num_comments > 10) {
$comments_remaining = $num_comments;
$pages = "";
$pageNumber = 1;
$comment_start = 0;

while ($comments_remaining > 0) {
	if ($pageNumber = 1) {
		$pages .= '<a href="http://www.*****.com/articleIndex.php?tid=' . $article_ID . '&commentStart=' . $comment_start . '">' . $pageNumber . '</a>';
	}
	else {
		$pages .= ', <a href="http://www.*****.com/articleIndex.php?tid=' . $article_ID . '&commentStart=' . $comment_start . '">' . $pageNumber . '</a>';
	}

	echo "PN: " . $pageNumber . " CR: " . $comments_remaining . " CS: " . $comment_Start;

	$comments_remaining = $comments_remaining - 10;
	$pageNumber++;
	$comment_start = $comment_start + 10;
}

echo '<table width="70%" border="0" align="center" cellpadding="0" cellspacing="1">';
echo   '<tr>';
echo	 '<td align="right"><span class="style10">Page: ' . $pages . '</span></td>';
echo   '</tr>';
echo '</table><br />';
}
?>

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.