Jump to content

Archived

This topic is now archived and is closed to further replies.

BadGoat

if(!empty($var)) question

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 :)

Share this post


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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites
Hi Ken,

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

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
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?

Share this post


Link to post
Share on other sites

×

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.