Hide html code if field is empty

Permalink Browser Info Environment
Hi Jordan,

I styled an item like:
<p><?php $item->intem_one->display(); ?></p>


Is it possible to hide the html code (<p></p> in this case) as well if the field for that item is empty?

Best

Torsten

Type: Discussion
Status: New
tsilbermann
View Replies: View Best Answer
theblockery replied on at Permalink Reply
theblockery
Hi Torsten,

Jordan is out today, but I can answer your question.

If "intem_one" is a textbox or textarea field, you can do this:

<?php if (!empty($item->intem_one->getText()) { ?>
    <p><?php $item->intem_one->display(); ?></p>
<?php } ?>


The various field types in DCP have several options for displaying/getting the content. We have API docs available athttp://theblockery.com/designer-content-pro/.... That should be helpful, but definitely let us know if you get hung up and have any other questions.

- Dan
tsilbermann replied on at Permalink Reply
tsilbermann
Hi Dan,

when I use that code (exactly), it breaks my page. Is there something wrong with the php code?

Here is my code:
<?php defined('C5_EXECUTE') or die(_("Access Denied.")); ?>
<?php /* This block was made with Designer Content Pro. Visithttp://theblockery.com/dcp for documentation. */ ?>
<div class="well">
<?php foreach ($controller->getRepeatingItems() as $item): ?>
   <h4><?php $item->name->display(); ?></h4>
   <?php if (!empty($item->info->getText()) { ?>
    <p><?php $item->info->display(); ?></p>
   <?php } ?>
   <p><?php $item->info2->display(); ?></p>
   <p>
   Tel: <?php $item->tel->display(); ?><br />
   Fax: <?php $item->fax->display(); ?><br />
   Link: <?php $item->link->display(); ?><br />
   E-Mail: <a href="mailto:<?php $item->email->display(); ?>"><?php $item->email->display(); ?></a>
   </p>

Best
Torsten
theblockery replied on at Permalink Best Answer Reply
theblockery
Hi Torsten,

My apologies. The use of empty() threw an error.

Try this instead:

<?php if ($item->info->getText()) { ?>
     <p><?php $item->info->display(); ?></p>
<?php } ?>


getText() will return an empty string (resulting in false for the if statement) if the field was left blank. So empty() wasn't really necessary anyway.

Thanks,
Dan
tsilbermann replied on at Permalink Reply
tsilbermann
Thanks - that works :)

concrete5 Environment Information

Browser User-Agent String

Hide Post Content

This will replace the post content with the message: "Content has been removed by an Administrator"

Hide Content

Request Refund

You have not specified a license for this support ticket. You must have a valid license assigned to a support ticket to request a refund.