简体   繁体   English

如何从控制器到模型访问变量

[英]how to access variable from controller to model

hello i have a problem with CodeIgniter. 你好,我对CodeIgniter有问题。 i want to access a variable from controller to model because my query need id this is my controller code 我想从控制器访问模型的变量,因为我的查询需要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);    
    }
}

?> ?>

and this is my model code 这是我的模型代码

<?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();

    }   

        }

?> i want to get id from url and pass them in to query i don't know how? ?>我想从URL获取ID并将其传递给查询,我不知道如何?

Just send the id as function argument to the model: 只需将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);

    }
}

And in the model: 并在模型中:

 public function moshakhsat_list($product_id)
    {
      //mysql connections
    }

You should parse the sql query to avoid the Mysql injection for this use the CI query builder class for making mysql queries . 您应该解析sql查询,以避免Mysql注入,为此请使用CI查询构建器类进行mysql查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM