how to send value through function in codeigniter - codeigniter

public function index_pagination(){
$this->load->library("pagination");
$config = array();
$config["base_url"] = base_url("index/index");
$config["total_rows"] = $this->index_model->record_count();
$config["per_page"] = 2;
$config["uri_segment"] = 3;
// Styling pagination
$config["full_tag_open"] = "<ul class='pagination'>";
$config["full_tag_close"] = "</ul>";
$config["next_tag_open"] = "<li>";
$config["next_tag_close"] = "</li>";
$config["prev_tag_open"] = "<li>";
$config["prev_tag_close"] = "</li>";
$config["num_tag_open"] = "<li>";
$config["num_tag_close"] = "</li>";
$config["cur_tag_open"] = "<li class='active'><a>";
$config["cur_tag_close"] = "</li></a>";
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data["page"] = $this->index_model->fetch_products($config["per_page"], $page);
$data["links"] = $this->pagination->create_links();
}
public function index(){
$this->index_pagination();
$this->load->view('frontend/index', $data);
}
How can I pass $data["page"] and $data["links"] from index_pagination() function to my view file through index function?

You can do like this..
<?php
public function index(){
$data = array();
$data = $this->index_pagination();
$this->load->view('frontend/index', $data);
}
public function index_pagination(){
$this->load->library("pagination");
$config = array();
$config["base_url"] = base_url("index/index");
$config["total_rows"] = $this->index_model->record_count();
$config["per_page"] = 2;
$config["uri_segment"] = 3;
// Styling pagination
$config["full_tag_open"] = "<ul class='pagination'>";
$config["full_tag_close"] = "</ul>";
$config["next_tag_open"] = "<li>";
$config["next_tag_close"] = "</li>";
$config["prev_tag_open"] = "<li>";
$config["prev_tag_close"] = "</li>";
$config["num_tag_open"] = "<li>";
$config["num_tag_close"] = "</li>";
$config["cur_tag_open"] = "<li class='active'><a>";
$config["cur_tag_close"] = "</li></a>";
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data["page"] = $this->index_model->fetch_products($config["per_page"], $page);
$data["links"] = $this->pagination->create_links();
return $data;
}

I wrote two simple function in my controller, and used it. This is may not answer of your question but by using this at least you go DRY.
/**
* This function used provide the pagination resources
* #param {string} $link : This is page link
* #param {number} $count : This is page count
* #param {number} $perPage : This is records per page limit
* #return {mixed} $result : This is array of records and pagination data
*/
function paginationCompress($link, $count, $perPage = 10) {
$this->load->library ( 'pagination' );
$config ['base_url'] = base_url () . $link;
$config ['total_rows'] = $count;
$config ['uri_segment'] = SEGMENT;
$config ['per_page'] = $perPage;
$config ['num_links'] = 5;
$config ['full_tag_open'] = '<nav><ul class="pagination">';
$config ['full_tag_close'] = '</ul></nav>';
$config ['first_tag_open'] = '<li class="arrow">';
$config ['first_link'] = 'First';
$config ['first_tag_close'] = '</li>';
$config ['prev_link'] = 'Previous';
$config ['prev_tag_open'] = '<li class="arrow">';
$config ['prev_tag_close'] = '</li>';
$config ['next_link'] = 'Next';
$config ['next_tag_open'] = '<li class="arrow">';
$config ['next_tag_close'] = '</li>';
$config ['cur_tag_open'] = '<li class="active"><a href="#">';
$config ['cur_tag_close'] = '</a></li>';
$config ['num_tag_open'] = '<li>';
$config ['num_tag_close'] = '</li>';
$config ['last_tag_open'] = '<li class="arrow">';
$config ['last_link'] = 'Last';
$config ['last_tag_close'] = '</li>';
$this->pagination->initialize ( $config );
$page = $config ['per_page'];
$segment = $this->uri->segment ( SEGMENT );
return array (
"page" => $page,
"segment" => $segment
);
}
function userListing()
{
$this->load->model('user_model');
$this->load->library('pagination');
$count = $this->user_model->userListingCount();
$returns = $this->paginationCompress ( "userListing/", $count, 5 );
$data['userRecords'] = $this->user_model->userListing($returns["page"], $returns["segment"]);
$this->load->view('includes/header');
$this->load->view("users", $data);
$this->load->view('includes/footer');
}
Try this with your piece of code.
You can go for this repository to understand more https://github.com/kishor10d/Admin-Panel-User-Management-using-CodeIgniter

Related

How to show continued numbered of data with pagination

I am trying to display data in an ordered list with pagination in codeigniter. The numbered works fine in first page number 1 until limit, but in the next page it counts down start to 1, not continuing from previous page.
here is the controller code :
public function index()
{
// init params
$params = array();
$limit_per_page = 2;
$start_index = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$total_records = $this->model_pesan->get_total();
$data['setting'] = $this->setting_model->get_file();
if ($total_records > 0)
{
// get current page records
$params["results"] = $this->model_pesan->get_current_page_records($limit_per_page, $start_index);
$params["ruangan"] = $this->ruangan_model->get_file();
$config['base_url'] = base_url() . 'home/index';
$config['total_rows'] = $total_records;
$config['per_page'] = $limit_per_page;
$config["uri_segment"] = 3;
$config['full_tag_open'] = '<nav><ul class="pagination justify-content-center">';
$config['full_tag_close'] = '</ul></nav>';
$config['num_tag_open'] = '<li class="page-item"><span class="page-link">';
$config['num_tag_close'] = '</span></li>';
$config['cur_tag_open'] = '<li class="page-item active"><span class="page-link">';
$config['cur_tag_close'] = '<span class="sr-only">(current)</span></span></li>';
$config['next_tag_open'] = '<li class="page-item"><span class="page-link">';
$config['next_tagl_close'] = '<span aria-hidden="true">»</span></span></li>';
$config['prev_tag_open'] = '<li class="page-item"><span class="page-link">';
$config['prev_tagl_close'] = '</span></li>';
$config['first_tag_open'] = '<li class="page-item"><span class="page-link">';
$config['first_tagl_close'] = '</span></li>';
$config['last_tag_open'] = '<li class="page-item"><span class="page-link">';
$config['last_tagl_close'] = '</span></li>';
$this->pagination->initialize($config);
// build paging links
$params["links"] = $this->pagination->create_links();
}
$this->load->view('template/header_user', $data);
$this->load->view('user/home', $params);
$this->load->view('template/footer_user', $data);
}
and this is the model code :
public function get_current_page_records($limit, $start)
{
$query = $this->db->query('set #row_number = 0');
$this->db->select('(#row_number:=#row_number + 1) as num ,reservasi.id_reservasi,DATE_FORMAT(reservasi.tanggal, "%d-%m-%Y") as tanggal, reservasi.acara, reservasi.waktu_mulai, reservasi.waktu_selesai, reservasi.keterangan, ruangan.nama_ruangan, user.nama, reservasi.organisasi, reservasi.notelp');
$this->db->from('reservasi');
$this->db->join('user', 'reservasi.id_user = user.id');
$this->db->join('ruangan', 'reservasi.id_ruangan = ruangan.id_ruangan');
$this->db->limit($limit, $start);
$query = $this->db->get();
if ($query->num_rows() > 0)
{
foreach ($query->result() as $row)
{
$data[] = $row;
}
return $data;
}
return false;
}
public function get_total()
{
return $this->db->count_all_results('reservasi');
}
the ouput should be =
first page :
1
2
3
4
then in the next page :
5
6
7
8
Try to set the row_number to $start+1 instead of 0 on the model, so it will not start from 0 again on every page requested :
$query = $this->db->query('set #row_number = '.$start+1);

Why do i get the same values in all pages in CI pagination?

I'm kinda new in CI and i'm trying to create a proper pagination for my page.
I have the following model and controller and even if i manage to get the pagination links, when i visit the other pages i get the same entries again and again.
I also have applied an Ajax "GET" filtering for fetching results from db and i managed to reuse the url parameters when i change pages.
The problem is that i don't know if i should try to create an infinite scroll function or maintain the default CI pagination.
I'd like to hear your opinions too!
Thank you in advance!
public function get_filtered_pets($limit, $start){
$this->db->order_by('pet_created_at', 'DESC');
$this->db->join('users', 'users.user_id = pets.pet_user_id');
$this->db->join('pet_categories', 'pet_categories.pet_category_id = pets.category_id');
$pet_data = array();
if ( $this->input->get('petStatus') ) {
$pet_data['pet_status'] = $this->input->get('petStatus');
}
if ( $this->input->get('petCategory') ) {
$pet_data['category_id'] = $this->input->get('petCategory');
}
if ( $this->input->get('petGender') ) {
$pet_data['pet_gender'] = $this->input->get('petGender');
}
if ( !empty($pet_data) ) {
$this->db->where($pet_data);
}
$query = $this->db->get('pets');
if ($start > 0) {
$this->db->last_query();
}
return $query->result_array();
}
And my controller
public function index(){
$query = $this->db->get('pets');
$pet_num = $query->num_rows();
$config = array();
$config["base_url"] = base_url() . "pets/index/";
$config["total_rows"] = $pet_num;
$config["per_page"] = 12;
$config['use_page_numbers'] = TRUE;
$config['reuse_query_string'] = TRUE;
$config['attributes'] = array('class' => 'page-link');
$config["full_tag_open"] = '<ul class="pagination">';
$config["full_tag_close"] = '</ul>';
$config["first_link"] = "«";
$config["first_tag_open"] = "<li>";
$config["first_tag_close"] = "</li>";
$config["last_link"] = "»";
$config["last_tag_open"] = "<li>";
$config["last_tag_close"] = "</li>";
$config['next_link'] = '>';
$config['next_tag_open'] = '<li>';
$config['next_tag_close'] = '<li>';
$config['prev_link'] = '<';
$config['prev_tag_open'] = '<li class="page-item">';
$config['prev_tag_close'] = '<li>';
$config['cur_tag_open'] = '<li class="page-item active"><a class="page-link" href="#">';
$config['cur_tag_close'] = '</a></li>';
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data['pets'] = $this->pet_model->get_filtered_pets($config["per_page"], $page);
$data["links"] = $this->pagination->create_links();
//View files
$this->load->view('templates/header', $data);
$this->load->view('pets/index', $data);
$this->load->view('templates/footer', $data);
}
You are not using the value of $limit and $start nowhere. See Limiting or Counting Results.
You should:
public function get_filtered_pets($limit, $start){
$this->db->limit($limit, $start);
/* ... */
return $query->result_array();
}
And also, when calling this function you have to multiply: per_page * page, to get the offset (starting index).
$this->pet_model->get_filtered_pets($config["per_page"], $page * $config["per_page"])
To count results correctly, I normally have two functions on model: "get($search, $offset)" and "count($search)".
The function count has exactly the same code on get, except the select fields.
See this code, I think you will get it: https://pastebin.com/dZG90Lzm.
ps: There's ways to optimize it and avoid duplicate code.

Codeigniter Perpage Link Not Showing First In URL Pagination links

I am making my table head links sortable and orderable how ever my per_page does not show up first
http://localhost/project-1/forum/category/1/&order=asc?per_page=1
It should show like
http://localhost/codeigniter/forum/category/1/?per_page=1&order=asc
Question when I click on the pagination links if the have been order or sorted how can I make sure that when I click on a pagination link even though has been order/sorted will still allways show ?per_page first in url.
$url = '';
if ($this->input->get('sort')) {
$url .= '&sort=' . $this->input->get('sort');
}
if ($this->input->get('order')) {
$url .= '&order=' . $this->input->get('order');
}
$config["base_url"] = base_url('forum/category') .'/'. $category_id .'/'. $url;
Controller
<?php
class Forum extends MX_Controller {
public function __construct() {
parent::__construct();
$this->load->model('catalog/forum/forum_model');
$this->load->library('pagination');
}
public function category() {
$this->document->set_title('Forums');
$category_id = $this->uri->segment(3);
if ($this->input->get('sort')) {
$sort = $this->input->get('sort');
} else {
$sort = 'message';
}
if ($this->input->get('order')) {
$order = $this->input->get('order');
} else {
$order = 'asc';
}
$url = '';
if ($this->input->get('per_page')) {
$url .= '?per_page=' . $this->input->get('per_page');
}
if ($this->input->get('sort')) {
$url .= '&sort=' . $this->input->get('sort');
}
if ($this->input->get('order')) {
$url .= '&order=' . $this->input->get('order');
}
$config["base_url"] = base_url('forum/category') .'/'. $category_id .'/'. $url;
$config["total_rows"] = $this->forum_model->total_category($category_id);
$config["per_page"] = 1;
$config['page_query_string'] = TRUE;
$config['full_tag_open'] = "<ul class='pagination'>";
$config['full_tag_close'] ="</ul>";
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
$config['cur_tag_open'] = "<li class='disabled'><li class='active'><a href='#'>";
$config['cur_tag_close'] = "<span class='sr-only'></span></a></li>";
$config['next_tag_open'] = "<li>";
$config['next_tagl_close'] = "</li>";
$config['prev_tag_open'] = "<li>";
$config['prev_tagl_close'] = "</li>";
$config['first_tag_open'] = "<li>";
$config['first_tagl_close'] = "</li>";
$config['last_tag_open'] = "<li>";
$config['last_tagl_close'] = "</li>";
$this->pagination->initialize($config);
$start = ($this->input->get('per_page')) ? $this->input->get('per_page') : '';
$filter_data = array(
'limit' => $config["per_page"],
'start' => $start,
'category_id' => $category_id,
'sort' => $sort,
'order' => $this->input->get('order')
);
$data['threads'] = array();
$results = $this->forum_model->get_threads_for_forum($filter_data);
foreach ($results as $result) {
$data['threads'][] = array(
'thread_id' => $result['thread_id'],
'user_id' => $result['user_id'],
'username' => $result['username'],
'subject' => $result['subject'],
'link' => site_url('thread') . '-' . $result['thread_id'],
'total' => $this->forum_model->total_threads($result['thread_id']),
'date_created' => date('d-m-Y', strtotime($result['date_created'])),
'user_link' => site_url('user') . '-' . $result['user_id']
);
}
$data['header'] = Modules::run('catalog/common/header/index');
$data['footer'] = Modules::run('catalog/common/footer/index');
$data['menu'] = Modules::run('catalog/common/menu/index');
$data['pagination'] = $this->pagination->create_links();
$data['back'] = site_url('forum');
$data['thread'] = site_url('newthread') . '?fid=' . $category_id;
$url = '';
if ($order == 'asc') {
$url .= '?order=desc';
} else {
$url .= '?order=asc';
}
$data['message'] = site_url('forum/category') .'/'. $category_id .'/'. $url;
$data['sort'] = $sort;
$data['order'] = '';
$this->load->view('default/template/forum/forum_thread_view', $data);
}
}
Set your default category id if url does not contain:
if ($this->uri->segment(3) == 'null') {
$category_id = 1; //set default for null
} else {
$category_id = $this->uri->segment(3);
}
Edit your config['base_url'] and add set $config['suffix'] if $_GET is not
empty.
New Pagination config:
$config["base_url"] = base_url('forum/category'); //no need custom category id
if (count($_GET) > 0)
$config['suffix']='?'.http_build_query($_GET,'',"&");//to encode requested data
$config["per_page"] = 1;
$config['page_query_string'] = TRUE;
$config['full_tag_open'] = "<ul class='pagination'>";
$config['full_tag_close'] ="</ul>";
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
$config['cur_tag_open'] = "<li class='disabled'><li class='active'><a href='#'>";
$config['cur_tag_close'] = "<span class='sr-only'></span></a></li>";
$config['next_tag_open'] = "<li>";
$config['next_tagl_close'] = "</li>";
$config['prev_tag_open'] = "<li>";
$config['prev_tagl_close'] = "</li>";
$config['first_tag_open'] = "<li>";
$config['first_tagl_close'] = "</li>";
$config['last_tag_open'] = "<li>";
$config['last_tagl_close'] = "</li>";
I don't see any:
$config[‘reuse_query_string’] = TRUE;
This will allow to use mixed type of urls. This was not possible before CI 3.0.

code igniter pagination for displaying specific data from database?

I want to display some specific data from database through pagination using CI.but pagination by default passess parameter in function which creates problem for me.
Here is my Controller
function page($catagoryid){
$this->load->library('pagination');
$config['base_url'] = base_url().'index.php/pagination/page/';
$count = $this->db->query("select * from tbl_products where category_id='$categoryid'");
$total=$count->num_rows();
$per_page = 4;
$config['total_rows'] = $total;
$config['per_page'] = $per_page;
$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();
$this->load->model("mpagination");
$data['list'] = $this->mpagination->get_s(,$categoryid,$config['per_page'],$this->uri->segment(3));
if ($data['list']!== null){
$this->load->view('pagination_view', $data);
}
else {
$this->load->view('noresult');
}
}
Here is my Model
function get_s($categoryid,$num, $offset) {
$this->db->select ('*');
->where('category_id',$categoryid); // field name
$sql = $this->db->get('tbl_products',$num, $offset); // table name
if ($sql->num_rows () >0) {
return $sql->result();
}
else {
return null;
}
}
Here is my view
<?php
foreach($list as $row):?>
<?php echo $row->_prduct_id." ".$row->name." ".$row->description;?>
<br/>
<?php endforeach;?>
<br/>
<?php echo $pagination; ?>
This code work at first but when i clicked on second link of pagination it will not work because by default pagination sends its value to url as parameter and doesnot show data.
what can i do?
I didn't understand your code properly but this is the way you need to do pagination.
public function page($category_id)
{
$result = $this->model_file->model_function($category_id);
$start_index = 0;
$total_rows = count($result);
$items_per_page = 10;
$filtered = array_splice($result,$start_index,$items_per_page);
$model['data'] = $filtered;
$model['page_link'] = create_page_links (base_url()."/controller_file_name/page", $items_per_page, $total_rows);
$this->load->view('view_file_name_path',$model);
}
function create_page_links($base_url, $per_page, $total_rows) {
$CI = & get_instance();
$CI->load->library('pagination');
$config['base_url'] = $base_url;
$config['total_rows'] = $total_rows;
$config['per_page'] = $per_page;
$CI->pagination->initialize($config);
return $CI->pagination->create_links();
}
Try like this
public function index($offset = 0)
{
$language_id = 1;
$this->load->library('pagination');
$limit = 10;
$total = $this->legend_model->get_legend_count($language_id);
$config['base_url'] = base_url().'legend/index/';
$config['total_rows'] = $total;
$config['per_page'] = $limit;
$config['uri_segment'] = 3;
$config['first_link'] = '<< First';
$config['last_link'] = 'Last >>';
$config['next_link'] = 'Next ' . '>';
$config['prev_link'] = '<' . ' Previous';
$config['num_tag_open'] = '<span class="number">';
$config['num_tag_close'] = '</span>';
$config['cur_tag_open'] = '<span class="current"><a href="#">';
$config['cur_tag_close'] = '</a></span>';
$this->pagination->initialize($config);
$data['offset'] = $offset;
$data['legends'] = $this->legend_model->get_legend($language_id, $limit, $offset);
$this->template->write('title', 'Legend : Manage Legend');
$this->template->write_view('content', 'legend/index', $data);
$this->template->render();
}

codeigniter pagination links doesnt work properly

I am using Codeigniter library pagination for my search.The problem is even if i have 2 results from the database table,clicking the next or previous link shows only one result.
code :
controller
function search()
{
$this->load->library('pagination');
$perpage =1;
$page = 1;
$search_content = $this->input->get('con_search');
$data->search = $this->public_model->search_content($search_content, $perpage);
$total = count($this->public_model->search_content($search_content));
$config['base_url'] = current_url()."?con_search=".$search_content;
$config['total_rows'] = $total;
$config['per_page'] = 1;
$config['cur_page'] = $page;
$config['use_page_numbers'] = TRUE;
$config['query_string_segment'] = "page";
$config['display_pages'] =TRUE;
$config['page_query_string'] = TRUE;
$config['cur_tag_open'] = '<span class="current">';
$config['cur_tag_close'] = '</span>';
$config['next_link'] = 'Next';
$config['prev_link'] = 'Previous';
$config['prev_tag_open'] = '<span class="prevtag">';
$config['prev_tag_close'] = '</span>';
$config['next_tag_open'] = '<span class="nexttag">';
$config['next_tag_close'] = '</span>';
$this->pagination->initialize($config);
$data->footer = $this->public_model->get_footer();
$this->load->helper('text');
$this->load->view('header', $data);
$this->load->view('search');
$this->load->view('footer');
}
model:
function search_content($search_txt, $perpage = 0)
{
if($perpage){
$this->db->limit($perpage);
}
$this->db->select('title, description, alias');
$this->db->where('status', 'yes');
$this->db->like('description', $search_txt);
$res = $this->db->get('tbl_content')->result_array();
return $res;
}
view :
if($search && is_array($search)) {
foreach($search as $ind=>$val)
{
$this->db->select('menu_type_id');
$this->db->where('menu_alias', $val['alias']);
$res = $this->db->get('tbl_menu')->row_array();
if($res) {
if($res['menu_type_id'] == 2)
$url = site_url('page/'.$val['alias']);
else
$url = site_url('page/footer/'.$val['alias']);
}
?>
<div class="box_1">
<div class="deal_contents">
<div class="bx_0"><?php echo $val['title'] ?></div>
<div class="bx_1" style="width:670px;"><?php echo strip_tags(character_limiter($val['description'],200)) ?></div>
<div class="bx_2" style="text-align:right; width:640px;">Read More</div>
</div>
</div>
<?php
}
echo $this->pagination->create_links();
}
thanks in advance
Do like this
$this->load->library('pagination');
$limit = 10;
$total = $this->legend_model->get_legend_count($language_id);
$config['base_url'] = base_url().'legend/index/';
$config['total_rows'] = $total;
$config['per_page'] = $limit;
$config['uri_segment'] = 3;
$config['first_link'] = '<< First';
$config['last_link'] = 'Last >>';
$config['next_link'] = 'Next ' . '>';
$config['prev_link'] = '<' . ' Previous';
$config['num_tag_open'] = '<span class="number">';
$config['num_tag_close'] = '</span>';
$config['cur_tag_open'] = '<span class="current"><a href="#">';
$config['cur_tag_close'] = '</a></span>';
$this->pagination->initialize($config);
$data['offset'] = $offset;
$data['legends'] = $this->legend_model->get_legend($language_id, $limit, $offset);
$this->template->write('title', 'Legend : Manage Legend');
$this->template->write_view('content', 'legend/index', $data);
$this->template->render();

Resources