Click to See Complete Forum and Search --> : Deciding if there is data in a field


bp_travis
05-29-2007, 04:01 PM
Hello there. I am trying to use PHP to display a list of comapanies in a database. The problem is that I want to display a companies 800 number and fax number, but not every company has an 800 number of fax number. I am trying to figure out how to make PHP determine if there is data in that column, to print it, but otherwise, don't print it. Here is a portion of the code.
$sql="SELECT * FROM buyers_guide_companies";
$result=mysql_query($sql,$db);
$row=mysql_fetch_array($result);
$other_number="SELECT 800_number FROM buyers_guide_companies";
$result2=mysql_query($other_number,$db);
$row2=mysql_fetch_array($result2);

do{
printf("<h1>%s</h1><p>",$row['company_name']);
if($row2=mysql_fetch_array($result2)){
printf(" %s <br />",$row2['800_number']);
}else{
echo"problem";
}

printf("%s<br />",$row['regular_number']);
printf("Fax:%s",$row['fax']);
printf("<a href=\"mailto:%s\">%s</a> <br />",$row['email'],$row['email']);
printf("<a href=\"http://%s\">%s</a><br />",$row['website'],$row['website']);
printf("<em>%s</em><br />",$row['pres']);
printf("<em>%s</em></p>",$row['mkg_mrg']);

}while($row=mysql_fetch_array($result));

The problem is, that the if statement always comes out true and never false, even though some companies don't have those numbers. Thanks.

Sheldon
05-29-2007, 05:54 PM
Try this
<?php
$sql = "SELECT * FROM buyers_guide_companies";
$result = mysql_query($sql,$db) or die ("There was a database issue: ". mysql_error());
while($row = mysql_fetch_array($result){

echo(("<h1>{$row['company_name']}</h1>"."\n");
if(!empty($row['800_number']))
echo("<p>{$row2['800_number']}</p>"."\n");
}
if(!empty($row['fax']))
echo("<p>{$row2['fax']}</p>"."\n");
}
if(!empty($row['regular_number']))
echo("<p>{$row2['regular_number']}</p>"."\n");
}
echo("<p><a href=\"mailto:{$row['email']}\">{$row['email']}</a></p>"."\n");
echo("<p><a href=\"{$row['website']}\">{$row['website']}</a></p>"."\n");
echo("<p><a href=\"mailto:{$row['email']}\">{$row['email']}</a></p>"."\n");
echo("<p>{$row['pres']}</p>"."\n");
echo("<p>{$row['mkg_mrg']}</p>"."\n");
}
?>