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

时间:2017-06-09 13:09:12

标签: php

你好我有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);    
    }
}

&GT;

这是我的型号代码

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

    }   

        }

&GT?; 我想从url获取id并将它们传递给查询我不知道怎么做?

1 个答案:

答案 0 :(得分:0)

只需将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查询。