I have done the pagination in Kohana 3.1 using the following code in my controller page .How can I display the Pagination links in my view page ? Is this the correct way ??? or any issue with my code ?
public function action_imagelist()
{
$per_page =2;
$page_num = $this->request->param('page', 1);
$offset = ($page_num - 1) * $per_page;
$view =View::factory('image/imagelist')->bind('page_links',$page_links)->bind('results', $results)->bind('pagination', $pagination);
// Get the total count of records in the database
$userid = Auth::instance()->get_user()->pk();
$count=ORM::factory('user_image')->where('app_userid','=',$userid)->count_all();
// Create an instance of Pagination class and set values
$pagination = Pagination::factory(array(
'total_items' => $count,
'current_page' => array('source' => 'image/imagelist', 'key' => 'page'),
'items_per_page' => $per_page,
'offset' => $offset,
'view' => 'pagination/basic'
));
// Load specific results for current page
$results = DB::select()->from('user_images')
->where('app_userid','=',$userid)
->order_by('image_id','ASC')
->limit($pagination->items_per_page)
->offset($pagination->offset)->execute();
// print_r($results);
$page_links = $pagination;
$this->template->content=$view->render();
}
To display pagination, in your view print out $page_links.
You should read how stuff is divided in MVC pattern!
Related
I'm relatively new in Code Igniter and I'm trying to use the Code Igniter pagination class for the first time. I'm trying to get some rows in my database table, depending on the state, lga and category selected by the user. These three are passed to the function as parameters. I have followed the process I found on SO and some other sites to implement pagination on the page, and it seems to work, except that it doesn't return all the rows that has the specified parameters.
This what I have:
Model - Posts_model.php
public function count_lga_cat_posts($state, $lga, $category) {
return $this->db->get_where('lga_posts', array('state' => $state, 'lga' => $lga, 'category' => $category, 'display' => 'true'))->num_rows();
}
public function fetch_lga_cat_data($limit, $start) {
$this->db->limit($limit, $start);
$this->db->order_by("id", "desc");
$query = $this->db->get("lga_posts");
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
Controller - Lga_posts.php
public function selected_state_lga_posts_category($state, $lga, $category)
{
//config for pagination
$config = array();
$uri_seg = 5;
$config["base_url"] = base_url('state2/'.$state.'/'.$lga.'/'.$category); //as specified in routes
$config["total_rows"] = $this->posts_model->count_lga_cat_posts($state, $lga, $category);
$config["per_page"] = 3;
$config["uri_segment"] = $uri_seg;
$config['use_page_numbers'] = TRUE;
$config['cur_tag_open'] = ' <a class="current">';
$config['cur_tag_close'] = '</a>';
$config['first_link'] = 'Back to first page';
$config['next_link'] = 'View more posts';
$config['prev_link'] = 'Previous posts';
$config['last_link'] = 'Jump to last page';
$config['display_pages'] = FALSE;
$this->pagination->initialize($config);
$page = ($this->uri->segment($uri_seg))? $this->uri->segment($uri_seg) : 0;
$this->db->where(array('state' => $state, 'lga' => $lga, 'category' => $category, 'display' => 'true'));
$data["results"] = $this->posts_model->fetch_lga_cat_data($config["per_page"], $page);
$str_links = $this->pagination->create_links();
$data["links"] = explode(' ',$str_links );
$data['state'] = $state;
$data['lga'] = $lga;
$data['category'] = $category;
$this->load->view('posts/lga/selected_state_lga_posts_category', $data);
When I load this page, everything seems to work fine, the pagination links are working as expected, but some rows in the table are not pulled, even though they have the same parameters as the ones that were pulled and displayed. If I change per_page config to 1, the last page gives an error "invalid argument in foreach" or something similar. I wonder if it has something to do with my query, since I do not want to get all the rows in the table, but the ones that have the selected parameters i.e. state, lga and category.
Can someone please point out to me what I'm doing wrong?
I use codeigntier 3 pagination library.
It works, but not exactly how should.
First I will post my code.
controller setting
$config['base_url'] = base_url('items');
$config['total_rows'] = $this->ItemsM->countAll();
$config['per_page'] = 5;
$config['use_page_numbers'] = TRUE;
$config['nex_link'] = "Next";
$limit = $config['per_page'];
$offset = $this->uri->segment(2);
$this->pagination->initialize($config);
$data = array(
'musicians' => $this->ItemsM->allItems($limit, $offset),
'pagination' => $this->pagination->create_links()
);
$this->load->view('public/header');
$this->load->view('public/item-list', $data);
$this->load->view("public/footer");
Model
class ItemsM extends CI_MODEL{
function allUsers($limit="", $offset ="")
{
$query = $this->db->select('*')
->from('items')
->limit($limit)
->offset($offset)
->order_by('creation_date', 'desc')
->get();
return $query->result();
}
public function countAll(){
return $this->db->count_all_results('items');
}
Route
$route['items/(:any)'] = 'items';
This pagination works, but the results Im getting are not correct.
On the first page/tab it shows items with this order
item 1 item 2 item 3 item 4 item 5 // ok
but when open page 2
I get next 5 results this way
item 3
item 4
item 5
item 6
item 7
Honestly I do not understand where is the problem, I someone know, please help. Thank you.
It seems your problem is with varialble $offset as you are passing $offset as your page number for eg. if your page number is 2 then it will skip 2 results but you need to skip 5 results. To solve this replace this code with you controller setting.
$config['base_url'] = base_url('items');
$config['total_rows'] = $this->ItemsM->countAll();
$config['per_page'] = 5;
$config['use_page_numbers'] = TRUE;
$config['nex_link'] = "Next";
$limit = $config['per_page'];
$page = $this->uri->segment(2);
if($page > 1){
$offset = ($page-1) * $limit;
}else{
$offset = $page;
// Or you can set $offset = 0;
}
$this->pagination->initialize($config);
$data = array(
'musicians' => $this->ItemsM->allItems($limit, $offset),
'pagination' => $this->pagination->create_links()
);
$this->load->view('public/header');
$this->load->view('public/item-list', $data);
$this->load->view("public/footer");
Here i edited your variable $offset. Now you will get results as expected.
I'm using codeigniter
i want to show some data taken form a database by querying as following.
$this->db->where('sex !=', $iam);
$this->db->where('sex', $searching_for);
$this->db->where('Age >=' , $age_from);
$this->db->where('Age <=' , $age_to);
if($Province != 1){
$this->db->where('Province' , $Province);
}
$this->db->limit($limit, $start);
$query = $this->db->get("members");
return $query->result_array();
The $iam,$searching_for, $age, $age_to is provided by user and I'm passing them from conttroller file using session array.
$search_info=array(
'iam' => $this->input->post('iam'),
'searching_for' => $this->input->post('searching_for'),
'age_from' => $this->input->post('age_from'),
'age_to' => $this->input->post('age_to'),
'country' => $this->input->post('country'),
'Province' => $this->input->post('Province')
);
$this->session->set_userdata(array("search_info" => $search_info));
and my pagination function is also in controller file and it is like this
public function pagination(){
$this->load->library("pagination");
$config = array();
$config["base_url"] = base_url() . "controller_search/index";
$this->load->model('models_search');
$config["total_rows"] = $this->models_search->search();
$config["per_page"] = 1;
$config["uri_segment"] = 3;
$this->pagination->initialize($config);
//$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;3
//echo $this->uri->segment(3);
//echo ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;3;
$page = $this->uri->segment(3);
$data["search_result"] = $this->models_search->fetch_categories($config["per_page"], $page);
$data["links"] = $this->pagination->create_links();
$data['error'] = '';
$this->load->view('home_header.php');
$this->load->view('search/search_result',$data);
}
But the thing is when i click the page numbers it didn't show any thing, i tried commenting all the where clues in the query and then it works.So i think the error is in session array and i tried to var_dump the session_all so then it shows array(0){}
Can anyone help me in this case ?
Use this code:
$this->session->set_userdata("search_info" => $search_info);
code to set session
Set your session data by this code:
$this->session->set_userdata("sess_data", "mydata");
Retrive this session data by this code:
$this->session->userdata("sess_data");
I think you are accessing session array wrong way.
Instead of
$iam
use
$this->session->userdata('iam');
Or
$this->session->userdata('$iam');
Whichever works in your case....
I just took all the posted values to variables in index function and then i put them in to session array as
$search_info=array(
'iam' => $iam,
'searching_for' => $searching_for,
'age_from' => $age_from,
'age_to' => $age_to,
'country' => $country,
'Province' => $Province
);
$this->session->set_userdata(array("search_info" => $search_info));
then i redirect it to pagination function. then it works neatly. before it was overwrite the post items and the session array when i moving towards pages. now because of the redirect it was stoped. as i think. any way it is working now very well
than you every one for helping me.
specially Mr.John Blake thank you very much sir.
check if you have load the session library.If not either load in your controller as
$this->load->library('session')
or you may auto load session in library of config/autoload.php
I am trying to theme particular pagers on the site and no matter what id I try it doesn't seem to get picked up. This is what I am using to theme all pagers now.
function Subtle_Hightlights_views_mini_pager($tags = array(), $limit = 10, $element = 0, $parameters = array(), $quantity = 9) {
global $pager_page_array, $pager_total;
// Calculate various markers within this pager piece:
// Middle is used to "center" pages around the current page.
$pager_middle = ceil($quantity / 2);
// current is the page we are currently paged to
$pager_current = $pager_page_array[$element] + 1;
// max is the maximum page number
$pager_max = $quantity;
// End of marker calculations.
$li_previous = theme('pager_previous', (isset($tags[1]) ? $tags[1] : t('')), $limit, $element, 1, $parameters);
if (empty($li_previous)) {
$li_previous = "";
}
$li_next = theme('pager_next', (isset($tags[3]) ? $tags[3] : t('')), $limit, $element, 1, $parameters);
if (empty($li_next)) {
$li_next = "";
}
if ($pager_total[$element] > 1) {
$items[] = array(
'class' => 'pager-previous-mini',
'data' => $li_previous,
);
if ($pager_current == 9){
$li_next = "";
}
$items[] = array(
'class' => 'pager-current-mini',
'data' => t('#current of #max', array('#current' => $pager_current, '#max' => $pager_max)),
);
$items[] = array(
'class' => 'pager-next-mini',
'data' => $li_next,
);
return theme('item_list', $items, NULL, 'ul', array('class' => 'pager'));
}
}
now this works to theme all mini pagers but when I try to apply the name of the view and block like this.
function Subtle_Hightlights_views_mini_pager__news_items__block_2($tags = array(), $limit = ``10, $element = 0, $parameters = array(), $quantity = 9) {
does nothing acts as though the code isnt there anymore. Anyone else tried this and it actually worked?
There is a good step by step instructions on how you can overwrite the pager (mini or full). The only step that I would suggest that you do different is when you get to list of theme function names that you can use to override your views pager inside your template.php file, just use this module: Theme developer. Once it's enabled it will provide in a much easier way the suggestions. If that fails, then just try the article's methods.
Drupal 6 Override views pager theme function
I recently upgraded my FuelPHP to v1.5. Now I'm trying to set up the pagination class.
This is my code:
$data['news'] = Model_News::find()->where('font', '>', -1);
$count = count($data['news']);
$config = array(
'pagination_url' => 'http://localhost/public/mobile/barbecue/',
'total_items' => $count,
'per_page' => 12,
'uri_segment' => 3,
);
$pagination = Pagination::forge('mypagination', $config);
$data['news'] = Model_News::find()->where('font', '>', -1)->order_by(array('date' => 'desc'))->limit($pagination->per_page)->offset($pagination->offset)->get();
$data['pagination'] = $pagination->render();
$this->template->title = "Teams";
$this->template->content = View::forge('news/index', $data);
I made a copy of the core/config/pagination.php to app/config/pagination.php.
And on my view news/index.php the pagination is seted: <?php echo $pagination ?>
Result:the information obtained by the query are displayed normally, but the string $ pagination returns nothing. Fuel displays no error.
What I do? Thanks.
In new version of Fuelphp, do this changes in view:
echo html_entity_decode($pagination);
And update the pagination config file.
Below are some change given.
'previous-marker' => "<",
//and
'next-marker' => ">"
In your view, print the pagination with:
<?php echo \Pagination::create_links(); ?>
instead of
<?php echo $pagination ?>
For FuelPHP v1.5+, that would be
<?php echo $pagination->render(); ?>