I am working on hide show functionality using JavaScript
but it occurring error on console for onclick function
I am not getting why this error occurring.
PHP CODE
<?php
$page = '0';
$output.='<div id="my_section" style="display:none;">This is my section</div>';
echo $output;
?>
I page-id is grater than 0 need to display block of div my_section
e.g.
$output.='<div id="my_section" if($page>\'0\') { style="display:none;" } else { style="display:block;" }>This is my section</div>';
i am not getting how to add this condition in php please help
if($page > 0) {
$style = 'style="display:none;"';
} else {
$style = 'style="display:block;"';
}
$output.='<div id="my_section" ' . $style . '>This is my section</div>';
You can try below code
<?php
$page = '0';
$output.='<div class="span6">
<div class="header" onclick="showtabs(my_section,icon);">
<h1>My Section <span class="iconc" id="icon">+</span></h1>
</div></div>';
if($page > 0){
$output.='<div id="my_section" style="display:block;">This is my section</div>';
} else {
$output.='<div id="my_section" style="display:none;">This is my section</div>';
}
echo $output;
?>
You need to have your conditional logic outside of the string, eg:
$display = ($page > 0)? 'none' : 'block';
$output.='<div id="my_section" style="display:' . $display . ';">This is my section</div>';
Or longer but perhaps clearer:
output.='<div id="my_section"';
if($page>0)
{
output.= 'style="display:none;"';
}
else {
output.= 'style="display:block;"';
}
output.= '>This is my section</div>';
Try this...
$output.='<div id="my_section" ';
if($page > 0) {
$output .= 'style="display:none;"';
} else {
$output .= 'style="display:block;"';
}
$output .= '>This is my section</div>';
Shortest variant
$display = ($page > 0) ? 'style="display:none;"' : 'style="display:block;"';
$output .= "<div id=\"my_section\" {$display}>This is my section</div>";
Instead of if
condition we can use Ternary Logic
. This will makes coding simple if/else
logic quicker and make code shorter
$style = ($page>0)?'style="display:none"':'style="display:block"';
echo '<div id="my_section" '.$style.' >This is my section</div>';
From what you have done, problem is that you insert your PHP code like if was text.
$output.='<div id="my_section" if($page>\'0\') { style="display:none;" } else { style="display:block;" }>This is my section</div>';
To make it work you can use Ternary Operator and concatenation. So it will work if you write it like that (to stay in same way you try to do it) :
$output.='<div id="my_section" style="display: '.($page > 0 ? 'none' : 'block').';">This is my section</div>';