user2902928
user2902928

Reputation: 1

Invalid argument supplied for foreach error in codeigniter

Here is my code in codeigniter

IN MODEL

    $where = "is_display ='Yes' AND start_date < '{$current_time}' AND end_date >              
             '{$current_time}' AND status = 'Live' AND id NOT IN (SELECT id FROM (emts_auction)                           
             WHERE is_display = 'Yes' AND quantity =1 AND sold_qty >0    
             AND last_auc_ending_time < '{$current_time}')";

    $this->db->select('*');
    $this->db->from('auction');
    $this->db->where($where);       
    $this->db->order_by("id", "desc");
    $query = $this->db->get();
    $data = $query->result();
        $query->free_result();
        return $data;

IN VIEW

  <?php foreach($auction_view as $auction)
       {
       echo $auction->name;
       echo $auction->price;
       echo $auction->bid_fee;
       }
      ?>

Upvotes: 0

Views: 3813

Answers (2)

user2932182
user2932182

Reputation: 1

In model

where = "is_display ='Yes' AND start_date < '{$current_time}' AND end_date >              
'{$current_time}' AND status = 'Live' AND id NOT IN (SELECT id FROM (emts_auction)                           
WHERE is_display = 'Yes' AND quantity =1 AND sold_qty >0    
AND last_auc_ending_time < '{$current_time}')";
$this->db->select('*');
$this->db->from('auction');
$this->db->where($where);       
$this->db->order_by("id", "desc");
$query = $this->db->get();
if($quer->num_rows() < 0){return  $query->result();
}
return  '';

In view

<?php 
if(!empty($auction_view)){
foreach($auction_view as $auction)
{
echo $auction->name;
echo $auction->price;
echo $auction->bid_fee;
}
}
?>

Upvotes: 0

user2092317
user2092317

Reputation: 3348

Please check the $auction_view if it is an array or not , before using foreach on it. Your result may be false since your database query may fail, or returned no result. try to check if it by using is_array or you could do check if it is not empty if( !empty($auction_view) )

if (is_array($auction_view))
{
   foreach($auction_view as $auction)
       {
       echo $auction->name;
       echo $auction->price;
       echo $auction->bid_fee;
       }
}

Upvotes: 1

Related Questions