0

Can anyone guide me on how to display all messages from my chat database table in WhatsApp format Grouping Messages by Date i.e

[Today]

Janet:How are?

Jon: Am fine and you

[ Yesterday ]

John: Go to bed please

Janet: Okay dear, Good nite

[19 May, 2016 ]

John: Go to bed please

Janet: Okay dear, Good nite

Mysql:

CREATE TABLE IF NOT EXISTS `chat` (
  `id` int(10) unsigned NOT NULL,
  `from` varchar(255) NOT NULL DEFAULT '',
  `to` varchar(255) NOT NULL DEFAULT '',
  `message` text NOT NULL,
  `sent_by` varchar(111) NOT NULL,
  `created` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  `recd` int(10) unsigned NOT NULL DEFAULT '0'
) ENGINE=MyISAM AUTO_INCREMENT=229 DEFAULT CHARSET=latin1;  

PHP:

$result = mysql_query("SELECT *FROM (SELECT * FROM `chat` WHERE `from` = '$my_id' AND `to` = '$chat_with' OR `from` = '$chat_with' AND `to` = '$my_id' ORDER BY `created`  DESC  LIMIT $start, $limit )tmp 
ORDER BY tmp.created ASC") or die(mysql_error());

while($row = mysql_fetch_array($result)){
print"$row['from']";
 echo parse_smileys(make_clickable(nl2br(stripslashes($row['message']))), $smiley_folder);
  print"$row['created'] @ $msg_time";
}
Hamzat Luqman
  • 69
  • 1
  • 10
  • [Little Bobby](http://bobby-tables.com/) says [your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php). Even [escaping the string](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) is not safe! – Jay Blanchard May 20 '16 at 14:02
  • Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard May 20 '16 at 14:02
  • Ok, just help me out, I will convert it to mysqli – Hamzat Luqman May 20 '16 at 14:06
  • WEll first of all you should index messages in the associative array. by index i mean adding them by the date accordingly. using date as the key. and later list them using double loop. foreach key and foreach value in the key – ProblemSlover May 20 '16 at 14:17

0 Answers0