简体   繁体   English

PHP查询结果=空,然后div隐藏

[英]PHP query result = empty, then div hide

I have a calculator function that displays the result when a user enters a query in a div (calcanswer) but often the query is not a calculation request, so the result is "query=" in which query represents the user input, and then nothing behind the = sign. 我有一个计算器功能,当用户在div(calcanswer)中输入查询时显示结果,但通常该查询不是计算请求,因此结果为“ query =”,其中查询代表用户输入,然后什么都没有在=号后面。 I am wondering whether it is possible to implement a function that hides the div when this happens (ie there is no calculation). 我想知道是否有可能实现在发生这种情况时隐藏div的功能(即没有计算)。

PHP: PHP:

<?php
$a=$_GET['q'];
//$a="1/2";
$add = stripos($a, '+') !== false;
$sub = stripos($a, '-') !== false;
$mul = stripos($a, '*') !== false;
$div = stripos($a, '/') !== false;
if($add){
    $b=explode("+",$a);
    $n1=(float)$b[0];
    $n2=(float)$b[1];
    $n3=$n1+$n2;
}else if($sub){
    $b=explode("-",$a);
    $n1=(float)$b[0];
    $n2=(float)$b[1];
    $n3=$n1-$n2;
} else if($mul){
    $b=explode("*",$a);
    $n1=(float)$b[0];
    $n2=(float)$b[1];
    $n3=$n1*$n2;
} else if($div){
    $b=explode("/",$a);
    $n1=(float)$b[0];
    $n2=(float)$b[1];
    $n3=$n1/$n2;
}
?>

HTML: HTML:

<div class="calcanswer"><center>
  <h4 class="card-title pb-3 mbr-fonts-style display-7">
  <?= $a."=".$n3 ?>
</h4></center></div>

I have been able to fix it by doing this: 我已经可以通过执行以下操作修复它:

<?php
        if(isset($n3)) {
        ?>

        <div class="calcanswer"><center>
          <h4 class="card-title pb-3 mbr-fonts-style display-7">
           <?= $a."=".$n3 ?>
        </h4></center></div>

        <?php } ?>

I want to thank @Tim Hinz for helping me in such a short amount of time & Jim Grant for taking time out of their day to help me. 我要感谢@Tim Hinz在这么短的时间内为我提供帮助,并感谢Jim Grant抽出宝贵的时间来帮助我。

Just do this: 只要这样做:

<?php 
if(isset($_GET["query"])) {
?>

<div class="calcanswer"><center>
  <h4 class="card-title pb-3 mbr-fonts-style display-7">
   <?= $a."=".$n3 ?>
</h4></center></div>

<?php } ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM