I am having trouble debugging the code below. The code displays listings grouped and seperated by the first letter in their company name. I get an error when the query of a particular category and location extracts no results. When the code below runs to display the records it gets the following error on both instances of the foreach construct:
Warning: Invalid argument supplied for foreach()
I have tried to find a way to put a braching statement so that if there are no results from the query that 'No casting directors are currently listed in $Location' is displayed. Can someone help? The code is below:
$NumRecords=0;
unset($companies);
while($row=mysql_fetch_array($result))
{++$NumRecords;
$firstletter=strtoupper(substr($row["CompanyName"],0,1));
$blocks[$firstletter][]=$row;
}
foreach($blocks as $letter=>$block)
{
print "<font size=\"2\" color=\"#cc0033\"><b><a href=#" . $letter . ">" . $letter. "</a></b></font> ";
}
print "<br><br>";
foreach($blocks as $letter=>$block)
{
print "<font size=\"2\" color=\"#cc0033\"><b><a name=" . $letter. "></a>" .$letter."</b></font><br>";
foreach($block as $company)
{
print "<b><span class=\"listing\">";
print $company["CompanyName"];
print "</b><br>";
print $company["Address"];
print "<br>";
print $company["City"] . ", " . $company["State"] . " " . $company["ZIPCode"];
print "<br>" ;
if ($company["Phone"]) print "Phone: <font color=\"#cc0033\">" . $company["Phone"] . "</font><br>";
if ($company["Fax"]) print "Fax: <font color=\"#cc0033\">" . $company["Fax"] . "</font><br>";
if ($company["Country"]<>'US') print $company["Country"] . "<br>";
if ($company["EMail"]) print "EMail: <font color=\"#cc0033\"><a href=\"mailto:" . $company["EMail"] . "\">" . $company["EMail"] . "</a></font><br>";
if ($company["WebSite"]) print "WebSite: <font color=\"#cc0033\"><a href=\"http://" . $company["WebSite"] . "\" target=\"_blank\">" . $company["WebSite"] . "</a></font><br>";
print "</span>";
} //end of while loop
print "<a href=#top>back to top</a><br><br>";
} //end of while loop
if (0 == $NumRecords) print "No casting directors are currently listed in " . $VLocation . ".<br>";
Thanks.