【问题标题】:PHP IF statement with four conditions [closed]具有四个条件的 PHP IF 语句 [关闭]
【发布时间】: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' &amp;&amp; ($speciesid == '1'...
  • if 语句中的条件数量没有技术限制。但是,正如这里所提到的,还有其他控制结构可能更易于阅读和维护。检查switch 语句或in_array。至于您的代码,如果它没有按预期工作,那么问题不在于if 语句中的条件数量,而在于它们的排列方式。检查你的语法!不需要所有条件都在同一行。

标签: php mysql if-statement


【解决方案1】:
(a == 1 || 2 || 3)

这种结构是不正确的,因为它会计算1 || 2 || 3(即true,因为至少有一个……嗯,所有的操作数都是真的)。​​ p>

试试这个:

in_array($a,range(1,3))

【讨论】:

  • 谢谢!...我现在唯一的问题是,当我查询具有相同值的行时,输出表中只显示一个结果。但是 json 输出显示所有结果
【解决方案2】:

哇!很高兴您没有在 1 到 100 之间进行检查。怎么样:

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) ) {

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-09
    • 1970-01-01
    • 2014-10-20
    • 1970-01-01
    • 2016-07-15
    • 2023-03-31
    • 2017-10-03
    相关资源
    最近更新 更多