views:

50

answers:

3

Hello, this is taking me too long to figure out. I am using Codeigniter to query a database. The model does this

 function currentfunction($id)
{ 
    $query = $this->db->get_where('mytable', array("id =" => $id));

    if($query->num_rows() > 0){
        return $query->result_array();
    }else {
        return false;
    }
}

The controller

$this->load->model('Display');
$results = $this->Display->currentfunction($id);
$this->load->view('current_items', array('currentitems' => $results));

The view

foreach($currentitems as $row){
              echo $row['name']
              ///....do more
              }

works just fine EXCEPT IF no rows are returned

then

Message: Invalid argument supplied for foreach()...

How do I handle the if...else...scenario

I tried this Q-A, but doesn't work for me. PlsHlp.

A: 

Just do:

if(is_array($currentitems)) {
    foreach($currentitems as $row){
              echo $row['name']
              ///....do more
    }
}
else
{
    echo "No items in database!";
}

You are getting an error because foreach expects its first argument to be an array. If there are no items in the database however your functions returns false.

halfdan
Thanks. It works.
RisingSun
ah man. I wrote the code for you though :(
Pavan
A: 

This is because when you run your code:

$query = $this->db->get_where('mytable', array("id =" => $id));

    if($query->num_rows() > 0){
        return $query->result_array();
    }else {
        return false;
    }

when there are no rows returned the above code works fine it just doesnt work in the view where you try to run a for loop. This for loop should not be run if there are no rows returned. yet you are trying to run a forloop even though there are no rows to work with.

My suggestion is changing the code like so:

$query = $this->db->get_where('mytable', array("id =" => $id));

if($query->num_rows() > 0){
    return $query->result_array();
}else {
    $noResults = true;
}

in the view you will have something like this before your for loop:

if($noResults != true){
     foreach($currentitems as $row){
        echo $row['name']
        ///....do more
     } 
}
else{
     //do something 
     echo "No items in database!";

}

Hope this helps.

PK

Pavan
Yes, but it is good to know alternate solutions. Thanks.
RisingSun
dont worry. you got there before me. we both had the same condept in mind. that is that you cant run the forloop without checking first to see if there are any rows to work with. GrRr next time i will get there before you :P nice one
Pavan
A: 

Why don't you just do this:

function currentfunction($id)
{ 
    return $this->db->get_where('mytable', array("id =" => $id));
}

In the view, if there are no results, an empty array will be returned and foreach won't throw an error:

foreach($currentitems->result_array() as $row)
{
    echo $row['name']
    ///....do more
}

Much cleaner IMO.

If you want to show an error message in your view, you can do:

if($currentitems->num_rows() > 0)
{
    foreach($currentitems->result_array() as $row)
    {
        echo $row['name']
        ///....do more
    }
}
else
{
    // Error message
}

This is better than checking if there are results with if/else twice, like halfdan and Pavan are suggesting.

captaintokyo
Hi, one query though. Isn't the if..else.. logic best left to the controller in a MVC model? But am not sure what the real advantages would be.
RisingSun
In my opinion there is nothing wrong with using if/else or foreach iterations in the view. In the view I like to use alternative syntax for control structures (http://php.net/manual/en/control-structures.alternative-syntax.php) to keep the view cleaner. If you don't want to show an error message, you don't even need if/else!
captaintokyo