使用 codeigniter 搜索数据库记录

发布于 2024-11-08 07:00:18 字数 3318 浏览 0 评论 0原文

有人可以帮助我使用代码中的搜索功能吗?我是 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>

can someone please help me with the my search function on my code? Im new into php and im using codeigniter framework for the development.

search view

<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>

controller

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);
        }
}

model

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();

    }

}

view for the 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');?> | <?=anchor('userslist/deleteAdmin/'.$row->id, 'Delete');?></td>
                        </tr>
                        <?php }?>
                    </table>


                    </div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

王权女流氓 2024-11-15 07:00:18

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

search_admins() 函数中

尝试

array('username'=> $search)

改为

you had

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

in your search_admins() function

try

array('username'=> $search)

instead

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文