jbrill Posted July 21, 2007 Share Posted July 21, 2007 hey guys, im making a very simple private messaging system and i cant figure out why it is only pulling the first message in the table, and not all of them..i think i need a while statement somewhere, but since im new to php i dont know where to put it or how it looks thanks! <? include 'admin_header.php'; //finds the messages $sessionid = $_SESSION['id']; $getmessages = "SELECT * FROM messages WHERE to_id=$sessionid"; $messagelist = mysql_query($getmessages); $messages = mysql_fetch_array($messagelist); $fromid = $messages['from_id']; //converts id to a name $getname = "SELECT * FROM users WHERE id=$fromid"; $namelist = mysql_query($getname); $name = mysql_fetch_array($namelist); $fname = $name['first_name']; $lname = $name['last_name']; //this is the variable to send to "message_read.php" $sendnow = "message_read.php?messageid=".$messages['message_id'].""; // creates alternating rows $css_class = ($css_class=='row_style_1') ? 'row_style_2' : 'row_style_1'; ?> <br> <table width="90%" align="center" border="0"><tr><td align="left"><font class="section">Morowat Global Private Messaging System</font></td><td align="right" valign="bottom"> <a href="message_create.php">Create New Message</a> <a href="#">Inbox</a> <a href="#">Sent Messages</a> </td></tr></table> <table width="90%" align="center" bgcolor="#ffffff" class="MainBody1"> <tr> <td width="60" align="center"> </td><td>Date</td><td>Subject</td><td>From</td> </tr> <? echo '<tr class=\"$css_class\">'; ?> <td width="60" align="center"> </td> <td><? echo "<a href=\"$sendnow\">". $messages['date_sent'] . "</a>"; ?></td> <td><? echo "<a href=\"$sendnow\">". $messages['subject'] . "</a>"; ?></td> <td><? echo "<a href=\"$sendnow\">"; echo $fname; echo "</a>"; ?></td> </tr> </table> <? include 'admin_footer.php'; ?> Quote Link to comment Share on other sites More sharing options...
L Posted July 21, 2007 Share Posted July 21, 2007 you need to use a while loop for that...the way u have it your retrieving one message..and fetching the contents of that one message...u need a while loop so u can do it for all the pms with the toid being the session id of the user Quote Link to comment Share on other sites More sharing options...
jbrill Posted July 21, 2007 Author Share Posted July 21, 2007 could you show me what that would look like? Quote Link to comment Share on other sites More sharing options...
ss32 Posted July 21, 2007 Share Posted July 21, 2007 you are going to need a while loop, and a more advanced query. your query should look like this: SELECT messages.title ... (more message data), users.first_name FROM messages LEFT JOIN users ON (messages.from_id = users.id) LIMIT 25 this will get you the data that you want (i think, you will have to input the correct fields in the SELECT clause) your while loop should look like this, where $q = mysql_query($sql), and $sql = the query above: <?php while ($row = mysql_fetch_assoc($q)) { //echo your stuff here } ?> and that should do it. Quote Link to comment Share on other sites More sharing options...
jbrill Posted July 21, 2007 Author Share Posted July 21, 2007 it really needs to be that complicated to just display everything for to is in the matching "to_id"? im completely lost looking at that query ??? Quote Link to comment Share on other sites More sharing options...
ss32 Posted July 21, 2007 Share Posted July 21, 2007 i dont know if you are ready to code a PM system if you cant look at that query... it basically joins the rows based on the data that relates the two tables- one row for each matching result in the left table. it is really difficult to explain... but it is basically for getting all of the data in one query. i forgot to implement the to_id join, but i think you can do that on your own (= (you will have to alias the users tables though) Quote Link to comment Share on other sites More sharing options...
jbrill Posted July 21, 2007 Author Share Posted July 21, 2007 I dont think its clear enough what im trying to do. there is only one table here... and this is just the inbox its gets the "session id" and then looks in "messages" table for the the matching number in the "to_id" field. it then pulls all the ones that match. the query for the "users" table just uses the session id to find the appropriate 1st and last name for the sender. here is what it looks like Quote Link to comment Share on other sites More sharing options...
L Posted July 21, 2007 Share Posted July 21, 2007 i think he was referring to the tables in ur database...not in ur layout.... Quote Link to comment Share on other sites More sharing options...
jbrill Posted July 21, 2007 Author Share Posted July 21, 2007 i really am so confused... Quote Link to comment Share on other sites More sharing options...
wsantos Posted July 21, 2007 Share Posted July 21, 2007 If I understand your problem correctly I think you should use an array.try this... while($messages = mysql_fetch_array($messagelist)) $messageArray[] = $messages; Then display your output index by index... Quote Link to comment Share on other sites More sharing options...
jbrill Posted July 21, 2007 Author Share Posted July 21, 2007 can you please explain to me how i would use that? and where in my code i should put it.. like i said, i am new to php, but hvaing been doing very well up until now lol Quote Link to comment Share on other sites More sharing options...
wsantos Posted July 22, 2007 Share Posted July 22, 2007 include 'admin_header.php'; //finds the messages $sessionid = $_SESSION['id']; $getmessages = "SELECT * FROM messages WHERE to_id=$sessionid"; $messagelist = mysql_query($getmessages); $messages = mysql_fetch_array($messagelist); // replace this line with the one 1 posted earlier $fromid = $messages['from_id']; for your output on the browser try this... foreach($messageArray as $row) { ?> <td><?php echo "<a href=\'$sendnow\'>" . $messages['date_sent'] . "</a>"; ?></td> <td><?php echo "<a href=\'$sendnow\'>" . $messages['subject'] . "</a>"; ?></td> <td><?php echo "<a href=\'$sendnow\'>" . $fname; ?>"</a>" <?php } ?> avoid parse errors...this code may have one in itself if this answer the question topic closed button is near the bottom of the screen... Quote Link to comment 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.