codeigniter pagination create_links() - php

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/';

Related

view within view with pagination in codeigniter

I have a view named "home.php" with a load button. When I click the button I am loading another view having pagination using ajax.
print $this->load->view('/layout/superadmin/email_listing', $this->_data, true);
For pagination in email_listing view, I have below code in model:
$config['base_url'] = site_url().'/home';
$config['total_rows'] = $this->_DB_FORUM->get('orderheader')->num_rows();
$config['per_page'] = 20;
$config['use_page_numbers'] = TRUE;
$config['num_links'] = 20;
$config['uri_segment'] = 4;
$config['full_tag_open']='<div class="pagination"><div class="links">';
$config['full_tag_close']='</div></div>';
$this->pagination->initialize($config);
$this->_DB_FORUM->select('some query');
$query = $this->_DB_FORUM->get('table_name', $config['per_page'], $this->uri->segment(4));
I am returning result of query.
return $query->result_array();
Now, when I click on page numbers page gets reloaded and so all reset. I know there must be issue with $config['base_url'] = site_url().'/home';, so what should be here that i will be on same view, only sub-view should get reload and pagination should work.
I am not getting as I am beginner with CI. Thank you in advance.

Can I have $.get call in $this->pagination->create_links();

I am working on Code igniter (new at codeigniter) and I want to do pagination on $.get.
Contoller Code is here:
public function get_todo($id=null)
{
$this->_required_login();
if($id!=null)
{
$result=$this->todo_model->get([
'todo_id'=>$id,
'user_id'=>$this->session->userdata('user_id')
]);
}
else
{
$config = array();
$config["base_url"] = base_url() . "dashboard/load_todo"; // I want here is $.get instead of a link
//I have js files in which $.get is.
$total= $this->todo_model->get_rows($this->session->userdata('user_id')); //Total rows
$config["total_rows"] = $total;
$config["per_page"] = 3; // Per Page required
// I have no idea what it is.
$config["uri_segment"] = 3;
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
// no idea thing ends here
//getting results and working fine
$data["results"] = $this->todo_model->
fetch_data($config["per_page"], $page);
// Real pain in the neck
$data["links"] = $this->pagination->create_links();
}
$this->output->set_output(json_encode($data));
}
Now about js file which is creating html response
var load_todo = function() {
$.get('api/get_todo',function(o){
//api/get_todo is controller
var output='';
for (var i=0;i<o.results.length;i++)
{
output+=Template.todo(o.results[i]); // Pagination result
}
output+='<p>'+o.links+'</p>'; // Pagination links
//console.log(output);
$("#list_todo").html(output);
},'json');
};
What I want it to have o.links to have $.get.
you forgot to return your output. In your controller method get_todo() edit the last line to this:
return $this->output
->set_content_type('application/json')
->set_output(json_encode($data));
After 3 days of struggle and headache I finally got my answer own my own. For those who might come with same thinking about pagination as I did, if you want to do pagination for ajax call get or post, best way will be to change ajax_pagination library.
ajax_pagination -> create_links() -> getAJAXlink()
and edit its code as u want to.

CodeIgniter 404 error page not found in pagination

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';

How to use pagination in codeigniter with segments?

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.

how to pass arguments in codeigniter php controller functions

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

Categories