<?php
require_once ("varables.php");

$myJSON['comments'] = array();
$myJSON['success']="0";
if(isset($_POST["nekot"]) && isset($_POST["an1d"]))
{
	$id =$_POST["an1d"];
    $connect = @mysqli_connect($hostname,$username,$password,$database);
    if($connect)
    {
		@mysqli_query($connect,"SET CHARACTER SET utf8mb4;");
		if($id==0){
			$query_all = "SELECT  tbl_comments.confirm,tbl_comments.pid,tbl_comments.id,tbl_comments.comment,tbl_comments.star,tbl_comments.cdate,".$tbl_customer.".C_name,".$tbl_products.".P_name FROM tbl_comments,".$tbl_customer.",".$tbl_products." WHERE  tbl_comments.userid = ".$tbl_customer.".id AND tbl_comments.pid = ".$tbl_products.".id AND tbl_comments.confirm ='0'   ORDER BY tbl_comments.confirm, id DESC";
			$allres = @mysqli_query($connect,$query_all);
			$star=0;
			if($allres){
				$cc = 0;
				while($row = @mysqli_fetch_assoc($allres)){
				array_push($myJSON["comments"], $row);
				$cc++;
				}
				
				$myJSON['success']="200";
				if($cc ==0){
					$query_all = "SELECT  tbl_comments.confirm,tbl_comments.pid,tbl_comments.id,tbl_comments.comment,tbl_comments.star,tbl_comments.cdate,".$tbl_customer.".C_name,".$tbl_products.".P_name FROM tbl_comments,".$tbl_customer.",".$tbl_products." WHERE tbl_comments.pid = ".$tbl_products.".id AND tbl_comments.userid = ".$tbl_customer.".id  ORDER BY tbl_comments.confirm, id DESC LIMIT 40";
					$allres = @mysqli_query($connect,$query_all);
					$star=0;
					if($allres){
					$cc = 0;
					while($row = @mysqli_fetch_assoc($allres)){
					array_push($myJSON["comments"], $row);
					}
					
					$myJSON['success']="200";
					}
					else{
						$myJSON['success']="خطایی رخ داده...\n".@mysqli_error($connect);
					}
				}	
			}
			else
			$myJSON['success']="خطایی رخ داده...\n".@mysqli_error($connect);
		}	
		else{
			$query_all = "SELECT  tbl_comments.confirm,tbl_comments.pid,tbl_comments.id,tbl_comments.comment,tbl_comments.star,tbl_comments.cdate,".$tbl_customer.".C_name,".$tbl_products.".P_name FROM tbl_comments,".$tbl_customer.",".$tbl_products." WHERE tbl_comments.pid = '".$id."' AND tbl_comments.userid = ".$tbl_customer.".id AND tbl_comments.pid = ".$tbl_products.".id AND tbl_comments.pid = '".$id."'   ORDER BY tbl_comments.confirm, id DESC";
			$allres = @mysqli_query($connect,$query_all);
			$star=0;
			if($allres){
				while($row = @mysqli_fetch_assoc($allres)){
				array_push($myJSON["comments"], $row);
				}
				$myJSON['success']="200";
			}
			else
				$myJSON['success']="خطایی رخ داده...\n".@mysqli_error($connect);
		}
	}
}
echo json_encode($myJSON);
?>
