在codeigniter中为foreach错误提供的参数无效

时间:2013-10-29 10:25:04

标签: php codeigniter

这是我在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;

在视图中

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

2 个答案:

答案 0 :(得分:1)

在使用foreach之前,请检查$auction_view是否为数组。您的结果可能是错误的,因为您的数据库查询可能失败,或者没有返回结果。尝试使用is_array检查是否可以,或者您可以检查它是否为空if( !empty($auction_view) )

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

答案 1 :(得分:0)

在模型中

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  '';

在视图中

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