我假设我需要在for循环之前加入两个查询,但最好是优化以下代码:
$LocationArray0 = array();
$LocationArray1 = array();
$LocationArray2 = array();
$LocationArray3 = array();
$LocationArray4 = array();
foreach($Bodyshops as $Bodyshop) {
$Query = "SELECT BodyshopID, BodyshopName, BodyshopLongitude, BodyshopLatitude FROM FusionBodyshops WHERE BodyshopID = '$Bodyshop'";
$result = mysqli_query($db, $Query);
$rows = mysqli_num_rows($result);
if ( $rows != 0 ) {
for ( $i = 0; $i < $rows; $i ++ ) {
$row = mysqli_fetch_array($result, MYSQL_ASSOC);
$BodyshopID = $row['BodyshopID'];
$BodyshopName = $row['BodyshopName'];
$BodyshopLatitude = $row['BodyshopLatitude'];
$BodyshopLongitude = $row['BodyshopLongitude'];
$query2 = "SELECT Tier FROM FusionBodyshopSettings WHERE BodyshopID = '$Bodyshop' AND BusinessID = '$UserCompany'";
$result2 = mysqli_query($db, $query2);
$row2 = mysqli_fetch_array($result2, MYSQL_ASSOC);
if ( $row2['Tier'] == "0") {
array_push($LocationArray0, "'" . str_replace("'", "",$BodyshopName) . "', '" . $BodyshopLatitude . "', '" . $BodyshopLongitude . "'"); //$row['BodyshopID'] . ", " .
}elseif ( $row2['Tier'] == "1") {
array_push($LocationArray1, "'" . str_replace("'", "",$BodyshopName) . "', '" . $BodyshopLatitude . "', '" . $BodyshopLongitude . "'"); //$row['BodyshopID'] . ", " .
}elseif ( $row2['Tier'] == "2") {
array_push($LocationArray2, "'" . str_replace("'", "",$BodyshopName) . "', '" . $BodyshopLatitude . "', '" . $BodyshopLongitude . "'"); //$row['BodyshopID'] . ", " .
}elseif ( $row2['Tier'] == "3") {
array_push($LocationArray3, "'" . str_replace("'", "",$BodyshopName) . "', '" . $BodyshopLatitude . "', '" . $BodyshopLongitude . "'"); //$row['BodyshopID'] . ", " .
}elseif ( $row2['Tier'] == "4") {
array_push($LocationArray4, "'" . str_replace("'", "",$BodyshopName) . "', '" . $BodyshopLatitude . "', '" . $BodyshopLongitude . "'"); //$row['BodyshopID'] . ", " .
}
}
}
}
非常感谢任何帮助!
答案 0 :(得分:1)
您可以使用以下内容替换所有代码:
SELECT a.BodyshopID
, a.BodyshopName
, a.BodyshopLongitude
, a.BodyshopLatitude
, b.Tier
FROM FusionBodyshops a
JOIN FusionBodyshopSettings b
ON b.BodyshopID = a.BodyshopID
WHERE a.BodyshopID IN ($Bodyshops) -- a list of bodyshops
AND b.BusinessID = $UserCompany;
现在看一下准备好的陈述。