控制器仅将第一个参数传递给模型

时间:2018-06-28 11:27:46

标签: codeigniter search model controller arguments

stackoverflow.com的大家好,这是我的第一个问题,请格外小心:)。

因此,就像问题一样,我的控制器仅发送第4个参数中的第一个参数。如果我替换参数的位置,他将再次仅发送第一个参数。 如果重要,则用于搜索。 无论如何,这是代码:

控制器:

public function groups($question = NULL, $city = NULL , $country = NULL, $area = NULL){

if ($question == NULL && $city == NULL && $country == NULL && $area == NULL) {
    $groups = $this->ModelSt->getGroups();

} else {
    $groups = $this->ModelSt->search($question, $city , $country, $area);
}

$data['groups'] = $groups;
$data['controller'] = "UserSt";
$data['method']= "search";
$this->loadView($data, "groups.php");

}

public function search() {
$question  = $this->input->get('question');
$city      = $this->input->get('city');
$country   = $this->input->get('country');
$area      = $this->input->get('area');
$this->groups($question, $city, $country, $area);

}

ModelSt:

public function search($question, $city , $country, $area) {
$query = "SELECT * FROM `group` WHERE `status`='1' ";

if($question && !empty($question)){
    $query .= " AND (`name` LIKE '%".$question."%' OR `desc` LIKE '%".$question."%')";
}
if($city && !empty($city)){
    $query .= " AND (`city` LIKE '".$city."')";
}
if($country && !empty($country)){
    $query .= " AND (`country` LIKE '".$country."')";
}
if($area && !empty($area)) {
    $query .= " AND (`area` LIKE '". $area ."')";
}

$result = $this->db->query($query);
$result = $result->result_array();
return $result;

}

2 个答案:

答案 0 :(得分:0)

将参数作为数组发送:

$this->ModelSt->search(
    array(
        'question' => $question,
        'country' => $country,
        'city' => $city,
        'area' => $area
    )
);

您的型号:

public function search($post)
{
    $question = isset($post['question']) && !is_null($post['question']) && !empty($post['question']) ? $post['question'] : NULL;
    $city = isset($post['city']) && !is_null($post['city']) && !empty($post['city']) ? $post['city'] : NULL;
    $country = isset($post['country']) && !is_null($post['country']) && !empty($post['country']) ? $post['country'] : NULL;
    $area = isset($post['area']) && !is_null($post['area']) && !empty($post['area']) ? $post['area'] : NULL;


    $this->db->select('name, city, country, area');
    $this->db->from('group');
    $this->db->where('status', 1);
    $this->db->group_start();

    /**
     * Question is not null
     */
    if($question != NULL)
    {
        $this->db->group_start();
        $this->db->like('name', $question);
        $this->db->or_like('desc', $question);
        $this->db->group_end();
    }

    /**
     * City is not null
     */
    if($city != NULL)
        $this->db->like('city', $city);

    /**
     * Country is not null
     */
    if($country != NULL)
        $this->db->like('country', $country);

    /**
     * Area is not null
     */
    if($area != NULL)
        $this->db->like('area', $area);

    $this->db->group_end();
    $this->db->order_by('name', 'asc');
    return $this->db->get()->result();
}

答案 1 :(得分:0)

好吧,经过一段时间没有触及代码中的任何内容,变量1,3和4传递给了Model,但是变量2却没有。没有触摸任何东西,它就开始工作了。几天前,Codeigniter的“上载”库发生了同样的事情,它没有工作40分钟,却无缘无故地开始工作...我想我必须等变量$ city通过。谢谢您的回答!