Jump to content

name variable displaying for multiple names?


kirkh34

Recommended Posts

this page is an inbox of links to messages

 

i have a $firstname and $lastname that displays next to a picture and they are both links to a user profile... i use the same $from_id to link to their profile and it works just fine... i am trying to use this $from_id variable to pull from a different table to obtain their first and last name, the problem is... it shows the first and last name of the user who has sent the latest message for all of the users... i can't figure it out... all the other users links to their profile works just fine with the $from_id variable but for some reason it won't pull individual first and last names....maybe it's because i'm pulling from a different table? i may be doing this wrong but i cannot figure it out...help is appreciated 

<?php

session_start();
include_once "scripts/connect_to_mysql.php";
if (!isset($_SESSION['id']))
header('Location: /index.php');


$user = $_SESSION['id'];




if (isset($_GET['id']))
{
$delete_id = $_GET['id'];
$sql = mysql_query("DELETE FROM messages WHERE message_id ='$delete_id'")  
or die (mysql_error()); 
}



$sql = mysql_query("SELECT * FROM messages WHERE to_user ='$user'")  
or die (mysql_error()); 

while ($row = mysql_fetch_array($sql))
{

$from_id = $row["from_user"];

}//close while




$sql = mysql_query("SELECT * FROM mymembers WHERE id ='$from_id'")  
or die (mysql_error()); 

while ($row = mysql_fetch_array($sql))
{
$firstname = $row["firstname"];
$lastname = $row["lastname"];
} //close while


?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

<style type="text/css">
#customers
{
font-family:"Trebuchet MS", Arial, Helvetica, sans-serif;
border-collapse:collapse;
padding: 0px;

}
#customers td, #customers th 
{
width: auto;
font-size:.8em;
border-bottom:1px solid #98bf21;

padding:5px 20px 5px 0px;
}
#customers th 
{
font-size:1.1em;
text-align:center;
padding-top:5px;
padding-bottom:4px;
background-color:#A7C942;
color:#ffffff;
width: auto;
}


#content{
color: #999;
margin: 0 0 0 0;
}	

#title{
margin: 0 0 0 0 ;

}
#date{
color: #999;
margin: 0;
}


</style>


</head>

<body>


<table id="customers">
<tr>
  <th>Inbox</th>
</tr>
<tr>
<td></td>
<td>Name</td>
<td></td>
<td>Subject</td>
<td>Date</td>
<td>
</td>
</tr>

<?php
$sql = mysql_query("SELECT * FROM messages WHERE to_user ='$user'")  
or die (mysql_error()); 

while ($row = mysql_fetch_array($sql))
{
$message_id = $row["message_id"];
$message_content = $row["message_content"];
$message_date = $row["message_date"];
$message_title = $row["message_title"];
$from_id = $row["from_user"];
$message_read = $row["message_read"];

$position= 30; // Define how many character you want to display. 
$title = substr($message_title, 0, $position); 

$content = substr($message_content, 0, $position);

    $check_pic = "memberFiles/$from_id/image01.jpg";
$default_pic = "memberFiles/0/image01.jpg";
if (file_exists($check_pic)) {
    $from_user = "<img src=\"$check_pic\" width=\"50px\" />"; // forces picture to be 100px wide and no more
} else {
$from_user = "<img src=\"$default_pic\" width=\"50px\" />"; // forces default picture to be 100px wide and no more
}

$envelope_read = "images/read.gif";
$envelope_unread = "images/unread.gif";
if ($message_read == '0'){
	$envelope = "<img src=\"$envelope_unread\" width=\"25px\" />";
} else {
	$envelope = "<img src=\"$envelope_read\" width=\"25px\" />";
}


echo "<tr>";

echo "<td>";
echo "<a href='profile.php?id=$from_id'>$from_user</a>";
echo "</td>";

echo "<td>";
echo "<a href='profile.php?id=$from_id'>$firstname $lastname</a>";
echo "</td>";

echo "<td>";
echo "$envelope";
echo "</td>";

echo "<td>";
echo "<a href='message.php?id=$message_id&user=$user'><p id='title'>$title</p></a>";
echo "<p id='content'>$content</p>";
echo "</td>";

echo "<td>";
if (date("d, m, y", time()) == date("d, m, y", strtotime($message_date))   ) {
   print date('h:i A', strtotime($message_date)); }
else {
print "<p id='date'>";
   print  date('m/d/y', strtotime($message_date));
   print "</p>";
   print date('h:i A', strtotime($message_date));
   
}
echo "</td>";

echo "<td>";
echo "<a href='inbox.php?id=$message_id'>Delete</a>";
echo "</td>";

echo "</tr>";
} //close while




print "$from_id";

?>


</table>






</body>
</html>

 

Link to comment
Share on other sites

Your two while loops are pointless, your possibly grabbing multiple rows, in which case the variables inside those while loops will always be set from the last row the query returned.

 

You should only need a single query to get everything you need. (You can grab data from multiple tables using mysql).

 

-cb-

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.