<?php
session_start();
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');
header("Access-Control-Allow-Headers: X-Requested-With");
header('Content-Type: application/json; charset=utf-8');


if(isset($_POST['user_id']))
{
require 'config.php';


function check_input($par) {
  $par = trim($par);
  $par = stripslashes($par);
  $par = htmlspecialchars($par);
  return $par;
}



$auth=check_input($_POST['auth']);
$user_id=check_input($_POST['user_id']);
$post_id=check_input($_POST['post_id']);


$auth = mysqli_escape_string($con, $auth);
$user_id=mysqli_escape_string($con, $user_id);
$post_id=mysqli_escape_string($con, $post_id);



$sql="SELECT * FROM users WHERE user_id = '$user_id' AND auth = '$auth'";
$result=$con->query($sql) or die ("error: Server error ".mysqli_error($con));
$count=mysqli_num_rows($result);

if ($count == 0)
{
        $suc = "User Does Not Exist"; 
         $status = false; 

}
else
{

$rows = mysqli_fetch_array($result);
$phone = $rows['phone'];
$email = $rows['email'];
$firstname = $rows['firstname'];
$lastname = $rows['lastname'];




$sqlx="SELECT * FROM comments WHERE post_id = '$post_id' ORDER BY id DESC LIMIT 0, 50";
$resultx=$con->query($sqlx) or die ("error: Server error ".mysqli_error($con));

$countx=mysqli_num_rows($resultx);
if ($countx >= 1)
{

$data = array();

while($rowx=mysqli_fetch_assoc($resultx))
   {
  
$poster_id = $rowx['user_id'];

$sqlz="SELECT * FROM users WHERE user_id = '$user_id'";
$resultz=$con->query($sqlz) or die ("error: Server error ".mysqli_error($con));
$rowz = mysqli_fetch_array($resultz);
$poster_id = $rowz['user_id'];
$pro_pic = $rowz['pro_pic'];


      
    $data[] = $rowx;
 
 
    $suc="Success";
   
   }
}
else
{
  $suc="No Comment on this post";
  $data = ''; 
    
}
}
}

$con->close();

header('Content-Type: application/json; charset=utf-8');
$output = array('success'=>$suc, 
 'comments'=> $data);
echo json_encode($output); //output JSON data
?>

