在 CodeIgniter 中使用搜索过滤器进行分页
Posted
技术标签:
【中文标题】在 CodeIgniter 中使用搜索过滤器进行分页【英文标题】:Pagination with Search Filter in CodeIgniter 【发布时间】:2021-01-02 05:17:39 【问题描述】:当我搜索关键字并对其进行分页时,它会返回与数据库中类似查询匹配的数据。
遇到 php 错误 严重性:警告
消息:count():参数必须是数组或者实现了Countable的对象
文件名:admin/view_member.php
行号:105
回溯:
文件:C:\xampp\htdocs\cms_new\application\views\admin\view_member.php 线路:105 函数:_error_handler
文件:C:\xampp\htdocs\cms_new\application\controllers\admin\Member.php 线路:63 功能:查看
文件:C:\xampp\htdocs\cms_new\index.php 线路:315 函数:require_once
我的控制器
$search = ($this->input->post("member_name"))? $this->input->post("member_name") : "NIL";
$search = ($this->uri->segment(3)) ? $this->uri->segment(3) : $search;
// pagination settings
$config = array();
$config['base_url'] = site_url("admin/member/index/$search");
$config['total_rows'] = $this->Model_member->record_count($search);
$config['per_page'] = "3";
$config["uri_segment"] = 4;
$choice = $config["total_rows"]/$config["per_page"];
$config["num_links"] = floor($choice);
// integrate bootstrap pagination
$config['full_tag_open'] = '<ul class="pagination">';
$config['full_tag_close'] = '</ul>';
$config['first_link'] = false;
$config['last_link'] = false;
$config['first_tag_open'] = '<li>';
$config['first_tag_close'] = '</li>';
$config['prev_link'] = 'Prev';
$config['prev_tag_open'] = '<li class="prev">';
$config['prev_tag_close'] = '</li>';
$config['next_link'] = 'Next';
$config['next_tag_open'] = '<li>';
$config['next_tag_close'] = '</li>';
$config['last_tag_open'] = '<li>';
$config['last_tag_close'] = '</li>';
$config['cur_tag_open'] = '<li class="active"><a href="#">';
$config['cur_tag_close'] = '</a></li>';
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
$this->pagination->initialize($config);
$data['page'] = ($this->uri->segment(4)) ? $this->uri->segment(4) : 0;
// get books list
$data['users'] = $this->Model_member->fetch_data($config['per_page'], $data['page'], $search);
$data['pagination'] = $this->pagination->create_links();
$this->load->view('admin/view_header',$data);
$this->load->view('admin/view_member',$data);
$this->load->view('admin/view_footer');
我的模特
function fetch_data($limit, $start, $st = NULL)
if($st == "NIL")
$st = "";
$this->db->select('*');
$this->db->from('tbl_member');
$this->db->like('member_name', $st);
$this->db->or_like('member_email', $st);
$this->db->limit($limit, $start);
$query = $this->db->get();
if ($query->num_rows() > 0)
//var_dump($query->result());
foreach ($query->result() as $row)
$data[] = $row;
//print_r($query);
return $data;
if ($query->num_rows() == 0)
$this->session->set_flashdata('recoard','<div class="alert alert-danger text-center">Record Not Found! </div>');
return false;
function record_count($st = NULL)
if($st == "NIL")
$st = "";
$this->db->select('*');
$this->db->from('tbl_member');
$this->db->like('member_name', $st);
$this->db->or_like('member_email', $st);
$query = $this->db->get();
return $query->num_rows();
var_dump($query->num_rows());
我的观点
<?php
$user = count($users);
var_dump($user);
for ($i = 0; $i < $user; ++$i)
if(! $users )
// Faild Message
echo $this->session->flashdata('recoard');
else
?>
<tbody>
<tr>
<td class="text-center"><img class="rounded-circle img-fluid avatar-40" src="<?php echo base_url();; ?>public/uploads/<?php echo $users[$i]->member_photo; ?>" ></td>
<td><?php echo $users[$i]->member_name; ?></td>
<td><?php echo $users[$i]->member_email; ?><br>
<?php echo $users[$i]->member_mobile; ?></td>
<td><?php echo $users[$i]->member_city; ?><br>
<?php echo $users[$i]->member_state; ?><br>
<?php echo $users[$i]->member_country; ?></td>
<td><?php echo $users[$i]->member_address; ?></td>
<td>
<?php
if($users[$i]->member_access == 1)
echo '<span class="badge iq-bg-primary">Active</span></td>';
else
echo '<span class="badge iq-bg-warning">Inactive</span></td>';
?>
</td>
<td>
<div class="flex align-items-center list-user-action">
<a data-toggle="tooltip" data-placement="top" title="" data-original-title="Change Status" href="<?php echo base_url(); ?>admin/member/change_status/<?php echo $users[$i]->member_id; ?>" onClick="return confirm('Are you sure?');"><i class="ri-user-add-line"></i></a>
<a data-toggle="tooltip" data-placement="top" title="" data-original-title="Edit" href="<?php echo base_url(); ?>admin/member/edit/<?php echo $users[$i]->member_id; ?>"><i class="ri-pencil-line"></i></a>
<a data-toggle="tooltip" data-placement="top" title="" data-original-title="Delete" href="<?php echo base_url(); ?>admin/member/delete/<?php $users[$i]->member_id; ?>" onClick="return confirm('Are you sure?');"><i class="iq-bg-danger ri-delete-bin-line"></i></a>
</div>
</td>
</tr>
</tbody>
<?php ?>
【问题讨论】:
您使用的是什么版本的 PHP?从 PHP 7.2 开始,如果您尝试在未设置的变量上使用方法计数,则会引发错误。 【参考方案1】:您似乎没有返回要计数和循环的数组,这就是您收到警告的原因。
您的循环中还有失败的消息,稍后会产生更多错误。 希望这对您有所帮助和工作
<?php
if (!$users || !is_array($users))
// Faild Message
echo $this->session->flashdata('recoard');
else
foreach ($users as $user) ?>
<tbody>
<tr>
<td class="text-center"><img class="rounded-circle img-fluid avatar-40" src="<?php echo base_url("public/uploads/"); ?><?php echo $user->member_photo; ?>" ></td>
<td><?php echo $user->member_name; ?></td>
<td><?php echo $user->member_email; ?><br>
<?php echo $user->member_mobile; ?></td>
<td><?php echo $user->member_city; ?><br>
<?php echo $user->member_state; ?><br>
<?php echo $user->member_country; ?></td>
<td><?php echo $user->member_address; ?></td>
<td>
<?php
if ($user->member_access == 1)
echo '<span class="badge iq-bg-primary">Active</span></td>';
else
echo '<span class="badge iq-bg-warning">Inactive</span></td>';
?>
</td>
<td>
<div class="flex align-items-center list-user-action">
<a data-toggle="tooltip" data-placement="top" title="" data-original-title="Change Status" href="<?php echo base_url(); ?>admin/member/change_status/<?php echo $user->member_id; ?>" onClick="return confirm('Are you sure?');"><i class="ri-user-add-line"></i></a>
<a data-toggle="tooltip" data-placement="top" title="" data-original-title="Edit" href="<?php echo base_url(); ?>admin/member/edit/<?php echo $user->member_id; ?>"><i class="ri-pencil-line"></i></a>
<a data-toggle="tooltip" data-placement="top" title="" data-original-title="Delete" href="<?php echo base_url(); ?>admin/member/delete/<?php $user->member_id; ?>" onClick="return confirm('Are you sure?');"><i class="iq-bg-danger ri-delete-bin-line"></i></a>
</div>
</td>
</tr>
</tbody>
<?php
?>
【讨论】:
以上是关于在 CodeIgniter 中使用搜索过滤器进行分页的主要内容,如果未能解决你的问题,请参考以下文章
在codeigniter查询生成器中使用union,并在虚拟mysql列中进行过滤