Jump to content

[SOLVED] Never ending loop


chocopi

Recommended Posts

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  ;D

 

~ 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

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

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 ;D

 

~ Chocopi

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.