Show Hide div if, if statement is true
My code works to a point. What I want is that when this if statemen开发者_运维百科t 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 {
}
?>
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
}
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 } ?>>
A fresh look at this(possibly)
in your php:
else{
$hidemydiv = "hide";
}
And then later in your html code:
<div class='<?php echo $hidemydiv ?>' > maybe show or hide this</div>
in this way your php remains quite clean
Use show/hide method as below
$("div").show();//To Show
$("div").hide();//To Hide
<?php
$divStyle=''; // show div
// add condition
if($variable == '1'){
$divStyle='style="display:none;"'; //hide div
}
print'<div '.$divStyle.'>Div to hide</div>';
?>
Probably the easiest to hide a div and show a div in PHP based on a variables and the operator.
<?php
$query3 = mysql_query($query3);
$numrows = mysql_num_rows($query3);
?>
<html>
<?php if($numrows > null){ ?>
no meow :-(
<?php } ?>
<?php if($numrows < null){ ?>
lots of meow
<?php } ?>
</html>
Here is my original code before adding your requirements:
<?php
$address = 'meow';
?>
<?php if($address == null){ ?>
no meow :-(
<?php } ?>
<?php if($address != null){ ?>
lots of meow
<?php } ?>
from php you can invoke jquery like this but my 2nd method is much cleaner and better for php
if($switchView) :?>
<script>$('.container').hide();</script>
<script>$('.confirm').show();</script>
<?php endif;
another way is to initiate your class and dynamically invoke the condition like this
$registerForm ='block';
then in your html use this
<div class="col" style="display: <?= $registerForm?>">
now you can play with the view with if and else easily without having a messed code example
if($condition) registerForm = 'none';
Make sure you use 'block' to show and 'none' to hide. This is far the easiest way with php
If you wrap the whole block within the <?php ?>
tags in the initial 'if' statement and don't specify an 'else', this condition is not satisfied, returns false so therefore won't display the <div>
精彩评论