<?php
require_once 'config.php';
//$error=false;
$errors=array();
$cities=array();
$thread_id=$_GET['thread_id'];
$query="SELECT * FROM messages WHERE thread_id='".$thread_id."'";

$row = mysqli_query($con,$query);
     
    if (mysqli_num_rows($row) >= 1) {
		//$r=mysqli_fetch_array($row)	;
			
		 while($r=mysqli_fetch_array($row)){
			 $user=mysqli_fetch_array(mysqli_query($con,"SELECT * FROM users WHERE id=".$r['to_user']));
			 $first_name=$user['first_name'];
   $last_name=$user['last_name'];
   $to_user_name=$first_name.' '.$last_name;
			$cities[]=['id'=>$r['id'],'thread_id'=>$r['thread_id'],'from_user'=>$r['from_user'],'to_user'=>$r['to_user'],'message'=>$r['message'],'created_at'=>$r['created_at'],'status'=>$r['status'],'to_user_name'=> $to_user_name];
			
			
		}   
		$errors['messages']=$cities;
		$errors['success']='messages data';
    } else {
        
		$errors['success']='There is no messages found';
    }
	echo json_encode($errors);
?>