Jump to content

Recommended Posts

I am very weak when it comes to loops so what i want to do it way out of my league.

 

I need a loop to show the results of a message board system where $board = 1, so it shoes all the results. The current code only shows the first result.

 

So here is my code

 

<?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';
// select message id's from db
$query = mysql_query("SELECT `id`,`poster_id`, `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'];
$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";

$post_num = 1;

//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>

 

Please ask any question you want as this message is uber badly worded

 

Cheers,

 

~ Chocopi

Link to comment
https://forums.phpfreaks.com/topic/55243-solved-loops/
Share on other sites

Instead of using die() each time it's probably best to add some error handling instead otherwise your script will terminate each time an error is encountered.

 

Example:

if ($query = mysql_query("SELECT username FROM `zBoard_users` WHERE id='$poster_id'")) {
  if ($row = mysql_fetch_assoc($query)) {
    if (empty($row['username'])) {
      [do your stuff here]
    } else {tell user username is empty}
  } else {tell user mysql_fetch_assoc failed}
} else {tell user mysql_query failed}

Link to comment
https://forums.phpfreaks.com/topic/55243-solved-loops/#findComment-273043
Share on other sites

no that doesnt quite do it, now i have post_num fixed (thanks to Yesideez) i can tell you what i would like

 

i would like to have the loop so it takes all of the values that equal $board and then put them out in order of post_num

 

Hope that clears up what i need  ;D

 

Thanks,

 

~ Chocopi

Link to comment
https://forums.phpfreaks.com/topic/55243-solved-loops/#findComment-273103
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.