Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show Hide div if, if statement is true

Tags:

My code works to a point. What I want is that when this if statement is false, the <div> doesn't show

<?php
    $query3 = mysql_query($query3);
    $numrows = mysql_num_rows($query3);
    if ($numrows > 0) {
        $fvisit = mysql_fetch_array($result3);
    }
    else {
    }
?>
like image 730
Christian Avatar asked Aug 11 '11 04:08

Christian


People also ask

How do I hide a div tag?

To hide an element, set the style display property to “none”. document. getElementById("element").

How do you hide a div if it is empty?

Hide empty divs - To hide the div set: display: none; in :empty selector · GitHub.

How to hide display in PHP?

You should really just add a class like “hide” instead and have . hide{display:none;} instead.


2 Answers

You can use css or js for hiding a div. In else statement you can write it as:

else{
?>
<style type="text/css">#divId{
display:none;
}</style>
<?php
}

Or in jQuery

else{
?>
<script type="text/javascript">$('#divId').hide()</script>
<?php
}

Or in javascript

else{
?>
<script type="text/javascript">document.getElementById('divId').style.display = 'none';</script>
<?php
}
like image 84
munjal Avatar answered Sep 21 '22 14:09

munjal


This does not need jquery, you could set a variable inside the if and use it in html or pass it thru your template system if any

<?php
$showDivFlag=false
$query3 = mysql_query($query3);
$numrows = mysql_num_rows($query3);
if ($numrows > 0){
    $fvisit = mysql_fetch_array($result3);
    $showDivFlag=true;
 }else {

 }

?>

later in html

  <div id="results" <?php if ($showDivFlag===false){?>style="display:none"<?php } ?>>
like image 23
Dreaded semicolon Avatar answered Sep 24 '22 14:09

Dreaded semicolon