Model:
<?php
class Countries extends CI_Model {
public function record_count() {
return $this->db->count_all("actor");
}
public function fetch_countries($limit, $start) {
$this->db->limit($limit, $start);
$query = $this->db->get("actor");
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
}
------------------------------------
Controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function __construct() {
parent:: __construct();
$this->load->helper("url");
$this->load->model("Countries");
$this->load->library("pagination");
}
public function index() {
$this->load->view('welcome_message');
}
public function example1() {
$config = array();
$config["base_url"] = base_url() . "welcome/example1";
$config["total_rows"] = $this->Countries->record_count();
$config["per_page"] = 5;
$config["uri_segment"] = 3;
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data["results"] = $this->Countries->
fetch_countries($config["per_page"], $page);
$data["links"] = $this->pagination->create_links();
$this->load->view("example1", $data);
}
}
/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
-------------------------------------
views:
<body>
<div id="container">
<h1>Countries</h1>
<div id="body">
<?php
print '<pre>';
foreach($results as $data) {
print_r($data);
}
?>
<p><?php echo $links; ?></p>
</div>
<p class="footer">Page rendered in <strong>{elapsed_time}</strong> seconds</p>
</div>
</body>
--------------------------------------
Output:
0 comments:
Post a Comment