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
jrd
Forum Commoner
Posts: 53 Joined: Tue Mar 14, 2006 1:30 am
Post
by jrd » Fri Mar 17, 2006 5:22 am
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 » Fri Mar 17, 2006 7:01 am
If it returns
, 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 » Sun Mar 19, 2006 10:12 am
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?
timvw
DevNet Master
Posts: 4897 Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium
Post
by timvw » Sun Mar 19, 2006 11:16 am
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
Post
by jrd » Sun Mar 19, 2006 6:37 pm
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-----------
.