Jump to content

if(!empty($var)) question


BadGoat

Recommended Posts

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:
[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'],'';
                }  ;[/code]

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
[code]if ($row['color'] == "RED") {
                    echo '<b><font color="red">'.$row['color'].'</font></b>'[/code]
But I keep getting errors, either encapsed white space or expecting a ) or a , on the line.

Thanks :)
Link to comment
Share on other sites

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:
[code]<?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'],'';
?>[/code]

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
Link to comment
Share on other sites

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

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

Ken
Link to comment
Share on other sites

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 [code]$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[/code]
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.
Link to comment
Share on other sites

Ok, try this one...

[code]<?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'],'';
?>[/code]

Ken
Link to comment
Share on other sites

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?
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.