Page 1 of 1

[SOLVED] if else please help

Posted: Fri Mar 17, 2006 5:22 am
by jrd

Code: Select all

while($row = odbc_fetch_array($query)) 
{			 													 
if ($row['ID'] != NULL ){
row_color($cnt++);  
echo '<td>';
echo 'Unique ID: '.$row['ID'].'<br /></td><td>'; 
echo '<font color=red>Forecast: '.$row['forecast']. '<br /></font></td><td>'; 
echo 'IELTS: '.$row['ielts'].'<br /></td>'; 
echo '</tr>';  }  

else {	
echo '-no anomalies found-';	  	 
	  }	 
	  
}
I have a staement which should return no anomalies if it is null, but i'm not sure why it doesn't. Please help, is it my code?

Posted: Fri Mar 17, 2006 7:01 am
by mickd
If it returns

Code: Select all

-no anomalies found-
, it means that $row['ID'] must be equal to NULL. Check that the output is correct by using var_dump or its an equivilant.

Posted: Sun Mar 19, 2006 10:12 am
by jrd
If it returns
Code:

-no anomalies found-

, it means that $row['ID'] must be equal to NULL. Check that the output is correct by using var_dump or its an equivilant.
Actually it returns the values i want if there are values but doesn't return 'no anomalies' when there are no value. How do i fix it?

Posted: Sun Mar 19, 2006 10:54 am
by John Cartwright
empty() ?

Posted: Sun Mar 19, 2006 11:16 am
by timvw
The else branch is only executed when there is a row...

If you want to echo '-no anomalies found-' when there are no rows in the resultset you'll have to rewrite your logic a little, eg:

Code: Select all

<?php
$row = odbc_fetch_array($query));
if (!$row) {
  echo 'no anomalies';
} else {
  while ($row) {
    print_r($row);
    $row = odbc_fetch_array($query);
  }
}
?>

thanks

Posted: Sun Mar 19, 2006 6:37 pm
by jrd
Thanks guys, both your styles work. Here's the code :

i.

Code: Select all

while($row = odbc_fetch_array($query)) 
{			 
if (empty($row)){		  
echo '-no anomalies found-';	
 }
else {	 
row_color($cnt++);  
echo '<td>';
echo 'Unique ID: '.$row['ID'].'<br /></td><td>'; 
echo '<font color=red>Actual: '.$row['actual']. '<br /></font></td><td>'; 
echo 'IELTS: '.$row['ielts'].'<br /></td>'; 
echo '</tr>';   
	  }
}
ii.

Code: Select all

while($row = odbc_fetch_array($query)) 
{			 
if (!$row){		  
echo '-no anomalies found-';	
 }
else {	 
row_color($cnt++);  
echo '<td>';
echo 'Unique ID: '.$row['ID'].'<br /></td><td>'; 
echo '<font color=red>Actual: '.$row['actual']. '<br /></font></td><td>'; 
echo 'IELTS: '.$row['ielts'].'<br /></td>'; 
echo '</tr>';   
	  }
}

Works like a charm, damn now comes the difficult part... selecting either method i. (or) ii. :(


--------SOLVED-----------



.