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
https://forums.phpfreaks.com/topic/82247-solved-loop-not-generating-proper-values/
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 />';
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.