Hide DIV if function is empty

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP

Hide DIV if function is empty



let's say I have the following code, how can I hide the DIV if the function is empty?



Similarly, how can I show another DIV if the function is empty?


<div class='box12'>
<h1>text<h1/>
<p><?php echo name5(); ?><p/>
</div>



Here is my function, for reference (it is returning a term meta field of a taxonomy in wordpress):


function regular_tran()





what is an empty function ?
– Reda Meskali
2 days ago





This seems like an X/Y issue. Can you post your full issue?
– FrankerZ
2 days ago




4 Answers
4



Check if the function returns something, then show the div if it does:


<?php
$name = name5();

if ($name)
?>
<div class='box12'>
<h1>text</h1>
<p><?php echo htmlentities($name); ?></p>
</div>
<?php

?>



I've also cleaned up your h1 and p ending tags. The htmlentities call is included to avoid issues where someone could insert HTML tags in their name and execute javascript, etc. through them.


htmlentities



To show a different message div, you can use else (or use !$name if you only want to show the div if the result evaluates as empty):


else


!$name


<?php
$name = name5();

if ($name)
?>
...
<?php
else
?>
<div>shown if empty</div>
<?php

?>





Thanks. Do I have to turn the function into a $variable to do this or can I do it without that?
– harvey
2 days ago





If the function is cheap, i.e. it doesn't involve an external resource like the database, etc., you can call the function multiple times instead. Replace $name with the function call name5() in that case.
– MatsLindh
2 days ago


$name


name5()





I am not sure what you mean by external resource. I added my function to the original question.
– harvey
2 days ago





Meaning that it hits the database, a web service etc. Your function definition seems to indicate that it's a wordpress plugin / theme, and the _meta function does at least hit the database each time you use it. So no, you want to avoid calling the function more than once.
– MatsLindh
2 days ago


_meta





Ok got it. Thanks! Do you recommend that I just use a variable instead of a function to begin with? If so how should I code it?
– harvey
2 days ago



Server code must spit out something for client javascript code. Change your code:


<p><?php echo name5(); ?><p/>



to something like:


<p>
<?php if (!name5()) ?>
<script>
$(".box12").hide(); // asuming you are using jQuery
</script>
<?php ?>
<p/>





you don't require javascript here. As long as the block is encased in a php if, it won't show
– Juakali92
2 days ago






@Juakali92 Well, it depends on a particular use case. This is just elementary example of how server side "talks" to client side. It's fully valid though answer by MatsLindh is more straightforward.
– lubosdz
2 days ago



Wrap the returned result of the function with an IF statement


IF


<?php
$result = name5();
if ($result && $result !== '') ?>
<div class='box12'>
<h1>text<h1/>
<p><?php echo $result ?><p/>
</div>
<?php ?>



You can try these methods.


1-
<div class="box12">
<h1>text</h1>
<?php if(function_exists('name5')) ?>
<p><?php echo name5(); ?></p>
<?php ?>
</div>

2-
<div class="box12">
<h1>text</h1>
<?php if(name5()) ?>
<p><?php echo name5(); ?></p>
<?php ?>
</div>

3-
<div class="box12">
<h1>text</h1>
<?php $css = (name5()) ? "block":"none"; ?>
<p style="display:<?=$css?>" >
<?php echo name5(); ?>
</p>
</div>





Why did you give a minus? Where did I make a mistake?
– Haşim Yerli
2 days ago





Welcome to SO. I haven't down-voted your answer, but it is likely that it is because your answer lacks explanation. Answers are more useful if they are specific. See stackoverflow.com/help/how-to-answer.
– Nick
2 days ago





Thank you for the information. I will pay attention.
– Haşim Yerli
2 days ago






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

Executable numpy error

Trying to Print Gridster Items to PDF without overlapping contents

Mass disable jenkins jobs