我想在codeigniter中使用分页进行简单的搜索...
我真的不知道怎么做..
public function testSearch()
{
$this->db->like('Profile_for', 'Myself');
$query = $this->db->get('users');
return $query->result();
}
public function searcher()
{
$config['base_url']='http://localhost/index.php/controller/searcher';
$config['total_rows'] = $this->Model->testSearch()->num_rows();
$config['per_page'] = 5;
$config['num_links'] = 2;
$this->pagination->initialize($config);
$data['records'] = $this->Model->testSearch($config['per_page'], $this->uri->segment(3));
$this->load->view('pagination', $data);
}
我认为我的控制器代码错了......
请帮帮我吧......
提前致谢.....
答案 0 :(得分:2)
1.在Config文件夹下打开routes.php
2.现在在controllers文件夹下创建一个名为pagination.php的文件并粘贴以下代码:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Pagination extends CI_Controller
{
public function __construct() {
parent:: __construct();
$this->load->helper("url");
$this->load->model("Countries_Model");
$this->load->library("pagination");
}
public function index() {
$config["base_url"] = base_url() . "pagination";
$config["total_rows"] = $this->Countries_Model->record_count();
$config["per_page"] = 10;
$config["uri_segment"] = 2;
$this->pagination->initialize($config);
$page = ($this->uri->segment(2))? $this->uri->segment(2) : 0;
$data["results"] = $this->Countries_Model
->get_countries($config["per_page"], $page);
$data["links"] = $this->pagination->create_links();
$this->load->view("pagination", $data);
}
}
3.在models文件夹下创建一个名为countries_model.php的文件并粘贴以下代码:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Countries_Model extends CI_Model
{
public function __construct() {
parent::__construct();
}
public function record_count() {
return $this->db->count_all("tblcountries");
}
public function get_countries($limit, $start) {
$this->db->limit($limit, $start);
$query = $this->db->get("tblcountries");
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
}
4.最后,在views文件夹下创建一个名为pagination.php的文件并粘贴以下代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8">
<title>Pagination with CodeIgniter</title>
</head>
<body>
<div id="container">
<h1 style="text-align:center">Page Navigation in PHP</h1>
<table width="400" border="1" align="center">
<tr>
<td width="100" bgcolor="#CCCCCC"><p>Country ID</p></td>
<td width="300" bgcolor="#CCCCCC">Country</td>
</tr>
<h1 style="text-align:center">Countries</h1>
<div id="body">
<?php
foreach($results as $data) {
?>
<tr>
<td><?php echo $data->CountryID ?></td>
<td><?php echo $data->Country ?></td>
</tr>
<?php
}
?>
</table>
<div style="text-align:center"><?php echo $links; ?></div>
</div>
</div>
</body>
</html>
这是代码概念与您的代码相同。我希望这对您有所帮助 很多谢谢