如果某个单元格中的值等于 "Certain Text" echo "Text"

If value in certain cell equals "Certain Text" echo "Text"

我试图让它在单元格包含特定文本(如“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>";
                }

            ?>

您可以使用一点正则表达式 -

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

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

使用 NOT IN 和要拒绝的值列表。无论如何,这是一种选择。 preg 匹配选项也有效,只是它要求 php 完成工作,而这要求 sql 完成。