php - Pagination error url was not found
I created a Pagination in Codeigniter, because I would like 2 news in one page and 2 news other page etc...
So, here is my Model:
class Country extends CI_Model
{
public function __construct() {
parent::__construct();
}
public function record_count() {
return $this->db->count_all("news");
}
public function fetch_countries($limit, $start) {
$this->db->limit($limit, $start);
$query = $this->db->get("news");
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
}
I can see my news. My problem is if I want to move to the next page, I will get an error that url was not found welcome/index/2 (its the second page). localhost/codeigniter/ -> this is my homepage and the news are revealed on the homepage. It is possible to see the news but when I want to scroll to the next page then it gives the error.
Controller:
public function __construct()
{
parent::__construct();
$this->load->model('news_model');
$this->load->helper('url_helper');
$this->load->model("Country");
$this->load->library("pagination");
}
public function index()
{
$config = array();
$config["base_url"] = base_url() . "welcome/index";
$config["total_rows"] = $this->Country->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->Country->
fetch_countries($config["per_page"], $page);
$data["links"] = $this->pagination->create_links();
$data['news'] = $this->news_model->get_news();
$data['title'] = 'News archive';
$this->load->view('index', $data);
}
And finally the View:
<?php
foreach($results as $data) {
echo $data->title . " - " . $data->text . "<br>";
}
?>
I hope I was clear, but if not, then please ask, and I will edit the question.
Answer
Solution:
Your code seems fine my guess you did not configure your routes. The URI Guide
If you just have welcome that will detect index function
Base Url