I have problem making CodeIgniter pagination when i click on link i get 404 page not found
my code in controler is
class Records extends CI_Controller
{
public function index()
{
$this->load->library('pagination');
$config['base_url']= 'localhost/records';
$config['total_rows']= $this->db->get('pages')->num_rows;
$config['per_page']= 2;
$config['num_links']= 5;
$this->pagination->initialize($config);
$data['query'] = $this->db->get('pages',$config['per_page'],$this->uri->segment(2));
$this->load->view('view_header');
$this->load->view('includes/nav_home');
$this->load->view('view_list', $data);
$this->load->view('view_footer');
}
i tried diffrent uri segments, but still no result. ? I am writing code in right way. my base_url is empty in config file?
in view my code is
<?php
echo $this->session->flashdata('item');
echo '<h4>Lista podataka</h4>';
foreach ($query->result() as $q):
?>
<?php echo $q->info . br() . br()?>
<?php
endforeach;
echo $this->pagination->create_links();
?>
As far I understand you can't access your next page pagination? So I think it's because of this $config['base_url']= 'localhost/records';
you should put your complete url(including the controller name and function name), so it should be
$config['base_url']= 'localhost/records/index.php/controller_name/function';
and in your uri segment should $this->uri->segment(3)
try may be change uri segment
$this->load->library('pagination');
$config['base_url']= base_url().'/records/index';
$config['total_rows']= $this->db->get('pages')->num_rows;
$config['uri_segment'] = 3;
$config['per_page']= 2;
$config['num_links']= 5;
$this->pagination->initialize($config);
$data['query'] = $this->db->get('pages',$config['per_page'],$this->uri->segment(3));
For more follow :- pagination in codeigniter
Base Url needs to be
$config['base_url']= 'records/index';
Base url in must be contain the same function that is used to fetch data
it means $config['base_url']='localhost/project/records/index';
Related
I have a blog page of posts that I am trying to paginate using CodeIgniter. The numbering and limiting seem to be working fine, except I keep getting a 404 when I try to travel to another page.
The strange thing is the normal culprits that cause this issue are correct. The baseUrl and the uri_segment.
My controller looks like this:
$config = array();
$config["base_url"] = $this->config->site_url("/blog");
$config["total_rows"] = $this->blog_model->count();
$config["per_page"] = 2;
$config["uri_segment"] = 2;
$config["num_links"] = round($config["total_rows"] / $config["per_page"]);
$config['use_page_numbers'] = TRUE;
$this->pagination->initialize($config);
$page = ($this->uri->segment(2)) ? $this->uri->segment(2) : 0;
$this->load->view('blog', array(
'user' => $this->user,
'blog' => $this->blog_model->loadPosts($config['per_page'], $page),
'links' => $this->pagination->create_links(),
'footer' => $this->blog_model->loadFooter()
));
And then in my model I am grabbing the posts
public function loadPosts($limit, $start)
{
$this->db->limit($limit, $start);
$this->db->order_by("date", "desc");
//this loads the contact info
$query = $this->db->get('entries');
return $query->result();
}
My full URL is www.mysite.com/blog and then with the pagination it appears as www.mysite.com/blog/2.
For the base_Url I have also tried base_url() . "/blog";.
And I have tried setting the uri_segment to 1 and 3, but nothing seems to work.
As well I have tried playing around with the routing and have added just to see if it would do anything:
$route['blog/(:num)'] = 'blog/$1';
You can use this line of code if your code is inside the index method:
$route['blog/:any'] = "blog/index/$1";
Because you have used the segment(2), and you should change the blog/index/$1 to blog/:any.
Assuming the function name that contain your pagination code is index(), you should change the route to:
$route['blog/(:num)'] = 'blog/index/$1';
And in your index() function, add the $page parameter:
public function index($page = 1){
...
With your routes, try if you can to add as many :anys or :nums after as you like:
$route['blog'] = 'blog/index'; // For the first level
$route['blog/(:any)/(:any)'] = 'blog/index/$1/$2'; // For extra "uri" segments.
// base_url pagination
$config["base_url"] = base_url("blog"); // Is preferred
You can keep your other code as it is. Just add this at your routes.php file:
$route['blog/:num'] = "blog/index";
//Assumes your code is inside the index method.
//If not, use this way:
//$route['blog/:num'] = "blog/YOUR_METHOD_NAME";
You can't pass a parameter to the index() function of a controller like it is a random function.
If you try to do controller/var instead of controller/function/var, CodeIgniter will search for a function var() inside the controller which does not exists. That's what happen when you try to access blog/2: 2() isn't a function in your controller.
You can create a new function in your controller, let's say page(), and move your code inside. That way, you will call blog/page/2. Function page() will exists and will not get a 404. Also don't fordet to redefine your base_url for pagination.
$config["base_url"] = site_url("blog/page");
Another solution if you absolutely need the URL like /blog/2, routes:
$route['blog/(:any)'] = 'blog/index/$1';
Remap may also be a solution: http://www.codeigniter.com/user_guide/general/controllers.html#remapping
I'm trying to load a blog type page, I load the entries from my database with this controller
public function blog($page){
$this->load->model("model_get");
$this->load->view("site_header");
$this->load->view("site_nav");
$counter = $this->model_get->getBlogcount();
for($counter; $counter > 0; $counter --){
$data["results"] = $this->model_get->getBlog($counter);
$this->load->view("content_blog", $data);
}
}
$this->load->view("site_footer");
}
and this model
function getBlogcount(){
$result = $this->db->count_all("blog");
return $result;
}
I count the entries in the database where I call them out by their ID. But now I'm trying to create multiple pages that expand automatically everytime I enter a new entry. So lets say I have 27 entries, and want to have no more than 5 entries on a single page, how do I make it so that it creates the necessary 6 pages to show them without loading the other 3 empty entries and stuff.
I'm new to codeigniter and have always worked with ASP .NET, any help would be helpfull.
Thanks in advance!
p.s. english isn't my first language
CodeIgniter have his own pagination Class. Take a look here : http://ellislab.com/codeigniter/user-guide/libraries/pagination.html
You can try this firstly and adapt to your project :
public function blog($page = 0)
{
$this->load->library('pagination');
$this->load->helper('url');
$config['base_url'] = base_url('blog/'. $page);
$config['total_rows'] = $this->model_get->getBlogcount();
$config['per_page'] = 5;
$this->pagination->initialize($config);
$data['results'] = $this->model_get->getBlog($config['per_page'], $page);
$this->load->view("content_blog", $data);
}
Edit your "getBlog" function model to get results with limit clause, like this :
function getBlog($limit, $start)
{
$results = $this->db->limit($limit, $start)->get('your_blog_table');
if ($results)
{
return $results->result();
}
return FALSE;
}
And use in your view this code to create your pagination links :
echo $this->pagination->create_links();
I have one problem in Codeigniter pagination with segments. I need to pass segments with page number.
Example: http://www.mysite.com/app/controller/method/param1/param2/page_number
<?php
$this->load->library('pagination');
$config['base_url'] = 'http://localhost/welcome/par1/para2/';
$config['total_rows'] = 200;
$config['per_page'] = 20;
$this->pagination->initialize($config);
echo $this->pagination->create_links();
?>
Try using the URL helper function current_url() as your base url.
From the CI documentation (http://ellislab.com/codeigniter/user-guide/helpers/url_helper.html):
current_url() -
Returns the full URL (including segments) of the page being currently viewed.
So, in your situation, the controller would look like this:
$this->load->library('pagination');
$config['base_url'] = current_url();
$config['total_rows'] = 200;
$config['per_page'] = 20;
$this->pagination->initialize($config);
echo $this->pagination->create_links();
This method should allow you to have as many parameters as you need.
The problem is that in pagination, i haven't got the database things showing up yet, but the problem isn't in that.
The problem is that I've got the page links showing up, but when I click the link to page 2, it goes to
http://127.0.0.1:8887/codeigniter-tests/index.php/pagination/index/127.0.0.1:8887/codeigniter-tests/index.php/pagination/index/5.
As you can see, the link is basically repeated twice, now if I click 1 while on page 2, it takes me to
http://127.0.0.1:8887/codeigniter-tests/index.php/pagination/index/127.0.0.1:8887/codeigniter-tests/index.php/pagination/index/127.0.0.1:8887/codeigniter-tests/index.php/pagination/index/
So as you can see, now it's written thrice, if I click 2 on this page it appends the url again and takes me there :/
Now I would like to ask WHY IS THIS happening???
Heres the code:_
Controller(pagination.php)
class Pagination extends CI_Controller {
function index() {
$this->load->library('pagination');
$config['base_url'] = '127.0.0.1:8887/codeigniter-tests/index.php/pagination/index/';
$config['total_rows'] = $this->db->get('data')->num_rows;
$config['per_page'] = 5;
$config['num_links'] = 10;
$config['uri_segment'] = 3;
$this->pagination->initialize($config);
$data['records'] = $this->db->get('data', $config['per_page'], $config['uri_segment']);
$this->load->view('pagination_view', $data);
}
}
Here's the view(pagination_view.php):_
<html>
<head>
<title>CI Pagination</title>
</head>
<body>
<h1>Pagination With CI</h1>
<?php
echo $this->pagination->create_links();
?>
</body>
</html>
Just a bit of extra info, if I set $config['base_url'] to nothing, it links to
127.0.0.1:8887/5
Any help would be appreciated, is this a bug?
Try to use this:
$config['base_url'] = '/codeigniter-tests/index.php/pagination/index/';
I don't think that $config['base_url'] supports an incomplete URL. Try adding the protocol bit or leaving only the path:
$config['base_url'] = 'http://127.0.0.1:8887/codeigniter-tests/index.php/pagination/index/';
$config['base_url'] = '/codeigniter-tests/index.php/pagination/index/';
hi friends i have this function to show all articles, i am writing this function again and again for different categories, because codeigniter arguments are related to url how do i pass arguments so that i can reuse this function ?
This is my controller function to show all news.
function all_news(){
//do some pagination
$this->load->library('pagination');
$config['base_url'] = 'http://localhost/news/all_news';
$config['total_rows'] = $this->db->get('articles')->num_rows();
$config['per_page'] = 10;
$config['num_links'] = 7;
//some css for pagination
$config['full_tag_open'] = '<div id="pagination">';
$config['full_tag_close'] = '</div>';
//initialize pagination
$this->pagination->initialize($config);
//end pagination
$data['title'] =" All News";
//for pagination
$data['query']= $this->db->order_by('id','desc');
$data['query'] = $this->db->get('articles',$config['per_page'],$this->uri->segment(3));
$this->load->vars($data);
$this->load->view('main/all_news');
}
Consider moving much of your controller logic into a model method. Then you'll be able send arguments to this method which will return back database results to your controller based on the arguments you send to the method.
http://example.com/index.php/news/local/metro/crime_is_up
The segment numbers would be this:
1-news
2-local
3-metro
4-crime_is_up
$product_id = $this->uri->segment(3);//metro
full info https://www.codeigniter.com/user_guide/libraries/uri.html