codeigniter-从数据库中搜索和显示数据

时间:2015-06-09 08:21:12

标签: php codeigniter

我正在尝试将数据从数据库显示到视图,但我无法这样做。 我遇到的问题是如何在将表格标签与控制器连接时显示数据。

查看:

<form action="#" class="login-wrapper" method="get">
    <div class="span12" align="center">
     <input class="input span12 password" type="text" name="search" placeholder="search by name">
    <div class="actions">
     <input class="btn btn-danger" type="submit" name="sub" value="search now">
    </div>
    </div >
</form>

控制器:

public function select ($search){

    $this->load->model('Login_model');

    if(isset($_GET ['search']) && !empty($_GET['search'])) {

        $search= $_GET[ 'search'];
        $this->load->model('Login_model');

        if($this->Login_model->selectorganizer($search))
        {
           $this->load->view('organizer');

        }
        else
        {
            redirect('admin/show');
        }
    }  
} 

型号:

public function selectorganizer ($search) {
    $condition = "search = '" . $search . "'";
    $this->db->select('*');
    $this->db->from('organizer');
    $this->db->where($condition);
    $query = $this->db->get();
    return $result = $query->result();
}          

1 个答案:

答案 0 :(得分:0)

由于您的字段名称为name="data[search]"

所以你使用$_GET['data']['search']

进入
<?php print_r($_GET['data']['search']);?>

OR

您必须根据代码name="search"

更改字段名称
<input class="input span12 password" type="text" name="search" placeholder="search by name">

控制器

您必须致电您的观点并传递您的查询结果

<?php
public function select ($search){

    $this->load->model('Login_model');

    if(isset($_GET ['search']) && !empty($_GET['search'])) {

        $search= $_GET[ 'search'];
        $this->load->model('Login_model');
        $result=$this->Login_model->selectorganizer($search)''

        if($result)
        {
            $data['result']=$result;
             $this->load->view('YOUR_VIEW', $data);
        }
        else
        {
            redirect('admin/show');
        }
    }  
}