![](/img/trans.png)
[英]How to access a public static variable in a model from a controller in Codeigniter?
[英]how to access variable from controller to model
你好,我對CodeIgniter有問題。 我想從控制器訪問模型的變量,因為我的查詢需要id這是我的控制器代碼
<?php
class C_Moshakhsat extends CI_Controller{
public function index(){
$data['Todos']=$this->M_Moshakhsat->moshakhsat_list();
$this->load->view('V_Moshakhsat',$data);
$product_id = $this->input->get('id', TRUE);
}
}
?>
這是我的模型代碼
<?php
class M_Moshakhsat extends CI_Model{
public function moshakhsat_list(){
$query="SELECT customer_name , order_date , saled_product_price , saled_product_quantity, product_name ,saled_product_price*saled_product_quantity as ss FROM customer c ,orders o ,order_detail d , product p WHERE p.produt_id=d.product_id and o.order_id=d.order_id and c.customer_id=o.order_id and o.order_id=product_id";
$res=$this->db->query($query);
return $res->result();
}
}
?>我想從URL獲取ID並將其傳遞給查詢,我不知道如何?
只需將id作為函數參數發送給模型:
class C_Moshakhsat extends CI_Controller{
public function index(){
$product_id = $this->input->get('id', TRUE);
$data['Todos']=$this->M_Moshakhsat->moshakhsat_list($product_id);
$this->load->view('V_Moshakhsat',$data);
}
}
並在模型中:
public function moshakhsat_list($product_id)
{
//mysql connections
}
您應該解析sql查詢,以避免Mysql注入,為此請使用CI查詢構建器類進行mysql查詢。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.