使用db with get parameters错误

时间:2015-05-23 02:51:42

标签: php codeigniter

我使用很多数据库进行语言响应。

class Category extends CI_Model
{

public function __construct()
{

    parent::__construct();
    $this->lanDB = array(
        "KR" => $this->load->database('KR', TRUE),
        "EN" => $this->load->database('EN', TRUE),
        );                                        
}

public function get(){

    $category = $this->lanDB['KR']->from('Category')->get()->result();
    foreach ($category as $value) {
        $value->list = $this->lanDB['KR']->select('id AS series_id, subject')->from('Series')->
            where('category_id', $value->id)->get()->result();
    }
    return $category;
}
} 

这段代码做得很好。但是使用get参数代替' KR'像这样

class Category extends CI_Model
    {

    public function __construct()
    {

        parent::__construct();
        $this->lanDB = array(
            "KR" => $this->load->database('KR', TRUE),
            "EN" => $this->load->database('EN', TRUE),
        );                                        
    }

    public function get(){

        $category = $this->lanDB[$_GET['country']]->from('Category')->get()->result();
        foreach ($category as $value) {
            $value->list = $this->lanDB[$_GET['country']]->select('id AS series_id, subject')->from('Series')->
            where('category_id', $value->id)->get()->result();
        }
        return $category;
    }
} 

class Category extends CI_Model
{

    public function __construct()
    {

        parent::__construct();
        $this->lanDB = array(
            "KR" => $this->load->database('KR', TRUE),
            "EN" => $this->load->database('EN', TRUE),
        );                                        
    }

    public function get(){

        $category = $this->lanDB[$this->input->get('country')]->from('Category')->get()->result();
        foreach ($category as $value) {
            $value->list = $this->lanDB[$this->input->get('country')]->select('id AS series_id, subject')->from('Series')->
            where('category_id', $value->id)->get()->result();
        }
        return $category;
    }
}

此代码不起作用。

请帮帮我。

1 个答案:

答案 0 :(得分:1)

您需要从控制器传递这样的参数:

public function controller_function_name() {

  $result = $this->model->get($this->input->get('country'));

  $data = array();
  $data['result'] = $result;
  $this->load->view('view_file_name', $data);
}

在模特中:

public function get($country){

$category = $this->lanDB[$country]->from('Category')->get()->result();
foreach ($category as $value) {
    $value->list = $this->lanDB[$country]->select('id AS series_id, subject')->from('Series')->
        where('category_id', $value->id)->get()->result();
}
return $category;
}