Jump to content

Pdo Group by


geatzo

Recommended Posts

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  } ?>

 

Link to comment
Share on other sites

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());

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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