chocopi Posted June 12, 2007 Share Posted June 12, 2007 can anyone tell me why this loop is never ending, the page will not load because it is stuck in a loop, but i am useless when it comes down to loops <?php // start session session_start(); require_once('page_header.php'); // set sessions to variables $id = $_SESSION['id']; $username = $_SESSION['username']; // set board id $board = '1'; // set post_num $post_num = '1'; // get max post_num $query = mysql_query("SELECT MAX(post_num) AS `post_num` FROM `zBoard_messages` WHERE board_id='$board'") or die(mysql_error()); $fetch= mysql_fetch_assoc($query) or die(mysql_error()); $post_num_max = $fetch['post_num']; while ($post_num <= $post_num_max) { // select message id's from db $query = mysql_query("SELECT `id`,`poster_id`, `post_num`, `subject`, `message`, `date` FROM `zBoard_messages` WHERE board_id='$board'") or die(mysql_error()); $row = mysql_fetch_assoc($query) or die(mysql_error()); // set db results to variables $id = $row['id']; $poster_id = $row['poster_id']; $post_num = $row['post_num']; $subject = $row['subject']; $text = $row['message']; $date = $row['date']; // get username $query = mysql_query("SELECT username FROM `zBoard_users` WHERE id='$poster_id'") or die(mysql_error()); $row = mysql_fetch_assoc($query) or die(mysql_error()); $poster = $row['username']; // convert message into viewer message require_once('bbcode.php'); // get legible date list ($date,$time) = explode(' ',$date); list ($year,$month,$day) = explode('-',$date); $date = "$day/$month/$year"; //display contents ?> <br /> <center> <table width="80%" cellspacing="0" cellpadding="0" border="0"> <tr> <td width="20%" align="center"><?php echo "#".$post_num.""; ?><br /><hr></td> <td width="80%" align="center"><b>Subject:</b> <?php echo $subject; ?> | <b>Posted at:</b> <?php echo $time; ?> on <?php echo $date; ?><br /><hr></td> </tr> <tr> <td width="20%" align="center" valign="top"> <table width="100%" cellspacing="0" cellpadding="0" border="0"> <tr> <td width="100%" align="center"> <?php echo ucfirst($poster); ?> </td> </tr> <tr> <td width="100%" align="center"> <?php echo"<img src=\"".$poster_id.".gif\" width=\"45\" height=\"57\" border=\"0\" alt=\"".ucfirst($poster)."'s Avatar\" />"; ?> </td> </tr> </table> </td> <td width="70%" align="left"> <table width="100%" cellspacing="0" cellpadding="0" border="0"> <tr> <td width="100%" align="left"> <?php echo $text; ?> </td> </tr> </table> </td> </tr> <tr> <td colspan="2"> <hr> </td> </tr> </table> </center> <?php $post_num++; } ?> Thanks ~ Chocopi EDIT: Ok i lied its just finish loading after about 10 mins with thousands of the same result Link to comment https://forums.phpfreaks.com/topic/55263-solved-never-ending-loop/ Share on other sites More sharing options...
kenrbnsn Posted June 12, 2007 Share Posted June 12, 2007 You've reused the variable "$post_num" in your loop. That might be throwing you into the almost infinite loop. Ken Link to comment https://forums.phpfreaks.com/topic/55263-solved-never-ending-loop/#findComment-273126 Share on other sites More sharing options...
chocopi Posted June 12, 2007 Author Share Posted June 12, 2007 ok thanks, so what do i need to get rid of / change Link to comment https://forums.phpfreaks.com/topic/55263-solved-never-ending-loop/#findComment-273127 Share on other sites More sharing options...
kenrbnsn Posted June 12, 2007 Share Posted June 12, 2007 Use a different variable to control your loop: <?php // get max post_num $query = mysql_query("SELECT MAX(post_num) AS `post_num_max` FROM `zBoard_messages` WHERE board_id='$board'") or die(mysql_error()); $fetch= mysql_fetch_assoc($query) or die(mysql_error()); $post_num_max = $fetch['post_num_max']; for ($pn = 1; $pn <= $post_num_max; $pn++) // use a for loop instead of the while loop //while ($post_num <= $post_num_max) ?> Then remove the <?php $postnum++; ?> at the end of the loop. Ken Link to comment https://forums.phpfreaks.com/topic/55263-solved-never-ending-loop/#findComment-273132 Share on other sites More sharing options...
chocopi Posted June 12, 2007 Author Share Posted June 12, 2007 kool thanks, but i need for the $post_num to be incremented Link to comment https://forums.phpfreaks.com/topic/55263-solved-never-ending-loop/#findComment-273139 Share on other sites More sharing options...
kenrbnsn Posted June 12, 2007 Share Posted June 12, 2007 The $pn variable I'm using in the for loop is automatically incremented. Ken Link to comment https://forums.phpfreaks.com/topic/55263-solved-never-ending-loop/#findComment-273144 Share on other sites More sharing options...
chocopi Posted June 12, 2007 Author Share Posted June 12, 2007 oh yea, its because i forgot to change <?php $query = mysql_query("SELECT `id`,`poster_id`, `subject`, `message`, `date` FROM `zBoard_messages` WHERE board_id='$board'") or die(mysql_error()); // to $query = mysql_query("SELECT `id`,`poster_id`, `subject`, `message`, `date` FROM `zBoard_messages` WHERE board_id='$board' && post_num='$pn'") or die(mysql_error()); ?> THANKS Kenrbnsn and everyone else ~ Chocopi Link to comment https://forums.phpfreaks.com/topic/55263-solved-never-ending-loop/#findComment-273150 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.