<?php
/**
 * Created by PhpStorm.
 * User: jaskokoyn
 * Date: 4/28/2015
 * Time: 7:14 PM
 */

include( '../inc/db.php' );
include( '../inc/functions.php' );

$params                     =   getParams();
$output                     =   array();

$term                       =   secure($params['term']);
$sort                       =   intval($params['sort']);
$sort                       =   $sort == 1 ? "ORDER BY `last_message` DESC" : "ORDER BY `id` DESC";

$roomsQuery                 =   $db->prepare("SELECT `id`,`title`,`chat_desc` FROM chats
                                              WHERE title LIKE :t " . $sort . " LIMIT 25");
$roomsQuery->execute(array(
    ":t"                    =>  '%' . $term . '%'
));
$roomsRow                   =   $roomsQuery->fetchAll(PDO::FETCH_ASSOC);
$output['results']          =   $roomsRow;
dj($output);