我的Codeigniter模型中有两个方法/函数
public function main_product($p_id) {
$this->db->select('p.*');
$this->db->from('products_main as p');
$this->db->where('p.product_id',$p_id);
$this->db->where('p.status','Active');
$query = $this->db->get();
if ($query->num_rows() > 0):
return $query->result();
foreach($query->result() as $row):
$product_tag = $row->product_tag;
$product_id = $row->product_id;
$name_family = $row->name_family;
$this->main_images($product_id, $product_tag, $name_family);
endforeach;
else:
$this->session->set_flashdata( 'message', 'The main product query was unsuccessful.' );
endif;
}
public function main_images($product_id, $product_tag, $name_family) {
//run query
$this->db->select('im.*');
$this->db->from('images_main as im');
$this->db->where('im.pro_img_id',$p_id);
/*$this->db->where('im.img_tag',$product_tag);
$this->db->where('im.img_family',$name_family);*/
$this->db->order_by('img_order', 'desc');
$query = $this->db->get();
if ($query->num_rows() > 0):
return $query->result();
else:
$this->session->set_flashdata( 'message', 'The new image query was unsuccessful.' );
endif;
}
我试图将成功的main_product函数中的一些数据传递给main_images函数。我以为我可以通过变量传递它,就像我在模型中那样,但它不成功。环顾四周之后,我找到了一些可能类似于此的例子,但也没有成功。
答案 0 :(得分:1)
在main_images
函数中,$this->db->where('im.pro_img_id',$p_id);
应该变为$this->db->where('im.pro_img_id',$product_id);
,因为$p_id
未定义。
此外,我强烈建议您在模型中保留所有数据库交互。控制器应该只将数据从模型传递到视图,或者从用户传递到模型。