使用codeigniter搜索数据库记录

时间:2011-05-20 02:00:48

标签: php codeigniter

有人可以帮我解决我的代码上的搜索功能吗?我是php的新手,我正在使用codeigniter框架进行开发。

搜索视图

<div id="admin-col">
                    <div>
                    <?php echo form_open('search_admin/search_admins'); ?>
                    <?php
                        $data = array('name'=>'search', 'id'=>'search');
                        echo form_input($data);
                    ?>
                    <?php
                    $data = array('name'=>'submit', 'id'=>'submit', 'value'=>'Search Admin');
                    echo form_submit($data);
                    ?>
                    </div>

控制器

class Search_admin extends CI_Controller
{
    function __construct()
    {
        parent::__construct();

        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');
        $this->load->library('security');
        $this->load->library('tank_auth');
        $this->load->model('users/usermodel');
    }

    function index()
    {
        if(!$this->tank_auth->is_logged_in())
        {
            redirect('/auth/login');
        }
        else
        {
            $data['user_id'] = $this->tank_auth->get_user_id();
            $data['username'] = $this->tank_auth->get_username();

        }
    }

    function search_admins()
        {
            $data['query']=$this->usermodel->search_admins($this->input->post('search'));
            $this->load->view('admin/users/search_result',$data);
        }
}

模型

class UserModel extends CI_Model {

    function _construct() {
        parent::_construct();
        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');
        $this->load->library('security');
        $this->load->library('tank_auth');
        $this->load->model('users/usermodel');
        $this->load->database();
    }

    function search_admins($search)
    {
    return $query = $this->db->get_where('users', array('username ='=> '$search'))->result();

    }

}

查看结果

<div id="admin-col">
                    <table>
                        <tr>
                            <th>ID</th>
                            <th>Username</th>
                            <th>Email Address</th>
                            <th>Actions</th>
                        </tr>
                        <?php foreach($query as $row){?>
                        <tr>
                            <td><?php print $row->id; ?></td>
                            <td><?php print $row->username; ?></td>
                            <td><?php print $row->email; ?></td>
                            <td><?=anchor('userslist/get_Admin/'.$row->id, 'Edit');?>&nbsp;|&nbsp;<?=anchor('userslist/deleteAdmin/'.$row->id, 'Delete');?></td>
                        </tr>
                        <?php }?>
                    </table>


                    </div>

1 个答案:

答案 0 :(得分:2)

你有

array('username ='=> '$search')

在您的search_admins()函数

尝试

array('username'=> $search)

代替