<?php
require_once 'config.php';
//$error=false;
$errors=array();
$cities=array();
$from_user=$_GET['user_id'];
$query="SELECT * FROM chat_thread WHERE from_user='".$from_user."' OR  to_user ='".$from_user."'";

$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'],'from_user'=>$r['from_user'],'to_user'=>$r['to_user'],'created_at'=>$r['created_at'],'status'=>$r['status'],'to_user_name'=> $to_user_name];
			
			
		}   
		$errors['threads']=$cities;
		$errors['success']='threads data';
    } else {
        
		$errors['success']='There is no thread found';
    }
	echo json_encode($errors);
?>