Forum Moderators: coopster

Message Too Old, No Replies

Not Null Always Getting Returned

         

almo136

4:13 pm on Feb 23, 2010 (gmt 0)

10+ Year Member



I have this piece of code in my cms:

<ul class="subsection_tabs" id="tab_group_one" style="clear:none;">
<?php if ($this->getChildHtml('description')==NULL) { echo '<div id="trollweb_1"></div>'; } else echo('
<li class="tab"><a href="javascript:void(0);" id="trollweb_1" onClick="trollweb_tabs(1)" class="active"><h4>' . $this->__('Product Description'). '</h4></a></li> '); ?>


($this->getChildHtml('description')==NULL)

Returns a text area which can be completed in the backend. I have this this text area empty.

My problem is it always outputs the 'else'. Even though I haven't filled in the description in the back end and it is blank.

How can I fix this?

I'm a php beginner so it is probably a basic mistake.

mooger35

4:23 pm on Feb 23, 2010 (gmt 0)

10+ Year Member



I believe an empty post variable isn't null.

Try using

if (empty($this->getChildHtml('description'))) { etc...

Matthew1980

4:27 pm on Feb 23, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there Almo1336,


<?php if ($this->getChildHtml('description')==NULL) { echo '<div id="trollweb_1"></div>'; } else{ echo('
<li class="tab"><a href="javascript:void(0);" id="trollweb_1" onClick="trollweb_tabs(1)" class="active"><h4>' . $this->__('Product Description'). '</h4></a></li> '); }?>


Add the braces around the else part of the clause:-

if($var to evaluate){
//output if true
}else{
//else output if false
}


Hope that helps,

Cheers,

MRb

almo136

4:36 pm on Feb 23, 2010 (gmt 0)

10+ Year Member



I tried this:

if (empty($this->getChildHtml('description')))


So my code now looks like this:

  <?php if (empty($this->getChildHtml('description'))) { echo '<div id="trollweb_1"></div>'; } else { echo('
<li class="tab"><a href="javascript:void(0);" id="trollweb_1" onClick="trollweb_tabs(1)" class="active"><h4>' . $this->__('Product Description'). '</h4></a></li> '); } ?>


This seemed to break everything and all of the content below this didn't get output on the page.

almo136

4:52 pm on Feb 23, 2010 (gmt 0)

10+ Year Member



This worked:

if ( 0<strlen(trim($this->getChildHtml('description'))) ) {

Matthew1980

7:50 pm on Feb 23, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there almo136,

With regards to
if ($this->getChildHtml('description')==NULL)

I think as this would have done the same job:-

if (!isset($this->getChildHtml('description')))


But, this should be this way around I think as you are trying to see if the value is less than 0, effectively saying, if its not holding a value. Unless I read it wrong:-


if (strlen(trim($this->getChildHtml('description'))) < 0 ) {


Then again, that's just a hunch, but if you went to your original code post, I think the issue was down to the missing braces around the 'else'.

But as long as you are getting the desired output now, that's great, good luck with the rest of the project.

Cheers,

MRb