An " if statement " (or something similar) for a textfield

7 posts by 3 authors in: Forums > CMS Builder
Last Post: October 6, 2013   (RSS)

By CarlosPinedo - October 5, 2013 - edited: October 6, 2013

Hi,

In an editor, I have a textfield to enter urls. This textfield it's not required, so, users of the cmsb may leave it blank.

This is the code in the frontend:

<p><span> Website: </span><a href="<?php echo htmlencode($record['info_map_link']) ?>" target="_blank"> click here </a></p>

When they enter the urls there's no problem. The problem appears when they don´t write an URL. Because, when the website visitors see the text "click here " and they click it, obviously, nothing happens.

I want the text "click here" to appear only if the cmsb user writes an url.

How can i do this?

Carlos

By jenolan - October 6, 2013

<p><span> Website: </span> <?php if( ! empty( trim( $record['info_map_link'] ) ) : ?><a href="<?php echo 
htmlencode($record['info_map_link']) ?>" target="_blank"> click 
here </a><?php endif ?></p>

---
<?= "Jenolan(Larry) :: Coding Since 1973" ?>
Peace and Long Life

By CarlosPinedo - October 6, 2013

Hi Larry , i´m getting this error: Fatal error: Can't use function return value in write context in /home/observat/public_html/test.php on line 320

 At line 320 is your code:

<p><span> Website: </span> <?php if( ! empty( trim( $record['info_map_link'] ) ) : ?><a href="<?php echo 
htmlencode($record['info_map_link']) ?>" target="_blank"> click 
here </a><?php endif ?></p>

By gkornbluth - October 6, 2013

Hi Carlos,

Try this...

<?php if($record['info_map_link'] ): // checks for existence of a map link ?>
<?php
if(!preg_match("/^https:\/\//i", $record['info_map_link'] )): // These check for a properly formatted link in the field ?>
<?PHP if (!preg_match("/^http:\/\//i", $record['info_map_link'])) {
$record['info_map_link'] = "http://" . $record['info_map_link']; } ?>
<?PHP endif ?>
<p><span> Website: </span><a href="<?php echo htmlencode($record['info_map_link']) ?>" target="_blank">click here</a></p>
<?php endif ?>

BTW: you'll need to supply at least a class value for the span before Website:

Hope this helps

Jerry Kornbluth

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

By CarlosPinedo - October 6, 2013

Great, that works!, thanks a lot!

Carlos

By CarlosPinedo - October 6, 2013

Thanks Larry, I´ll give it a try. Thanks for your time.