Jump to content

Recommended Posts

Hi. I am trying to make a code which displays a picture of a user who just sent you a message. However if i have two messages from the same user then i also get two pictures of the user. What do i need to change in this code to make the picture display itself only once per user?

 


<?php

$query = mysql_query("SELECT * FROM message WHERE receiver = '$usr' ORDER BY date DESC");

while ($row = mysql_fetch_assoc($query))
{
$message = $row['message'];
$from = $row['sender'];
$id = $row['id'];
$date = $row['date'];
$sender_image = $row['profile_picture'];
$new = $row['new'];
$name = $row['from_name'];

?>
<div class="featured">
<a href="read.php?id=<?php echo $id; ?>"><img src="<?php echo $sender_image; ?>" class="borders" /><div class="borders3"><div class="borders_text"><?php echo $name; ?></div></div></a></div>
<?php
}
?>

Link to comment
https://forums.phpfreaks.com/topic/263593-mysql-help/
Share on other sites

<?php

 

$query = mysql_query("SELECT * FROM message WHERE receiver = '$usr' ORDER BY date DESC");

 

// switch to check if the image is to be displayed

$show_image = TRUE;

 

while ($row = mysql_fetch_assoc($query))

{

    $message = $row['message'];

    $from = $row['sender'];

    $id = $row['id'];

    $date = $row['date'];

    $sender_image = $row['profile_picture'];

    $new = $row['new'];

    $name = $row['from_name'];

   

    ?>

    <div class="featured">

    <a href="read.php?id=<?php echo $id; ?>"><img src="<?php if($show_image) {echo $sender_image; $show_image == FALSE;} ?>" class="borders" /><div class="borders3"><div class="borders_text"><?php echo $name; ?></div></div></a></div>

    <?php

}

?>

 

 

This should do the trick.

Link to comment
https://forums.phpfreaks.com/topic/263593-mysql-help/#findComment-1350943
Share on other sites

Hi again. Sorry for taking so long to reply.

 

I have this messages table. If a user sends a message to another user then the following will happen:

 

User's username, receiver's username and the message will all be inserted into messages table.

 

After that, the user's image will be displayed in the message list so that the receiver could open it up and read the message.

 

However if the user sends more than one message, then more than one picture will be displayed in the messages list.

 

Here is an example: http://imageshack.us/photo/my-images/528/messp.jpg/

 

I just want one picture displayed in the messages list page, no matter the amount of messages.

Link to comment
https://forums.phpfreaks.com/topic/263593-mysql-help/#findComment-1351850
Share on other sites

Because you are using the image as the link to open each message, what do you want to use as the link when there is more than one message from that user? You must still have a separate link for each message.

 

Sample code that would 'remember' the image name and only output one image each time the image name changes -

<?php
$query = "your query statement goes here...";
$result = mysql_query($query);
$last_image = null; // remember the last image displayed. initialize to a value that will never exist in the data.
while($row = mysql_fetch_assoc($result)){
// detect if the image changed (or is the first one)
if($last_image != $row['your_image_field_name']){
	// output the image
	echo "<img src='{$row['your_image_field_name']}' alt=''>";
	$last_image = $row['your_image_field_name']; // remember the image name
    }
// output the message link for each image here...
}
?>

Link to comment
https://forums.phpfreaks.com/topic/263593-mysql-help/#findComment-1351867
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.