geatzo Posted April 30, 2023 Share Posted April 30, 2023 Im trying to make a php message script i can output the message's but im having a issue with grouping them Here is a photo of the html https://prnt.sc/WtJhNnteYaq1 But im outputting the message's and if the same person sends 2 message's it shows has separate https://prnt.sc/xSAIT9cQlHt3 I can ether just output all the from usernames on the left side then when the user clicks a chat head the messege will do a simple search where that username but again it will show muti chat heads for the same users Here is my table https://prnt.sc/0hRyCNozTlpZ $stmt2m2 = $db->prepare('SELECT * FROM messages WHERE recipient_username = ? LIMIT 4 '); $stmt2m2->execute( array($_SESSION['username'] ) ) ; $row2m2 = $stmt2m2->fetchAll(); foreach($row2m2 as $users2) { ?> <a href="#" class="media"> <div class="item-img"> <img src="media/figure/notifiy_1.png" alt="Notify"> </div> <div class="media-body"> <h6 class="item-title"><?php echo $users2['sender_username'] ; ?></h6> <div class="item-time"><?php echo $users2['date'] ; ?></div> <p><?php echo $users2['message'] ; ?></p> </div> </a> <?php } ?> Quote Link to comment https://forums.phpfreaks.com/topic/316224-pdo-group-by/ Share on other sites More sharing options...
Barand Posted April 30, 2023 Share Posted April 30, 2023 A couple of ways to handle this situation where you want name from from first record only and message from all records. 1 ) Use a variable so you know you are on the first record $first = 1; foreach ($row2 as $r) { if ($first) { // output name header here $first = 0; } // output message here } 2 ) fetch first record then use a do..while loop $r = $stmt->fetch(); // output name header here do { // output message here while ($r = $stmt->fetch()); Quote Link to comment https://forums.phpfreaks.com/topic/316224-pdo-group-by/#findComment-1607832 Share on other sites More sharing options...
geatzo Posted April 30, 2023 Author Share Posted April 30, 2023 18 minutes ago, Barand said: A couple of ways to handle this situation where you want name from from first record only and message from all records. 1 ) Use a variable so you know you are on the first record $first = 1; foreach ($row2 as $r) { if ($first) { // output name header here $first = 0; } // output message here } 2 ) fetch first record then use a do..while loop $r = $stmt->fetch(); // output name header here do { // output message here while ($r = $stmt->fetch()); The part im stuck on is displaying all the users who messaged so lets say i have 2 mails from user1 and 1 mail; from user 2 my coding will show all 3 mails. What i need is for my coding to show 2 mails because of course there are 2 users mailing me not 3 ( even tho there are 3 results) i hope this makes sense. So i simply trying to find a way to display the users who mailed me but at the moment its showing user1 user1 user2 instead i need it to show user1 user2 its showing user1 twice because there are 2 messages from user 1 Quote Link to comment https://forums.phpfreaks.com/topic/316224-pdo-group-by/#findComment-1607833 Share on other sites More sharing options...
Barand Posted April 30, 2023 Share Posted April 30, 2023 Something along these lines (I don't know your data) ... SELECT DISTINCT username FROM messages WHERE message_type = 'email' Quote Link to comment https://forums.phpfreaks.com/topic/316224-pdo-group-by/#findComment-1607835 Share on other sites More sharing options...
geatzo Posted April 30, 2023 Author Share Posted April 30, 2023 (edited) 39 minutes ago, Barand said: Something along these lines (I don't know your data) ... SELECT DISTINCT username FROM messages WHERE message_type = 'email' so $stmt2m2 = $db->prepare('SELECT * (DISTINCT recipient_username) FROM messages WHERE recipient_username = ? LIMIT 4 '); My data is posted in the first post Edited April 30, 2023 by geatzo Quote Link to comment https://forums.phpfreaks.com/topic/316224-pdo-group-by/#findComment-1607838 Share on other sites More sharing options...
Barand Posted May 1, 2023 Share Posted May 1, 2023 9 hours ago, geatzo said: My data is posted in the first post I don't accept links from strangers, nor will many others here. Quote Link to comment https://forums.phpfreaks.com/topic/316224-pdo-group-by/#findComment-1607844 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.