Welcome Guest, Not a member yet? Register   Sign In
Manage an empty query
#1

Hi,

I got a model :

PHP Code:
           public function biens_location()
 
           {
 
                       $query $this->db->select('')
 
                       ->from('biens')
 
                       ->where('actif''1')
 
                       ->where('rent','1')
 
                       ->get();
 
                       if($query->num_rows()!==0)
 
                       {
 
                                   return $query->result();
 
                       }
 
                       else
                        
{
 
                                   return FALSE;
 
                       }
 
           
and a controller :
PHP Code:
           public function location()
 
           {
 
                       $this->data['biens'] = $this->Biens_model->biens_location();
 
                       $this->data['title'] = "Location";
 
                       $this->render('location_view');
 
           
and my view :
PHP Code:
foreach ($biens as $bien)
{
...


Everything is Ok, but in the case there is no result I got an error so I would like to check results and show a message on the page something like "There are no properties for rent".
How can I achieve this ?
Reply
#2

Ok I answered myself, I've just modified the controller to test the result true or false and set the correct variables to pass...
Reply
#3

There is more than one way to handle this. In your case it might be most easily done by not returning FALSE from the model. Why? Because $query->result() returns an empty array when no records are found and that fact can be used in the view.

Here's a revised model that potentially will return an empty array.

PHP Code:
 public function biens_location()
 
 {
 return 
$this->db->select('')
 ->
from('biens')
 ->
where('actif''1')
 ->
where('rent''1')
 ->
get()
 ->
result();
 
 

Your controller is unchanged.

On the view your check the var and respond with your message if the var is empty.

PHP Code:
if(empty$biens))
{
 
   echo "There are no properties for rent.";
}
else
{
 
  foreach ($biens as $bien)
 
  {
 
     ...
 
  }


You could also leave the model as is and revise your controller to conditionally provide an empty array to the view just described.

PHP Code:
public function location()
{
 
 $data $this->Biens_model->biens_location();
 
 if($data === FALSE)
 
 {
 
    $data = array();
 
 }
 
 $this->data['biens'] = $data;
 
 $this->data['title'] = "Location";
 
 $this->render('location_view');

Reply




Theme © iAndrew 2016 - Forum software by © MyBB