Errant Spaces and "If" statments

7 posts by 3 authors in: Forums > CMS Builder
Last Post: April 16, 2009   (RSS)

I've been using if statements to test for optional information.

If the information is present then it's displayed on the web page along with a heading.

The problem is that if inadvertently there's a space or a return in an optional field, it's interpreted as content and the heading appears on the page.

Anyone know how I would test for spaces and returns with no other characters present and not show the headings?

Thanks

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

Re: [gkornbluth] Errant Spaces and "If" statments

By ross - March 30, 2009

Hi Jerry

What you'll want to do here is view the source where the extra spaces are showing up just to see what the output is. If it's a <br> or a <p> tag then you can try the if's on that like

if value is not equal to <br> or <p> then display the field.

Does that make sense? Give it a shot and let me know :)
-----------------------------------------------------------
Cheers,
Ross Fairbairn - Consulting
consulting@interactivetools.com

Hire me! Save time by getting our experts to help with your project.
Template changes, advanced features, full integration, whatever you
need. Whether you need one hour or fifty, get it done fast with
Priority Consulting: http://www.interactivetools.com/consulting/

Re: [ross] Errant Spaces and "If" statments

Thanks Ross,

I'll play with this and post the result.

Jerry
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

Re: [gkornbluth] Errant Spaces and "If" statments

By Dave - April 1, 2009

Hi Jerry,

The's a few ways to do this. I just did a quick search and there's a built in PHP function called ctype_space that checks for whitespace. Note this won't work if the content has PHP tags in it, but will for whitespace.
http://www.php.net/ctype_space

So you could have something like:

<?php if ($record['title'] && !ctype_space($record['title'])): ?>
<?php echo $record['title'] ?>
<?php endif ?>


Which means "if title has a value AND it's NOT (!) all spacing characters, then show it".

Another way to approach the problem would be to strip leading and trailing spaces after loading the data but before testing if it has a value.

Hope that helps!
Dave Edis - Senior Developer
interactivetools.com

Re: [Dave] Errant Spaces and "If" statments

By gkornbluth - April 1, 2009

I think this will do the trick.

Thanks as always.

I'll post my result here.

Jerry
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

Re: [gkornbluth] Errant Spaces and "If" statments

By ross - April 16, 2009

Hi Jerry

The remove leading and trailing space option would involve the trim() function. I am thinking if the solution Dave gave you already works though, that should be fine. Either option would have the same result and you've already set that one up.

You can read about the trim function the PHP site here:


http://ca.php.net/trim


Keep us up to date with how you are making out. Thanks!
-----------------------------------------------------------
Cheers,
Ross Fairbairn - Consulting
consulting@interactivetools.com

Hire me! Save time by getting our experts to help with your project.
Template changes, advanced features, full integration, whatever you
need. Whether you need one hour or fifty, get it done fast with
Priority Consulting: http://www.interactivetools.com/consulting/