因此,在从数据库中选择一些列和行之后,我必须将它们放入数组中,将它们放入数组中,我使用 foreach 循环,并且它起作用了。然后,当它遍历查询结果时,我需要执行一些查询,这些查询基于查询结果的价值,关于循环仍在工作的位置。但我出错了。这是我的代码。你们能帮忙解决这个问题吗?我是 PHP 新手,所以我不知道要修复它。
<?php
require("config.php");
$whatvenue= $_POST['whatvenue'];
$showby= $_POST['showedby'];
if($whatvenue==="hotel")
{
$query = "Select * FROM venues where venue_category like :whatvenue";
}
else if($whatvenue==="restoran")
{
$query = "Select * FROM venues where venue_category like :whatvenue";
}
else
{
$query = "Select * FROM venues where venue_category like :whatvenue";
}
$query_params = array(
':whatvenue' => $_POST['whatvenue']
);
try
{
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch (PDOException $ex)
{
$response["success"] = 0;
$response["message"] = "Database Error!";
die(json_encode($response));
}
$queryreviews ="SELECT COUNT( VENUE_ID) AS TOTALREVIEWER FROM COMMENT WHERE VENUE_ID =:venue_id";
$queryrsb ="SELECT COUNT( USER_ID ) AS TOTALREVIEWERSB FROM COMMENT WHERE VENUE_ID =:venue_id AND REVIEW_RATE =5";
$queryrb ="SELECT COUNT( USER_ID ) AS TOTALREVIEWERB FROM COMMENT WHERE VENUE_ID =:venue_id AND REVIEW_RATE =4";
$queryrc ="SELECT COUNT( USER_ID ) AS TOTALREVIEWERC FROM COMMENT WHERE VENUE_ID =:venue_id AND REVIEW_RATE =3";
$queryrk ="SELECT COUNT( USER_ID ) AS TOTALREVIEWERK FROM COMMENT WHERE VENUE_ID =:venue_id AND REVIEW_RATE =2";
$queryrsk ="SELECT COUNT( USER_ID ) AS TOTALREVIEWERSK FROM COMMENT WHERE VENUE_ID =:venue_id AND REVIEW_RATE =1";
$rows = $stmt->fetchAll();
if ($rows)
{
$response["success"] = 1;
$response["message"] = "Venue Ditampilkan!";
$response["venues"] = array();
foreach ($rows as $row) {
$venues = array();
$venues["venue_id"] = $row["VENUE_ID"];
$query_paramsr = array(
':venue_id' => $venues["venue_id"]
);
try
{
$stmtr = $db->prepare($queryreviews);
$resultr = $stmtr->execute($query_paramsr);
$stmtrsb = $db->prepare($queryrsb);
$resultsb = $stmtrsb->execute($query_paramsr);
$stmtrb = $db->prepare($queryrb);
$resultrb = $stmtrb->execute($query_paramsr);
$stmtrc = $db->prepare($queryrc);
$resultrc = $stmtrc->execute($query_paramsr);
$stmtrk = $db->prepare($queryrk);
$resultrk = $stmtrk->execute($query_paramsr);
$stmtrsk = $db->prepare($queryrsk);
$resultrsk = $stmtrsk->execute($query_paramsr);
}
catch (PDOException $ex)
{
$response["success"] = 0;
$response["message"] = "Database Error!";
die(json_encode($response));
}
$rowsr = $stmtr ->fetch();
$rowsrsb = $stmtrsb->fetch();
$rowsrb = $stmtrb->fetch();
$rowsrc = $stmtrc->fetch();
$rowsrk = $stmtrk->fetch();
$rowsrsk = $stmtrsk->fetch();
$venues["venue_totalreviwer"] = $rowsr ["TOTALREVIEWER"];
$venues["venue_totalreviwersb"] = $rowsrsb["TOTALREVIEWERSB"];
$venues["venue_totalreviwerb"] = $rowsrb ["TOTALREVIEWERB"];
$venues["venue_totalreviwerc"] = $rowsrc["TOTALREVIEWERC"];
$venues["venue_totalreviwerk"] = $rowsrk["TOTALREVIEWERK"];
$venues["venue_totalreviwersk"] = $rowsrsk["TOTALREVIEWERSK"];
$venues["venue_name"] = $row["VENUE_NAME"];
$venues["venue_address"] = $row["VENUE_ADDRESS"];
$venues["venue_desc"] = $row["VENUE_DESC"];
$venues["venue_lat"] = $row["VENUE_LAT"];
$venues["venue_long"] = $row["VENUE_LONG"];
$venues["venue_category"] = $row["VENUE_CATEGORY"];
$venues["venue_phone"] = $row["VENUE_PHONE"];
$venues["venue_email"] = $row["VENUE_EMAIL"];
$venues["venue_website"] = $row["VENUE_WEBSITE"];
$venues["venue_picture"] = $row["VENUE_PICTURE"];
$venues["venue_rating"] = $row["VENUE_RATING"];
//update our repsonse JSON data
array_push($response["venues"], $venues);
}
// echoing JSON response
echo json_encode($response);
}
else
{
$response["success"] = 0;
$response["message"] = "Venue yang diminta tidak tersedia";
die(json_encode($response));
}
?>