如果某个单元格中的值等于"某些文本" echo" Text"

时间:2015-02-02 19:12:18

标签: php database if-statement row

当单元格包含某些类似于" 0"的文本时,我试图让它回显一条警告消息。或" N / A"。当首先没有输入值时它会起作用,但是当已经存在某个值时,我无法回复它。任何帮助都会很棒。谢谢!

<?php
                $listing = "$_POST[listing]";

                $sql = "SELECT open_house_attended FROM property_flyers WHERE street_property = '$listing' ";
                $result = $conn->query($sql);

                if ($result->num_rows > 0) {
                    // output data of each row
                    while($row = $result->fetch_assoc()) {
                    echo "</span><span class='report_bignumber'><br>".  $row["open_house_attended"]."</span>";
                    }
                } else {
                    echo "<br> ". $noresults . "</span>";
                }

            ?>

2 个答案:

答案 0 :(得分:1)

你可以使用一点regex -

if ($result->num_rows > 0) {
   // output data of each row
   while($row = $result->fetch_assoc()) {
      preg_match(trim($row["open_house_attended"]), '/[0]|[N\/A]/', $matches); // try to match '0' or 'N/A'
      if(count($matches) == 0) { // do we have matches?
          echo "</span><span class='report_bignumber'><br>".  $row["open_house_attended"]."</span>";
      } else {
           echo "<br> ". $noresults . "</span>";
      }
   }
}
?>

或者你可以更直接地去 -

if ($result->num_rows > 0) {
   // output data of each row
   while($row = $result->fetch_assoc()) {
      $ohCount = $row["open_house_attended"];
      if(( $ohCount != '0') && ($ohCount != 'N/A')) { // do we have matches?
          echo "</span><span class='report_bignumber'><br>".  $ohCount ."</span>";
      } else {
           echo "<br> ". $noresults . "</span>";
      }
   }
}
?>

preg_match()

答案 1 :(得分:0)

$sql = "SELECT open_house_attended FROM property_flyers WHERE street_property = '$listing' AND open_house_attended NOT IN ('0', 'N/A')";

使用NOT IN和值列表拒绝。无论如何,这是一个选择。 preg match选项也可以,只是它要求php完成工作,这就要求sql这样做。