【发布时间】:2014-03-18 20:57:15
【问题描述】:
我想检查 php if 语句中的四个条件?例如
If ((a == 1 || 2 || 3) && (b == 1 || 2 || 3) && (c == 1 || 2 || 3) && (d == 1 || 2 || 3)) {
.... Execute query
}
这是我目前在代码中使用的:
<?php
include 'dbconnect.php';
//check if variable is set
if ( isset($_POST['beaches'], $_POST['species']) && !empty($_POST['beaches'] && !empty($_POST['species']))) {
$beachid = strtolower($_POST['beaches']);
echo '<br>';
$speciesid = strtolower($_POST['species']);
echo '<br>';
//$fishmethodid = strtolower($_POST['fish_method']);
echo '<br>';
//if ( isset($_POST['species']) && !empty($_POST['species'])) {
// echo $speciesid = $_POST['species'];
//if ( isset($_POST['fish_method']) && !empty($_POST['fish_method'])) {
// echo $fishmethodid = $_POST['fish_method'];
// die();
//query begins
if ($beachid == '1' || $beachid == '2' || $beachid == '3'|| $beachid == '4'|| $beachid == '5' || $beachid == '6' || $beachid == '7' || $beachid == '8' || $beachid == '9' || $beachid == '10' || $beachid == '11' || $beachid == '13' || $beachid == '14' || $beachid == '15' || $beachid == '16' || $beachid == '17' || $beachid == '18' || $beachid == '19' || $beachid == '20' || $beachid == '21' || $beachid == '22' && ($speciesid == '1' || $speciesid == '2' || $speciesid == '3' || $speciesid == '4' || $speciesid == '5' || $speciesid == '6' || $speciesid == '7')) {
$query = mysql_query("SELECT `beach`.`beach_description`, `species_description`, `fishmethod`.`fishmet_description`, `weight`, `price`, `date`
FROM `returnlanded`
INNER JOIN `beach` ON `returnlanded`.`beachid` = `beach`.`beach_id`
INNER JOIN `species` ON `returnlanded`.`speciesid` = `species`.`species_id`
INNER JOIN `fishmethod` ON `returnlanded`.`fishmethodid` = `fishmethod`.`fishmet_id`
WHERE `returnlanded`.`beachid`='$beachid' AND `returnlanded`.`speciesid`='$speciesid'");//" ORDER BY returnlanded.`id` ";
if (mysql_num_rows($query) > 0) {
$resp["catch"] = array();
while ($query_row = mysql_fetch_array($query)) {
$weight = $query_row['weight'];
$price = $query_row['price'];
$fishmethodid = $query_row['fishmet_description'];
$date = $query_row ['date'];
$catch = array();
$catch["$beachid"] = $query_row['beach_description'];
$catch["$speciesid"] = $query_row['species_description'];
$catch["$fishmethodid"] = $query_row['fishmet_description'];
$catch["$weight"] = $query_row['weight'];
$catch["$price"] = $query_row['price'];
$catch["$date"] = $query_row['date'];
array_push($resp["catch"], $catch);
}
//if successful
$resp["success"] = 1;
echo json_encode($resp);
echo "<table border='1'><tr><th>Date</th><th>Location</th><th>Fish Species</th><th>Fishing Method (Gear)</th><th>Weight (lbs)</th><th>Price($)</th>";
echo "<tr>";
echo "<td>".$catch["$date"]."</td>";
echo "<td>".$catch["$beachid"]."</td>";
echo "<td>".$catch["$speciesid"]."</td>";
echo "<td>".$catch["$fishmethodid"]."</td>";
echo "<td>".$catch["$weight"]."</td>";
echo "<td>".$catch["$price"]."</td>";
echo "</tr>";
echo "</table>";
echo'<br>';
} else {
$resp["success"] = 0;
$resp["display"] = "No data found";
json_encode($resp);
echo '<br>';
echo "No results found for your search";
echo '<br>';
echo '<br>';
}
}
}
include 'templates/searchagain.php';
include 'templates/footer.php';
?>
但是,该查询仅在我使用最多两个条件($speciesid 和 $beachid)时才有效。我做了一些研究,但我没有找到任何可以适应这四个条件的东西。我想在 if 语句中包含 $fishmethodid 和 $date。
有人可以帮忙吗?
【问题讨论】:
-
当您检查 beachid 和 speciesid 时,您可以将它们解析为整数,然后检查它是否小于 22 和 7。即:if( (beachid
-
你也可能缺少一两个括号,比如
...$beachid == '22' && ($speciesid == '1'... -
if语句中的条件数量没有技术限制。但是,正如这里所提到的,还有其他控制结构可能更易于阅读和维护。检查switch语句或in_array。至于您的代码,如果它没有按预期工作,那么问题不在于if语句中的条件数量,而在于它们的排列方式。检查你的语法!不需要所有条件都在同一行。
标签: php mysql if-statement