带有四个条件的PHP IF语句
我想检查php if语句中的四个条件?e、 g带有四个条件的PHP IF语句,php,mysql,if-statement,Php,Mysql,If Statement,我想检查php if语句中的四个条件?e、 g 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 ((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';
?>
这种结构是不正确的,因为它将计算1 | | 2 | | 3
(这是true
,因为至少有一个…嗯,所有操作数都是真实的)
相反,请尝试以下方法:
in_array($a,range(1,3))
哇!很高兴你没有检查1到100之间。那么:
if ($beachid >= 1 && $beachid <= 22 && $speciesid >= 1 && $speciesid <= 7) {
如果($beachid>=1&&$beachid=1&&$speciesid=1&&$beachid=1&&&$speciesid=0&$beachid<23)&($speciesid>0&$speciesid<8)){
当您检查beachid和speciesid时,您可以将它们解析为整数,只需检查它们是否小于22和7。即:如果((beachid<22)&&&(speciesid<7)),您可能也缺少一个或两个括号,比如…$beachid=='22'&&($speciesid==“1”…
对于if
语句中的条件数量没有技术限制。但是,正如前面提到的,还有其他一些控制结构可能更易于阅读和维护。请检查switch
语句,或\u array
中的。对于您的代码,如果它没有按照您的方式工作,请检查此处的代码假设这样,问题不在于if
语句中条件的数量,而在于它们的排列方式。检查语法!不要求所有条件都在同一行上。谢谢!…我现在唯一的问题是,当我查询具有相同值的行时,输出表中只显示一个结果但是json输出会显示所有结果
if ($beachid >= 1 && $beachid <= 22 && $speciesid >= 1 && $speciesid <= 7) {
if ( ($beachid >= 1 && $beachid <= 22) && ($speciesid >= 1 && $speciesid <= 7) ) {
if ( ($beachid > 0 && $beachid < 23) && ($speciesid > 0 && $speciesid < 8) ) {