view within view with pagination in codeigniter - php

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.

Related

Codeigniter pagination doesn't show page relevant data, but the whole data set

Here is my controller :
function index()
{
$data['title']="Post";
$this->load->library('pagination');
$config['base_url'] = base_url().'Post/index';
$config['total_rows'] = $this->post_model->get_total_count();
$config['per_page'] = 1;
$config['uri_segment'] = 1;
$this->pagination->initialize($config);
$data['posts'] = $this->post_model->get_single_post($config['per_page'],$this->uri->segment(1));
$this->load->view('frontend/layouts/header',$data);
$this->load->view('frontend/view_post',$data);
$this->load->view('frontend/layouts/footer');
}
Model
function get_single_post(){
$query= $this->db->select('*')
->from('tbl_post,tbl_users')
->where('tbl_post.post_created=tbl_users.user_id')
->where('tbl_post.post_status',1)
->order_by('tbl_post.post_id','asc')
->get();
$data = $query->result_array();
return $data;
}
function get_total_count(){
$this->db->select('*');
$this->db->from('tbl_post');
$query = $this->db->get();
return $query->num_rows();
}
The relevant page data doesn't show. It shows all of the data in one page!
$config['uri_segment'] = 1 are you sure on this ?? Check this CI example
As you mentioned $config['base_url'] = base_url().'Post/index'; in question $config['uri_segment'] = 1 should be $config['uri_segment'] = 2
As well doo this too
In Controller
$config['uri_segment'] = 1;
$this->pagination->initialize($config);
$data['links'] = $this->pagination->create_links();
$data['posts'] = $this->post_model->get_single_post($config['per_page'],$this->uri->segment(1));
In View
echo $links;
Edit 01
In Model add this
function get_single_post($page,$limit) # Changed
{
$query= $this->db->select('*')
->from('tbl_post,tbl_users')
->where('tbl_post.post_created=tbl_users.user_id')
->where('tbl_post.post_status',1)
->limit($page,$limit) # Changed
->order_by('tbl_post.post_id','asc')
->get();
$data = $query->result_array();
return $data;
}
There are lots of problem.
$config['uri_segment'] cannot be 1. It should be 3
So fix it to
$config['uri_segment']=3;
segment 1 means controller name.
2 means function name.
3 means page number.
if you use 1 it will replace the controller name with page number.
You passing parameter to your model function but your model not receiving the parameters.So your function should be like this
function get_single_post($limit,$page_no){
Your model returning all records not the current page records. So add limit
->limit($page_no,$limit)//remember page_no should be 0 for 1,1 for 2.

I want to create paging on my content managed site

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();

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.

codeigniter pagination create_links()

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

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