按城市搜索&我的数据库里有哪些城市和酒店的酒店。我的代码没有用。你能查一下我的代码吗?也许代码中有些错误。
$countryName = $_GET['countrycode'];
$url = 'https://api.sandbox.amadeus.com/v1.2/hotels/search-box?apikey=EbdwHszYlp0f7fQB6YHo40SIhu4ZAomm&south_west_corner=51.328476%2C%20-10.758522&north_east_corner=55.320949%2C%20-5.474099&check_in=2017-06-19&check_out=2017-06-29&number_of_results=1000';
$response = file_get_contents($url);
$apiResults = json_decode($response, true);
$apiResults = $apiResults["results"];
$finalArray = [];
$dbResults = $wpdb->get_results(
$wpdb->prepare("
SELECT *
FROM hotels
WHERE county = %s",
$countryName
),
ARRAY_A
);
# If atleast 1 row is fecthed from
# database
if ($dbResults->num_rows) {
foreach ($dbResults as $dbResult) {
echo $storedCountryName = $dbResult["county"];
echo $storedHotelName = $dbResult["hotel"];
foreach ($apiResults as $apiResult) {
if ($storedCountryName === $apiResult['address']['city']
&& $storedHotelName === $apiResult['property_name'])
{
$finalArray[] = $apiResult;
}
}
}
}
var_dump($finalArray);
的var_dump($ finalArray);结果:
array (size=0)
empty
答案 0 :(得分:2)
尝试
if ($storedCountryName == $apiResult['address']['city'] && $storedHotelName == $apiResult['property_name'])
{
$finalArray[] = $apiResult;
}
===
表示您也在检查数据类型。您的数据库字段数据类型和json响应字符串数据类型可能不匹配。因此,从条件中删除一个=并仅比较值。
答案 1 :(得分:0)
试试这个并告诉我。
我将此行if ($dbResults->num_rows) {
替换为if ($wpdb->num_rows>0) {
。
它应该有用。
$countryName = $_GET['countrycode'];
$url = 'https://api.sandbox.amadeus.com/v1.2/hotels/search-box?apikey=EbdwHszYlp0f7fQB6YHo40SIhu4ZAomm&south_west_corner=51.328476%2C%20-10.758522&north_east_corner=55.320949%2C%20-5.474099&check_in=2017-06-19&check_out=2017-06-29&number_of_results=1000';
$response = file_get_contents($url);
$apiResults = json_decode($response, true);
$apiResults = $apiResults["results"];
$finalArray = [];
$dbResults = $wpdb->get_results(
$wpdb->prepare("
SELECT *
FROM hotels
WHERE county = %s",
$countryName
),
ARRAY_A
);
# If atleast 1 row is fecthed from
# database
if ($wpdb->num_rows>0) {
foreach ($dbResults as $dbResult) {
echo $storedCountryName = $dbResult["county"];
echo $storedHotelName = $dbResult["hotel"];
foreach ($apiResults as $apiResult) {
$ResultArray= array();
if ($storedCountryName == $apiResult['address']['city']
&& $storedHotelName == $apiResult['property_name'])
{
$ResultArray = $apiResult;
$finalArray[] = $ResultArray;
}
}
}
}
var_dump($finalArray);