chocopi Posted June 12, 2007 Share Posted June 12, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/55243-solved-loops/ Share on other sites More sharing options...
Full-Demon Posted June 12, 2007 Share Posted June 12, 2007 $board = 1; while ($board<5) { [all your actions to be looped here] $board++; } FD Quote Link to comment https://forums.phpfreaks.com/topic/55243-solved-loops/#findComment-273038 Share on other sites More sharing options...
chocopi Posted June 12, 2007 Author Share Posted June 12, 2007 Thanks, but thats not whta i want, sorry. I need for $board to remain a constant and all the messages will be pulled where $board=1 Quote Link to comment https://forums.phpfreaks.com/topic/55243-solved-loops/#findComment-273042 Share on other sites More sharing options...
Yesideez Posted June 12, 2007 Share Posted June 12, 2007 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} Quote Link to comment https://forums.phpfreaks.com/topic/55243-solved-loops/#findComment-273043 Share on other sites More sharing options...
Yesideez Posted June 12, 2007 Share Posted June 12, 2007 This any good? $query = mysql_query("SELECT `id`,`poster_id`, `subject`, `message`, `date` FROM `zBoard_messages` WHERE board_id='$board'") while ($row = mysql_fetch_assoc($query)) { do your stuff here } Quote Link to comment https://forums.phpfreaks.com/topic/55243-solved-loops/#findComment-273044 Share on other sites More sharing options...
Full-Demon Posted June 12, 2007 Share Posted June 12, 2007 Ah that way, To get the values, at 'do your stuff here': $row["userid"]; $row["fullname"]; $row["userstatus"]; FD Quote Link to comment https://forums.phpfreaks.com/topic/55243-solved-loops/#findComment-273045 Share on other sites More sharing options...
chocopi Posted June 12, 2007 Author Share Posted June 12, 2007 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 Thanks, ~ Chocopi Quote Link to comment https://forums.phpfreaks.com/topic/55243-solved-loops/#findComment-273103 Share on other sites More sharing options...
Full-Demon Posted June 12, 2007 Share Posted June 12, 2007 in mysql command you add the order command and you do it exactly as we said FD Quote Link to comment https://forums.phpfreaks.com/topic/55243-solved-loops/#findComment-273421 Share on other sites More sharing options...
chocopi Posted June 12, 2007 Author Share Posted June 12, 2007 ok, i have done so and it works great. Cheers ~ Chocopi Quote Link to comment https://forums.phpfreaks.com/topic/55243-solved-loops/#findComment-273428 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.