我正在开发webservice。 在请求类别中,ID类似于
$categories_string = 1,2,3,4,5.
作为回应,我必须返回与所有id相关的整个数据(来自3-4个表)的json数组。 所以我对字符串到数组使用explode()函数。所以在这段代码之后我的数组是,
$category_id_array = explode(',',$categories_string );
所以,我在$ category_id_array中得到了数组。
问题是,如果单个类别中有数据,即使其他类别中没有数据,我也必须给出响应。
当前输出: 从这个代码中,如果有4个类别中的3个类别的数据,它将给出(数据3和第4个没有数据,所以它将转到else块) 那么有没有办法检查是否一旦转到if块永远不会转到else块或此功能的任何其他逻辑。
预期输出 功能是在4个类别中,如果一个类别中有数据而不是其他类别,则仅显示第一类别数据,如果数据在2个类别中,则将提供2个类别的数据,反之亦然。
注意:我必须在这里使用else阻止(如果所有类别中没有数据,则必须使用else阻止响应)
因此,我使用下面的代码。
foreach ($category_id_array as $single_cat_value) {
$res_whole_criteria = $this->db->query("SELECT * , ( 3959 * ACOS( COS( RADIANS( '$latitude' ) ) * COS( RADIANS( latitude ) ) * COS( RADIANS( longitude ) - RADIANS( '$longitude' ) ) + SIN( RADIANS( '$latitude' ) ) * SIN( RADIANS( latitude ) ) ) ) AS distance, information_business.name AS business_name FROM information_business LEFT JOIN category ON information_business.category_id = category.id LEFT JOIN offers ON offers.category_id = category.id WHERE category.id = '$single_cat_value' HAVING distance < 10 ORDER BY distance");
$row_whole_criteria = $res_whole_criteria->result();
echo "<pre>";
print_r($row_whole_criteria);
$resultsDoExist = 'false'; // code edited
$business_id = $row_whole_criteria['0']->business_id;
// code for count business_wise_like starts here
$like_count_businesswise = $this->db->query("SELECT COUNT( id ) AS business_wise_like FROM `business_likes` WHERE business_id = '$business_id'");
$row_like_businesswise = $like_count_businesswise->result();
$businesswisecount = $row_like_businesswise['0']->business_wise_like;
// code for count business_wise_like ends here
if($res_whole_criteria -> num_rows > 0)
{
$resultsDoExist = 'true'; // code edited
$response['success'] = "1";
$response['message'] = "List found for near by Business Data";
$response["data"] = array();
$counter = 0;
foreach($row_whole_criteria as $row)
{
$data = array();
$offerimage = $row->image;
$businessimage = $row->business_image;
$data['Primary Offer Discount'] = $row->discount;
$data['Offer Image'] = $_SERVER["HTTP_HOST"]."/dealsnow/public/images/CATEGORIES/"."$offerimage";
$data['Business Type'] = $row->name;
$data['Business Name'] = $row->business_name;
$data['Business Information']=$row->business_desc;
$data['Business Image'] = $_SERVER["HTTP_HOST"]."/dealsnow/public/images/business_image/"."$businessimage";
$data['Offer Name'] = $row->title;
$data['Offer Address'] = $row->address;
$data['Phone Number'] = $row->mobile;
$data['Original Price'] = $row->original_price;
$data['business Id'] = $row->business_id;
$data['Expiry Date']= $row->end_date;
$data['Latitude']= $row->latitude;
$data['Longitude']= $row->longitude;
$data['Like Status']= '0';
$data['Favorite Status']= '1';
$data['Like Count']= $businesswisecount;
array_push($response["data"], $data);
$counter++;
}
echo $output2 = json_encode(array('responsedata' => $response));
}
else
{
$response = array();
$response ["success"] = 0;
$response ["message"] = "Error.";
$output2 = json_encode(array('responsedata' => $response));
echo $output2;
}
}
编辑:
foreach() // main foreach
{
$resultsDoExist = 'false';
if($res -> num_rows > 0)
{
$resultsDoExist = 'true';
if($resultsDoExist = 'false')
{
foreach ($variable as $key) { // sub foreach
// data
}
}
if($resultsDoExist == 'false')
{
echo "no data found";
}
}
}
答案 0 :(得分:0)
这个代码适用于第一个类别不返回任何东西然后返回到else,如果阻止其他方面没有。
foreach ($category_id_array as $single_cat_value) {
$res_whole_criteria = $this->db->query("SELECT * , ( 3959 * ACOS( COS( RADIANS( '$latitude' ) ) * COS( RADIANS( latitude ) ) * COS( RADIANS( longitude ) - RADIANS( '$longitude' ) ) + SIN( RADIANS( '$latitude' ) ) * SIN( RADIANS( latitude ) ) ) ) AS distance, information_business.name AS business_name FROM information_business LEFT JOIN category ON information_business.category_id = category.id LEFT JOIN offers ON offers.category_id = category.id WHERE category.id = '$single_cat_value' HAVING distance < 10 ORDER BY distance");
$row_whole_criteria = $res_whole_criteria->result();
echo "<pre>";
print_r($row_whole_criteria);
$resultsDoExist = 'false'; // code edited
$business_id = $row_whole_criteria['0']->business_id;
// code for count business_wise_like starts here
$like_count_businesswise = $this->db->query("SELECT COUNT( id ) AS business_wise_like FROM `business_likes` WHERE business_id = '$business_id'");
$row_like_businesswise = $like_count_businesswise->result();
$businesswisecount = $row_like_businesswise['0']->business_wise_like;
// code for count business_wise_like ends here
if($res_whole_criteria -> num_rows > 0)
{
$resultsDoExist = 'true'; // code edited
$response['success'] = "1";
$response['message'] = "List found for near by Business Data";
$response["data"] = array();
$counter = 0;
foreach($row_whole_criteria as $row)
{
$data = array();
$offerimage = $row->image;
$businessimage = $row->business_image;
$data['Primary Offer Discount'] = $row->discount;
$data['Offer Image'] = $_SERVER["HTTP_HOST"]."/dealsnow/public/images/CATEGORIES/"."$offerimage";
$data['Business Type'] = $row->name;
$data['Business Name'] = $row->business_name;
$data['Business Information']=$row->business_desc;
$data['Business Image'] = $_SERVER["HTTP_HOST"]."/dealsnow/public/images/business_image/"."$businessimage";
$data['Offer Name'] = $row->title;
$data['Offer Address'] = $row->address;
$data['Phone Number'] = $row->mobile;
$data['Original Price'] = $row->original_price;
$data['business Id'] = $row->business_id;
$data['Expiry Date']= $row->end_date;
$data['Latitude']= $row->latitude;
$data['Longitude']= $row->longitude;
$data['Like Status']= '0';
$data['Favorite Status']= '1';
$data['Like Count']= $businesswisecount;
array_push($response["data"], $data);
$counter++;
}
echo $output2 = json_encode(array('responsedata' => $response));
}
if( $resultsDoExist=='false')
{
$response = array();
$response ["success"] = 0;
$response ["message"] = "Error.";
$output2 = json_encode(array('responsedata' => $response));
echo $output2;
}
}