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
https://forums.phpfreaks.com/topic/61044-why-isnt-this-displaying-all-the-results/
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.

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)

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

 


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

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.