CodeIgniter Forums

Full Version: Check if table is empty
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Volkof[/eluser]
Hi,

Can anyone guide me on how to check if a returned table is empty?
I used this:

Code:
<?php
   foreach($programmes as $row){
    if(!(empty($row))){
     echo '<a class="'.$row-&gt;programmeID.'" href="/index.php/test/getModules/'. $row-&gt;programmeID.'">';
     echo '<b>'.$row->programmeName.'</b><br/>';
     echo '<br/></a>';  
    }else{
     echo 'No Programmes found!';
    }
   }
  ?&gt;

but the if(!(empty($row))) does not seems to work.

El Forum

[eluser]Aken[/eluser]
Look into num_rows() or count_all() methods.

El Forum

[eluser]Rowan Wilson[/eluser]
There are many ways to achieve this.

In your if statement in the foreach loop you don't need the parenthesis around empty().

But otherwise, why not check your result is not empty before trying to loop through it?

e.g.

Code:
if($programmes)
{
foreach($programmes as $row)
{
  //..
}
}
else
{
echo 'No Programmes found!';
}

El Forum

[eluser]Volkof[/eluser]
Thanks rowstu.

It works now Smile