简体   繁体   English

如何在Codeigniter中将变量从控制器传递到模型

[英]how to pass variables from controller to model in codeigniter

controller: 控制器:

public function getimagesprojects()
{
    $param2 = $this->uri->segment(4);
    $param3 = $this->uri->segment(5);
    $data   = $this->Admin_db->getimages($param2, $param3);
    foreach ($data as $images) {
        return $images;
    }
}

here I pass two variables from ajax method to the above controller. 在这里,我将两个变量从ajax方法传递给上述控制器。
Then I call getimages method of model by passing two variables but values are not passing to model. 然后我通过传递两个变量来调用模型的getimages方法,但是值没有传递给模型。

model: 模型:

function getimages($vendor_id, $vendor_pid)
{
    echo $vendor_id;
    echo $vendor_pid;
    $images = array();
    $res    = $this->db->query("SELECT project_gallery FROM projects where vendor_id='$vendor_id' and id='$vendor_pid';");
    foreach ($res->result_array() as $row) {
        $images[] = $row;
    }
    return $images;
}

In the above model I am trying to print values which are passed but not prited. 在上面的模型中,我试图打印通过但未被引用的值。
please help me. 请帮我。

echo images instead of returning.And in model return $res->result_array() .The result_array() returns the result in array format. echo图像而不是返回图像。在模型中return $res->result_array()result_array()array格式返回结果。

Controller: 控制器:

public function getimagesprojects()

{

 $param2 = $this->uri->segment(4);

$param3 =$this->uri->segment(5);
$this->load->model('Admin_db');
$data=$this->Admin_db->getimages($param2,$param3);
foreach($data as $images)

  {

  echo $images;//echo here

  }
}

Model: 模型:

function getimages($vendor_id, $vendor_pid)
{
    echo $vendor_id;
    echo $vendor_pid;
    $images = array();
    $res    = $this->db->query("SELECT project_gallery FROM projects where vendor_id='$vendor_id' and id='$vendor_pid'");
   return $res->result_array();//just return result in aray format
}

I don't see why you pass the variable to the model only to echo them from there, model shouldn't be printing anyway, do the printing in the controller (or better still, in the view). 我不明白为什么将变量传递给模型只是为了从那里回显它们,模型无论如何都不应该打印,请在控制器中打印(或者在视图中更好)。 Then you iterate the result_array only to recreate the exact same array under the name $images, that's unnecessary. 然后,只需迭代result_array即可重新创建名称为$ images的相同数组,这是不必要的。 I'm not sure if your query was working correctly, but I made a change to use the Codeigniter functions as they are a little cleaner and are more secure. 我不确定您的查询是否正常运行,但是我进行了更改以使用Codeigniter函数,因为它们更简洁,更安全。

Controller: 控制器:

public function getimagesprojects()
{
    $param2 = $this->uri->segment(4);
    $param3 = $this->uri->segment(5);
    echo $param2;
    echo $param3;
    $data   = $this->Admin_db->getimages($param2, $param3);
    foreach ($data as $images) {
        echo $images;
    }
}

model: 模型:

function getimages($vendor_id, $vendor_pid)
{

    $images = array();
    $this->db->where('vendor_id', $vendor_id);
    $this->db->where('id', $vendor_pid);
    $res = $this->db->get('project_gallery');

    return $res->result_array();
}

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

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