BadGoat Posted September 12, 2006 Share Posted September 12, 2006 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 ' '.$row['state'],''; } ; if (!empty($zip)) { echo ''; } else { echo ' '.$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 https://forums.phpfreaks.com/topic/20515-ifemptyvar-question/ Share on other sites More sharing options...
Gruzin Posted September 12, 2006 Share Posted September 12, 2006 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] Link to comment https://forums.phpfreaks.com/topic/20515-ifemptyvar-question/#findComment-90451 Share on other sites More sharing options...
kenrbnsn Posted September 12, 2006 Share Posted September 12, 2006 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]<?phpif (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 ' '.$row['state'];if (strlen(trim($zip)) != 0) // field isn't emtpy echo ' '.$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 https://forums.phpfreaks.com/topic/20515-ifemptyvar-question/#findComment-90460 Share on other sites More sharing options...
BadGoat Posted September 12, 2006 Author Share Posted September 12, 2006 Hi Ken,That's alot smaller than I had! I tried as you recommend, but the only variable that prints is the city. Link to comment https://forums.phpfreaks.com/topic/20515-ifemptyvar-question/#findComment-90474 Share on other sites More sharing options...
kenrbnsn Posted September 12, 2006 Share Posted September 12, 2006 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 https://forums.phpfreaks.com/topic/20515-ifemptyvar-question/#findComment-90482 Share on other sites More sharing options...
BadGoat Posted September 12, 2006 Author Share Posted September 12, 2006 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 https://forums.phpfreaks.com/topic/20515-ifemptyvar-question/#findComment-90490 Share on other sites More sharing options...
kenrbnsn Posted September 12, 2006 Share Posted September 12, 2006 Ok, try this one...[code]<?phpif (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 ' '.$row['state'];if (strlen(trim($row['zip'])) != 0) // field isn't emtpy echo ' '.$row['zip'];if (strlen(trim($row['country'])) != 0) // field isn't emtpy echo '<br />'.$row['country'],'';?>[/code]Ken Link to comment https://forums.phpfreaks.com/topic/20515-ifemptyvar-question/#findComment-90500 Share on other sites More sharing options...
BadGoat Posted September 12, 2006 Author Share Posted September 12, 2006 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 https://forums.phpfreaks.com/topic/20515-ifemptyvar-question/#findComment-90507 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.