Group Messages by PHP date, Jquery

Asked

Viewed 84 times

3

Main fields MYSQL

id, message, date

I would like it to be displayed this way in the chat

26/02/2019
- message 1

27/02/2019
- message 2
- message 3

In a way I managed to group by date, below is the form I made.

However I could not display in chat.

Until then, what I have, is this in PHP

$messages = Array
(
    [0] => stdClass Object
        (
            [id] => 1
            [from] => 1
            [to] => 2
            [message] => Mensagem 1
            [is_read] => 1
            [time] => 2019-02-26 10:32:55
        )

    [1] => stdClass Object
        (
            [id] => 2
            [from] => 2
            [to] => 1
            [message] => Mensagem 2
            [is_read] => 1
            [time] => 2019-02-27 08:33:40
        )

    [2] => stdClass Object
        (
            [id] => 3
            [from] => 1
            [to] => 2
            [message] => Mensagem 3
            [is_read] => 1
            [time] => 2019-02-27 08:38:20
        )

)

$sorted_date = [];
foreach ($messages as $message)
{
    $chat = [
        'msg'       => $message->id,
        'sender'    => $message->from, 
        'recipient' => $message->to,
        'avatar'    => 'no-image.jpg',
        'body'      => $message->message,
        'time'      => date("M j, Y, g:i a", strtotime($message->time)),
        'type'      => $message->from == $this->meu_id ? 'sender' : 'receiver',
        'name'      => $message->from == $this->meu_id ? 'You' : 0
    ];

    $timestamp = strtotime($message->time);
    $date = date('d-m-Y', $timestamp);
    if ( !isset($sorted_date[$date]) )
    {
        //$sorted_date[$date] = [$message];
        $sorted_date[$date] = [$chat];
        //array_push($sorted_date, $chat);
    }
    else
    {
        //$sorted_date[$date][] = $message;
        $sorted_date[$date][] = $chat;
    }
}

$response = [
    'success' => true,
    'thread'  => $sorted_date
];

header('Content-Type: application/json');
echo json_encode($response);

print_r ($sorted_date);

Array
(
    [26-02-2019] => Array
        (
            [0] => Array
                (
                    [msg] => 1
                    [sender] => 1
                    [recipient] => 2
                    [avatar] => no-image.jpg
                    [body] => Mensagem 1
                    [time] => Feb 26, 2019, 10:32 am
                    [type] => sender
                    [name] => You
                )

        )

    [27-02-2019] => Array
        (
            [0] => Array
                (
                    [msg] => 2
                    [sender] => 2
                    [recipient] => 1
                    [avatar] => no-image.jpg
                    [body] => Mensagem 2
                    [time] => Feb 27, 2019, 8:33 am
                    [type] => receiver
                    [name] => 0
                )

            [1] => Array
                (
                    [msg] => 3
                    [sender] => 1
                    [recipient] => 2
                    [avatar] => no-image.jpg
                    [body] => Mensagem 3
                    [time] => Feb 27, 2019, 8:38 am
                    [type] => sender
                    [name] => You
                )

        )

)

This is what I have on the return of AJAX

success: (function (response) {
thread = response.thread;
$('.message-box').html('');
$.each(thread, function () {
        /* esta parte do código funciona, quando as mensagens não estão agrupadas */
	html = '<div class="message ' + this.type + '">' +
		'   <span>' + this.body + '</span>' +
		'       <span class="metadata">' +
		'       <span class="time">' + this.time + '</span>' +
		'   </span>' +
		'</div>';
	$('.message-box').append(html);
});
}),

Show 2 more comments

2 answers

2


I think pirmeiro you should group the array inside php

My suggestion is this:

Inside your loop, in the function

 if ( !in_array_r( $date, $sorted_date  ))
    {
        $sorted_date[] = array( 
            'data' => $date,
            'message' => array( $chat )
        );
    }
    else
    {
        $key = array_seach_f($date, $sorted_date )  ;          
        $sorted_date[$key]['message'][] = $chat;
    }

And I created two more functions:

function in_array_r($item , $array) {
    return preg_match('/"'.preg_quote($item, '/').'"/i' , json_encode($array));
}
 function array_seach_f( $data, $array )
 {
     $index = -1;
     foreach ($array as $key => $value) {
         if( in_array( $data, $value ) ){
                $index = $key;
                break;
            } 
     }
     return $index;
 }

Hence you can treat this return in jquery

  • It worked for me, thank you..

0

My suggestion is that you use PHP’s already native functions for this, here in this PHP documentation link you will find several examples of using native PHP functions for both strings, arrays, objects and multi arrays and at the bottom of the page there are several examples added by the community itself.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.