মঙ্গলবার, ৯ জুন, ২০১৫

The Model

We’ll start by creating a model in the application which needs to do two things: provide a count of all the records in the Country table, and retrieve a list of countries from the table. Save the following asmodels/countries.php:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Countries extends CI_Model
{
    public function __construct() {
        parent::__construct();
    }
 
    public function record_count() {
        return $this->db->count_all("Country");
    }
 
    public function fetch_countries($limit, $start) {
        $this->db->limit($limit, $start);
        $query = $this->db->get("Country");
 
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
   }
}


The Controller

Next, we need to create a method in the default Welcome controller (controllers/welcome.php) calledexample1(). But just before we do that, we’ll need to load the model and also the pagination library in the class’ constructor.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?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() . "welcome/example1";
        $config["total_rows"] = $this->Countries->record_count();
        $config["per_page"] = 20;
        $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);
    }
}

The View

For the view file, we can copy the views/welcome_message.php and called it example1.php and replace most of the content of the body with the following:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<body>
 <div id="container">
  <h1>Countries</h1>
  <div id="body">
<?php
foreach($results as $data) {
    echo $data->Name . " - " . $data->Continent . "<br>";
}
?>
   <p><?php echo $links; ?></p>
  </div>
  <p class="footer">Page rendered in <strong>{elapsed_time}</strong> seconds</p>
 </div>
</body>

কোন মন্তব্য নেই:

একটি মন্তব্য পোস্ট করুন