Tuesday 10 March 2015

PAGINATION_IN_CODEIGNITER

(1)write code  for "countries.php"  save into models :

<?php

class Countries extends CI_Model
{
    public function __construct() {
        parent::__construct();
    }

    public function record_count()
{

$this->load->database();

     return $this->db->count_all("contact_info"); //where contact_info is a table name

    }

    public function fetch_countries($limit, $start)
{
        $this->db->limit($limit, $start);

        $query = $this->db->get("contact_info");

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
   }
}

?>

(2)write code for  example1.php    save into views folder:


<html>
<body>
 <div id="container">
  <h1>Countries</h1>
  <div id="body">
<?php
foreach($results as $data) {
    echo $data->name;
echo $data->email;
}
?>
   <p><?php echo $links; ?></p>

  </div>

  <p class="footer">Page rendered in <strong>{elapsed_time}</strong> seconds</p>

 </div>
</body>
</html>

(3)write code for   "welcome.php"  & save it into controllers folder:



<?php
class Welcome extends CI_Controller
{
    public function __construct() {
        parent:: __construct();
        $this->load->helper("url");
        $this->load->model("Countries");
        $this->load->library("pagination");
    }

    public function example1() {
        $config = array();

$config["base_url"] = base_url() . "index.php/welcome/example1";

        //$config["<span class="s24bzc094h8" id="s24bzc094h8_5" style="height: 16px;">base</span>_url"] = base_url() . "welcome/example1"];
        $config["total_rows"] = $this->Countries->record_count();
        $config["per_page"] = 2;
        $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);
    }
}


?>

(4) to run type in browser:

http://localhost/ci/index.php/welcome/example1

No comments:

Post a Comment