PHP IF语句有四个条件

时间:2014-03-18 20:57:16

标签: php mysql if-statement

我想检查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。

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:5)

(a == 1 || 2 || 3)

这种结构不正确,因为它会评估1 || 2 || 3true,因为至少有一个......好吧,所有的操作数都是真实的)。

相反,试试这个:

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

答案 1 :(得分:0)

WOW!很高兴你不能在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) ) {