Jump to content

why isnt this displaying all the results?


jbrill

Recommended Posts

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';
?>

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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

 

pmsystem.jpg

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites


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

Link to comment
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.