Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

If a variable contains nothing then.....

Status
Not open for further replies.

999Dom999

Technical User
Apr 25, 2002
266
GB
Am very new to PHP and I'm displaying an address from mysql which has fields

Company
Add1
Add2
Add3
Add4
Postcode

I use mysql_fetch_row to get the Company I choose from a drop down menu then echo out each row. The problem I have is some addresses don't have an Add3 or Add4.

So my question is if a variable doesn't contain anything can I use an If statement to ignore displaying it?

I thought about isset but it will always be set even if it contains nothing.
Code:
mysql_connect("localhost",$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$result = mysql_query("SELECT * FROM delivery WHERE company = '$company'");
if (!$result) {
   echo 'Could not run query: ' . mysql_error();
   exit;
}
$row = mysql_fetch_row($result);

echo "<font face='Arial'><br><br>Invoice address:<br><br>";
echo $row[2]."<br>"; // Company
echo $row[3]."<br>"; // Add1
echo $row[4]."<br>"; // Add2
echo $row[5]."<br>"; // Add3
echo $row[6]."<br>"; // Add4
echo $row[7]."<br>"; // Postcode
 
if ($row[5] <> '') {echo $row[5]."<br>"; // Add3}
 
I would change it to test on empty string...
Code:
if ($row[5]!='') echo "$row[5]<br>\n";
You know that you can use the names of the fields instead of just the index for the row array?
Code:
if ($row['Add3']!='') echo "$row['Add3']<br>\n";
Cheers,
Jeff

[tt]Jeff's Page [/tt][tt]@[/tt][tt] Code Couch
[/tt]
 
Wow what a quick responce!!!

I will try not to pester you guys too much ;)

Its hard starting out I have a book and google to help me but sometimes its not easy to find what you are looking for!

Thanks for the help guys! [pipe]
 
if (!empty(trim($row[5]))){
..display..
}

this way you wont display address lines with a tab or space(s) in.

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
I lied, you cant combine trim with empty, best

$row[5]=trim($row[5]);

if (!empty($row[5]))
{
show it
}

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top