Pagination with CodeIgniter
The Model
models/
model_page_name
.php
class class_name
extends CI_Model
{
public function __construct()
{
parent::__construct();
}
public function record_count()
{
return $this->db->count_all("table_name");
}
public function your_function_name
($limit, $start) {
$this->db->limit($limit,
$start);
$query =
$this->db->get("table_name");
if ($query->num_rows()
> 0) {
foreach ($query->result()
as $row) {
$data[]
= $row;
}
return $data;
}
return false;
}
}
The Controller
controllers/
controller_page_name
.php
class
name and page name are same.
<?php
class class_name
extends CI_Controller
{
public function __construct()
{
parent::
__construct();
$this->load->helper("url");
$this->load->model("model_page_name");
$this->load->library("pagination");
}
public function example1()
{
$config =
array();
$config["base_url"]
= base_url() . "controller_page_name/function_name";
$config["total_rows"]
= $this->model_page_name ->model_page_function_name();
$config["per_page"]
= 20; //here you can set how many item par page.
$config["uri_segment"]
= 3;
$this->pagination->initialize($config);
$page =
($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data["results"]
= $this-> model_page_name->
model_page_function_name($config["per_page"],
$page);
$data["links"]
= $this->pagination->create_links();
$this->load->view("view_page_name",
$data);
}
}
The View
views/
view_page_name.php
<body>
<div
id="container">
<h1>Page
Title</h1>
<div
id="body">
<?php
foreach($results as $data)
{
echo $data->Field_name1
. " - " . $data-> Field_name2 "<br>";
}
?>
<p><?php
echo $links; ?></p>
</div>
</div>
</body>
That’s it you pagination is complet.
Test your code and enjoy.
Comments
Post a Comment