[SOLVED] if else please help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jrd
Forum Commoner
Posts: 53
Joined: Tue Mar 14, 2006 1:30 am

[SOLVED] if else please help

Post 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?
mickd
Forum Contributor
Posts: 397
Joined: Tue Jun 21, 2005 9:05 am
Location: Australia

Post 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.
jrd
Forum Commoner
Posts: 53
Joined: Tue Mar 14, 2006 1:30 am

Post 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?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

empty() ?
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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);
  }
}
?>
jrd
Forum Commoner
Posts: 53
Joined: Tue Mar 14, 2006 1:30 am

thanks

Post 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-----------



.
Post Reply