I’m trying to write xml to a page to be read by flash from a mysql database. Everything worked fine until I added an if statement to print something different if there was no data from the query.
If I apply the test outside the while loop I get the proper “NO RESULT” response but if there is data returned I lose the first item in the array. My code looks like this:
$row = mysql_fetch_array($allrecords);
$xmlData .="<results>
";
if (!$row["Name"]){
$xmlData .="<manufacturer>
";
$xmlData .= " <category>NO RESULTS</category>
";
$xmlData .= " <image>PLEASE TRY ANOTHER BRAND</image>
";
$xmlData .= " <brand>'null'</brand>
";
$xmlData .= " <logo>'null'</logo>
";
$xmlData .="</manufacturer>
";
}else{
while ($row = mysql_fetch_array($allrecords)){
$xmlData .="<manufacturer>
";
$xmlData .= " <category>" . $row["Category"] . "</category>
";
$xmlData .= " <image>" . $row["catImage"] . "</image>
";
$xmlData .= " <brand>" .$row["Name"] . "</brand>
";
$xmlData .= " <logo>" . $row["Logo"] . "</logo>
";
$xmlData .="</manufacturer>
";
}
}
$xmlData .="</results>
";
print $xmlData;
$xmlData="";
If I place the test inside the while loop like so:
$xmlData .="<results>
";
while ($row = mysql_fetch_array($allrecords)){
if (!$row["Name"]){
$xmlData .="<manufacturer>
";
$xmlData .= " <category>NO RESULTS</category>
";
$xmlData .= " <image>PLEASE TRY ANOTHER BRAND</image>
";
$xmlData .= " <brand>'null'</brand>
";
$xmlData .= " <logo>'null'</logo>
";
$xmlData .="</manufacturer>
";
}else{
$xmlData .="<manufacturer>
";
$xmlData .= " <category>" . $row["Category"] . "</category>
";
$xmlData .= " <image>" . $row["catImage"] . "</image>
";
$xmlData .= " <brand>" .$row["Name"] . "</brand>
";
$xmlData .= " <logo>" . $row["Logo"] . "</logo>
";
$xmlData .="</manufacturer>
";
}
}
$xmlData .="</results>
";
print $xmlData;
$xmlData="";
The full array is printed if there is data but if there us no data nothing is printed. Does anybody see what I’m doing wrong? It seems to have something to do with fetching the array twice? Any advice would be greatly appreciated.
Thank you