Pain Posted June 3, 2012 Share Posted June 3, 2012 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 } ?> Quote Link to comment https://forums.phpfreaks.com/topic/263593-mysql-help/ Share on other sites More sharing options...
Skewled Posted June 3, 2012 Share Posted June 3, 2012 You just need to write a conditional statement on the output to filter that... I'm not sure you can do that in the query itself. Quote Link to comment https://forums.phpfreaks.com/topic/263593-mysql-help/#findComment-1350910 Share on other sites More sharing options...
Pain Posted June 3, 2012 Author Share Posted June 3, 2012 Not sure how to structure it... ? Quote Link to comment https://forums.phpfreaks.com/topic/263593-mysql-help/#findComment-1350913 Share on other sites More sharing options...
Barand Posted June 3, 2012 Share Posted June 3, 2012 <?php $first = 1; while (...) { if ($first) { echo "<img .... >"; $first = 0; } ... } ?> Quote Link to comment https://forums.phpfreaks.com/topic/263593-mysql-help/#findComment-1350917 Share on other sites More sharing options...
Skewled Posted June 3, 2012 Share Posted June 3, 2012 I sent him a PM, but yes that's what you'd have to do.. Quote Link to comment https://forums.phpfreaks.com/topic/263593-mysql-help/#findComment-1350923 Share on other sites More sharing options...
mustafasr Posted June 3, 2012 Share Posted June 3, 2012 <?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. Quote Link to comment https://forums.phpfreaks.com/topic/263593-mysql-help/#findComment-1350943 Share on other sites More sharing options...
Pain Posted June 7, 2012 Author Share Posted June 7, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/263593-mysql-help/#findComment-1351850 Share on other sites More sharing options...
PFMaBiSmAd Posted June 7, 2012 Share Posted June 7, 2012 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... } ?> Quote Link to comment https://forums.phpfreaks.com/topic/263593-mysql-help/#findComment-1351867 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.