Jump to content


Photo

if(!empty($var)) question


  • Please log in to reply
7 replies to this topic

#1 BadGoat

BadGoat
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts

Posted 12 September 2006 - 02:54 PM

Hello!

I've managed to figure out how to correctly (perhaps not correctly, but it works!) use the if(!empty($var)) feature, but there's one little problem I have been trying to iron out. I am listing a series of variables which makes up an address, but in some cases, there is no $address2. Currently in it's place there is a blank line, but I want it to not make a line at all, so the $city would be immediately below $address1. I'm positive it's a simple one, but I haven't been able to figure it. It either puts $address1 and $address2 on the same line, or it leaves the blank line.

The code:
if (!empty($address1))
                {
                echo '';
                }
                else
                {
                echo ''.$row['address1'],'<br />';
                }  ;

                if (!empty($address2))
                {
                echo '';
                }
                else
                {
                echo ''.$row['address2'],'';
                }  ;

                echo '<br />'.$row['city'],'';

                if (!empty($state))
                {
                echo '';
                }
                else
                {
                echo '&nbsp;&nbsp;'.$row['state'],'';
                }  ;

                if (!empty($zip))
                {
                echo '';
                }
                else
                {
                echo '&nbsp;&nbsp;'.$row['zip'],'';
                }  ;

                if (!empty($country))
                {
                echo '';
                }
                else
                {
                echo '<br />'.$row['country'],'';
                }  ;

Also, another if/else question.. How would I make the color of the text a specific color based on the value of the variable? I've tried using this
if ($row['color'] == "RED") {
                    echo '<b><font color="red">'.$row['color'].'</font></b>'
But I keep getting errors, either encapsed white space or expecting a ) or a , on the line.

Thanks :)

#2 Gruzin

Gruzin
  • Members
  • PipPipPip
  • Advanced Member
  • 448 posts
  • LocationGeorgia

Posted 12 September 2006 - 02:59 PM

Try this, of course you must include "red" class in your css.
if ($row['color'] == "RED") {
                    echo '<span class=red>Your Text Here</span>'

I don't need your script, I'll try to write it myself

#3 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 12 September 2006 - 03:11 PM

You have too much code to do what you want to do...
Also, you don't need semi-colons after the end curly brackets.
Try this:
<?php
if (strlen(trim($address1)) != 0) // field isn't emtpy
                echo '$row['address1'].'<br />';

if (strlen(trim($address2)) != 0) // field isn't emtpy
                echo $row['address2'];
                echo '<br />'.$row['city'].', ';

if (strlen(trim($state)) != 0) // field isn't emtpy
                echo '&nbsp;&nbsp;'.$row['state'];

if (strlen(trim($zip)) != 0) // field isn't emtpy
                echo '&nbsp;&nbsp;'.$row['zip'];

if (strlen(trim($country)) != 0) // field isn't emtpy
                echo '<br />'.$row['country'],'';
?>

The reason I don't use the empty() function is that if a field has a value of "0", the empty() function will return "TRUE".

Ken

#4 BadGoat

BadGoat
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts

Posted 12 September 2006 - 03:25 PM

Hi Ken,

That's alot smaller than I had! I tried as you recommend, but the only variable that prints is the city.

#5 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 12 September 2006 - 03:42 PM

If you copied my code, I had an extra single quote in this line:
<?php                 echo '$row['address1'].'<br />';?>
It's the one before the "$".

Where are these variables being set?  Can you post some of the script before these lines?

Ken

#6 BadGoat

BadGoat
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts

Posted 12 September 2006 - 03:53 PM

Yessir, I found that extra ' and removed it. The variables are being set in a different script and this script is just to echo the variables when they are searched for by a matching $company_id.  The query which pulls the variables is
$query = "SELECT ips.sip, ips.eip, ips.nic, ips.updated, ips.company_id, company.company_id, company.company, company.address1, company.address2, company.zip, company.contact, company.phone, company.e1, company.e2, company.e_ver, company.city_id, city.city_id, city.city, company.state_id, state.state_id, state.state, company.country_id, country.country_id, country.country FROM ips, company, city, country LEFT JOIN state ON company.state_id = state.state_id WHERE company.country_id = country.country_id AND company.city_id = city.city_id AND ips.company_id = company.company_id AND sip < '$find' AND eip > '$find'"; // exactly like this
        $data = mysql_query($query) or die("Error: ". mysql_error(). " with query ". $query); // explain what's wrong
I was able to see all the variables using the too-long version I concocted, but in yours, I am only able to see the variable that is not compared to 0.

#7 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 12 September 2006 - 04:01 PM

Ok, try this one...

<?php
if (strlen(trim($row['address1'])) != 0) // field isn't emtpy
                echo $row['address1'].'<br />';

if (strlen(trim($row['address2'])) != 0) // field isn't emtpy
                echo $row['address2'].'<br />';
                echo $row['city'].', ';

if (strlen(trim($row['state'])) != 0) // field isn't emtpy
                echo '&nbsp;&nbsp;'.$row['state'];

if (strlen(trim($row['zip'])) != 0) // field isn't emtpy
                echo '&nbsp;&nbsp;'.$row['zip'];

if (strlen(trim($row['country'])) != 0) // field isn't emtpy
                echo '<br />'.$row['country'],'';
?>

Ken

#8 BadGoat

BadGoat
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts

Posted 12 September 2006 - 04:05 PM

Ken, that worked. My level of confusion grows the deeper the variable gets nested. I'm praying experience clears that up. Much appreciated!

As for the color issue, I am writing switch/case code to see if that works. Is switch/case an efficient way to chase that tail?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users