kjtocool Posted December 18, 2007 Share Posted December 18, 2007 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 More sharing options...
cooldude832 Posted December 18, 2007 Share Posted December 18, 2007 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 https://forums.phpfreaks.com/topic/82247-solved-loop-not-generating-proper-values/#findComment-418046 Share on other sites More sharing options...
kjtocool Posted December 19, 2007 Author Share Posted December 19, 2007 The only change I noticed was to change: $pageNumber = pageNumber +1; to $pageNumber++; The output remained the same. (I had tried this first, before switching it) Link to comment https://forums.phpfreaks.com/topic/82247-solved-loop-not-generating-proper-values/#findComment-418048 Share on other sites More sharing options...
rab Posted December 19, 2007 Share Posted December 19, 2007 Change echo "PN: " . $pageNumber . " CR: " . $comments_remaining . " CS: " . $comment_Start; to echo "PN: " . $pageNumber . " CR: " . $comments_remaining . " CS: " . $comment_start; Just a quick look over... Link to comment https://forums.phpfreaks.com/topic/82247-solved-loop-not-generating-proper-values/#findComment-418070 Share on other sites More sharing options...
kjtocool Posted December 19, 2007 Author Share Posted December 19, 2007 Yep, good eye. That solves one of the issues. Now if I can just figure out what is causing my pageNumber variable not to increment. Output: PN: 1 CR: 13 CS: 0 PN: 1 CR: 3 CS: 10 Link to comment https://forums.phpfreaks.com/topic/82247-solved-loop-not-generating-proper-values/#findComment-418099 Share on other sites More sharing options...
cooldude832 Posted December 19, 2007 Share Posted December 19, 2007 Got it ! if ($pageNumber = 1) { need if ($pageNumber === 1) { you are resetting the value to 1 on each loop Link to comment https://forums.phpfreaks.com/topic/82247-solved-loop-not-generating-proper-values/#findComment-418103 Share on other sites More sharing options...
kjtocool Posted December 19, 2007 Author Share Posted December 19, 2007 Can you tell I only learned to write php a month ago? Thanks for the help guys. This forum is a lifesaver. Link to comment https://forums.phpfreaks.com/topic/82247-solved-loop-not-generating-proper-values/#findComment-418118 Share on other sites More sharing options...
cooldude832 Posted December 19, 2007 Share Posted December 19, 2007 na you ain't I do it all the time a few weeks back I stared at this <?php session_start(); //Login stuff if(valid){ $_SESSION['UserID'] == $row['UserID']; } ?> and couldn't fiugre out why it wasn't working even ppl here missed it Link to comment https://forums.phpfreaks.com/topic/82247-solved-loop-not-generating-proper-values/#findComment-418121 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.