使用 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');?> | <?=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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您
在
search_admins()
函数中尝试
改为
you had
in your
search_admins()
functiontry
instead