i have error in codeigniter when i want to search month "april" and year "2014" in pagination page 2 back to month "mei"
this is controller
function index($nama='', $bln = 0, $thn = 0)
{
if (!$nama AND $this->input->post('nama'))
{
$nama = $this->input->post('nama');
}
else if(!$nama)
{
$nama = 0;
}
if (!$bln AND $this->input->post('bulan'))
{
$bln = $this->input->post('bulan');
}
else if(!$bln)
{
$bln = 0;
}
if (!$thn AND $this->input->post('tahun'))
{
$thn = $this->input->post('tahun');
}
else if(!$thn)
{
$thn = 0;
}
$count = $this->miuran->get_count_iuran($nama, $bln, $thn);
// Set up pagination
$offset = 0;
$perpage = 5;
$uri_segment = 7;
if ($count > $perpage) {
$this->load->library('pagination');
$this->load->config('pagination');
$config = $this->config->item('pag');
$config['base_url'] = site_url('pelatih/iuran/'.$nama.'/'.$bln.'/'.$thn);
$config['total_rows'] = $count;
$config['per_page'] = $perpage;
$config['uri_segment'] = $uri_segment;
$this->pagination->initialize($config);
$this->data['pagination'] = $this->pagination->create_links();
if($this->uri->segment($uri_segment)){
$offset = $this->uri->segment($uri_segment);
}
}
else
{
$this->data['pagination'] = '';
$offset = 0;
}
$this->data['anggota'] = $this->miuran->get_daftar_iuran($bln, $thn,$nama,$perpage,$offset);
$this->data['title'] ='UKM Taekwondo | iuran';
$this->data['orang'] = $this->mlogin->dataPengguna($this->session->userdata('username'));
$this->data['contents'] = $this->load->view('pelatih/iuran/view_iuran', $this->data, true);
$this->load->view('template/wrapper/pelatih/wrapper_anggota',$this->data);
}
when i switch to page 2 data back to month "mei".
please help me what to do.
thank you.
Have you tried loading the date helper http://ellislab.com/codeigniter/user-guide/helpers/date_helper.html
Related
I using GET method for searching records. now when i have applied pagination i can't understand how to submit data to next pagination page link.
I have tried this answer. but it is just stacking page number in link segments.
example:
From
localhost/phc/search/results/0?s=ai&product_cat=all&post_type=product
To
localhost/phc/search/results/?s=ai&product_cat=all&post_type=product/12/24
so i didn't used that code.
And now my code is:
public function results() {
//pagination
$offset = $this->uri->segment(3);
$limit = 12;
//$offset = 0;
$data['limit'] = $limit;
$config['base_url'] = base_url() . 'search/results/';
$config['first_url'] = base_url() .'search/results/0';
$data['total'] = $this->All_data_model->countSearchProducts($_GET);
$config['total_rows'] = $data['total'];
$config['per_page'] = $limit;
$config['uri_segment'] = 3;
$config['next_link'] = 'Next';
$config['prev_link'] = 'Previous';
$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();
$data['offset'] = $offset;
$data['total_rows'] = $config['total_rows'];
$data['page'] = 'search';
$data['navbar'] = $this->All_data_model->navbarContent();
$data['products'] = $this->All_data_model->searchResults($_GET , $offset , $limit);
$this->template->write_view('content', 'products/search', $data);
$this->template->render();
}
Tell what the best way to submit data in this case if not GET and how can i access this data.
Put offset in your url
localhost/phc/search/results?s=ai&product_cat=all&post_type=product&offset=123
and set these configs.
$config['page_query_string'] = TRUE;
$config['query_string_segment'] = 'offset';
$config['reuse_query_string'] = TRUE;
I changed your code but it's not tested at all.
public function results() {
//pagination
$offset = $this->input->get('offset') ? $this->input->get('offset') : 0;
$limit = 12;
$data['limit'] = $limit;
$config['base_url'] = base_url() . 'search/results/';
$data['total'] = $this->All_data_model->countSearchProducts($_GET);
$config['total_rows'] = $data['total'];
$config['per_page'] = $limit;
// set these mostly...
$config['page_query_string'] = TRUE;
$config['query_string_segment'] = 'offset';
$config['reuse_query_string'] = TRUE;
$config['next_link'] = 'Next';
$config['prev_link'] = 'Previous';
$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();
$data['offset'] = $offset;
$data['total_rows'] = $config['total_rows'];
$data['page'] = 'search';
$data['navbar'] = $this->All_data_model->navbarContent();
$data['products'] = $this->All_data_model->searchResults($_GET , $offset , $limit);
$this->template->write_view('content', 'products/search', $data);
$this->template->render();
}
Hi I'd like some help please. I 'm having two controllers: movies.php and actors.php where I list all actors and all movies in them.
For example this is the method for listing all my movies
public function index() {
// count all movies
$total = $this->movie_model->count();
// set up pagination
$per_page = 10;
if ($total > $per_page) {
$this->load->library('pagination');
$config['base_url'] = site_url($this->uri->segment(1) . '/');
$config['total_rows'] = $total;
$config['per_page'] = $per_page;
$config['uri_segment'] = 2;
$this->pagination->initialize($config);
$this->data['pagination'] = $this->pagination->create_links();
$offset = $this->uri->segment(2);
}
else {
$this->data['pagination'] = '';
$offset = 0;
}
// fetch the movies
$this->db->limit($per_page, $offset);
$this->data['movies'] = $this->movie_model->get();
// load the view
$this->view('movies/index');
}
and for listing all actors
public function index() {
// count all actors
$total = $this->actor_model->count();
// set up pagination
$per_page = 10;
if ($total > $per_page) {
$this->load->library('pagination');
$config['base_url'] = site_url($this->uri->segment(1) . '/');
$config['total_rows'] = $total;
$config['per_page'] = $per_page;
$config['uri_segment'] = 2;
$this->pagination->initialize($config);
$this->data['pagination'] = $this->pagination->create_links();
$offset = $this->uri->segment(2);
}
else {
$this->data['pagination'] = '';
$offset = 0;
}
// fetch the movies
$this->db->limit($per_page, $offset);
$this->data['actors'] = $this->actor_model->get();
// load the view
$this->view('actors/index');
}
I have set my routes like this
$route['default_controller'] = "movies";
$route['404_override'] = '';
$route['movies'] = 'movies/index';
$route['actors'] = 'actors/index';
And the urls are like this
h**p: //localhost/www/task/public/ // for movies (default controller)
h**p: //localhost/www/task/public/actors // for actors controller
The problem is when I try to click a panination link to get the next records in each controller I get a 404 error. I have tried to change my config settings in pagination but no luck.
Any help would be appreciated.
I have link here very help full for pagination and sortable table links this may help.
http://forum.codeigniter.com/thread-1198.html
Change $config['base_url'] = site_url($this->uri->segment(1) . '/'); to,
$config['base_url'] = base_url().'movies/index';
and
$config['base_url'] = base_url().'actors/index';
And $config['uri_segment'] = 3;
hi every one i am trying to display mysql database records using codeigniter pagination..for that i written the following below codes..and when i run this it shows me all the results in one page while the page numbers are displayed right according to the given per_page limit..
Model:
function get_records($user_name, $limit, $start) {
$this->db->limit($limit, $start);
$this->db->select('GROUP_CONCAT(cid) AS cIDs');
$this->db->where('user_name', $user_name);
$this->db->group_by("company_user");
$this->db->from('company_records');
$query = $this->db->get();
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
function count_records($user_name) {
$this->db->select('GROUP_CONCAT(cid) AS cIDs');
$this->db->where('user_name', $user_name);
$this->db->group_by("company_user");
return $this->db->count_all('company_records');
}
Controller:
$this->load->model('Records', 'Records', TRUE);
$this->load->library('pagination');
$config['base_url'] = base_url() . 'records/index';
$config['total_rows'] = $this->Records->count_records($this->session->userdata('user'));
$config['per_page'] = 10;
$config["uri_segment"] = 4;
$config['use_page_numbers'] = TRUE;
$config['full_tag_open'] = '<div id="pagination">';
$config['full_tag_close'] = '</div>';
$this->pagination->initialize($config);
/* $start = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0; */
if ($this->uri->segment(4) > 0) {
$offset = ($this->uri->segment(4) + 0) * $config['per_page'] - $config['per_page'];
} else {
$offset = $this->uri->segment(4);
}
$data['recordsDetails'] = $this->Records->get_records($this->session->userdata('user'), $config["per_page"], $offset);
$data['pages'] = $this->pagination->create_links();
i have tried google search and stackoverflow.. but not yet found any solutions to this..may be i have issue with the query or i dont know..
please help me...
I used this code for pagination.
In the model
//Search for results
public function search($id, $table, $pageNumber) {
//Get the number of pages
$numOfPage = $this->findResults($id, $table, RETURN_NUM_OF_PAGES, $pageNumber);
if ($numOfPage < 1) {
return false; //If there are no search results return false
} else {
$row = array();
$res = $this->findResults($id, $table, RETURN_RESULTS, $pageNumber);
for ($j = 0; $j < $res->num_rows(); $j++) {
$row[$j] = $res->row($j);
}
return $row; //Return the results
}
}
// Find the results from DB and return number of pages/ results
function findResults($id, $table, $returnType, $pageNumber) {
$this->db->where('id', $id);
$this->db->order_by('reputation', 'desc'); //Order the users by reputation
$itemsPerPage = 4;
$startingItem = ($pageNumber - 1) * $itemsPerPage;
if ($returnType == RETURN_RESULTS) {
$res = $this->db->get($table, $itemsPerPage, $startingItem);
return $res; //Return the results
} else { //Return the number of pages
$res = $this->db->get($table);
$count = $res->num_rows(); //Get the total number of results
$numofPages = (int) ($count / $itemsPerPage);
if ($count % $itemsPerPage != 0)
$numofPages = $numofPages + 1; //If the number of items < $itemsPerPage there should be 1 page
return $numofPages; //Return the number of pages
}
}
So from the controller you have to call the function search($id, $table, $pageNumber).
Also the constants were defined in config/constants.php file as follows.
define('RETURN_NUM_OF_PAGES', 0);
define('RETURN_RESULTS', 1);
public function viewconsultant($id1='')
{
$id1 = $id1 ? $id1 : 1;
$pagenum=5;
$start=($id1-1)*$pagenum;
$users = $this->db->get_where('connections', array('user_id'=>97));
$pageall = count($users);
$config['total_rows']=$pageall;
$config['per_page']=$pagenum;
$config['num_links']=4;
$config['base_url']="localhost/~chrisfu/linkedin2/index.php/connections/viewconsultant";
$config['use_page_numbers']=true;
$this->load->library('pagination');
$this->pagination->initialize($config);
$query = $this->db->get_where('connections', array('user_id'=>97), $pagenum, $start);
$data['connections'] = $query->result();
$this->load->view('profile_view',$data);
}
The create_links() does not work well, it just displays a blank string. I tried this localhost/~chrisfu/linkedin2/index.php/connections/viewconsultant/2, it can jump to second page, but not pagination links created blow the table. please help!!
The problem is that you are not creating the links..
$this->load->library('pagination');
$this->pagination->initialize($config);
// add the line below to your code
$data['links'] = $this->pagination->create_links();
// debug
// echo $data['links'];
$query = $this->db->get_where('connections', array('user_id'=>97), $pagenum, $start);
$data['connections'] = $query->result();
$this->load->view('profile_view',$data);
Also, you can omit the first line in your method by doing the following:
public function viewconsultant($id1=1)
{
// line below no longer necessary
//$id1 = $id1 ? $id1 : 1;
}
Try this:
function viewconsultant($id1=1, $offset = 0){
$this->load->library('pagination');
$pagenum=5;
//$users = $this->db->where('connections', array('user_id'=>97))->limit('20', $offset);
$total = $this->db->where('connections', array('user_id'=>97));
$pageall = $total->num_rows();
//$users = $this->db->get_where('connections', array('user_id'=>97));
//$start=($id1-1)*$pagenum;
//$pageall = count($users);
$config['total_rows'] = $pageall;
$config['per_page'] = $pagenum;
$config["uri_segment"] = 3;
$config['num_links'] = 4;
$config['base_url'] = base_url()."/index.php/connections/viewconsultant/";
$config['use_page_numbers'] = true;
$this->load->library('pagination');
$this->pagination->initialize($config);
$query = $this->db->get_where('connections', array('user_id'=>97), $pagenum, $this->uri->segment(3));
$data['connections'] = $query->result();
$data['links'] = $this->pagination->create_links();
$this->load->view('profile_view',$data);
}
Now, just echo $links in your view file to get the pagination.
There is a bit of problem in my code which i am not able to solve.
I m using CI 1.7.2.
I have implemented the CI Pagination into the system correctly.
The results are displayed fine but the links in the pagination are not rendering correctly.
For eg. If i click on the page 2 then the results are displayed as per the 2nd Page but the current link at pagination numbers remains 1 which should change to 2.
Here is the code that has been implemented
$total = $this->bmodel->countResultsBanner();
$data['total'] = $total;
$uri_segment = $this->uri->segment(4);
if($uri_segment == 0 || empty($uri_segment)){
$uri_segment = 0;
}
$perPage = 5;
$config['base_url'] = base_url()."index.php/modules/banner/index";
$config['total_rows'] = $total;
$config['per_page'] = $perPage;
$config['num_links'] = 4;
//$config['cur_tag_open'] = '<b><span class="current_page">';
//$config['cur_tag_close'] = '</span></b>';
$this->pagination->initialize($config);
$result = $this->bmodel->getAllBanners($perPage,$uri_segment);
$data['result'] = $result;
thanks in advance.
J
Heyy,
I also faced the same problem. In the end, solution turned out to be very simple. :)
by default CI assumes that uri segment used for pagination is (3). Which in your case, for you (i am assuming shamelessly) is incorrect.
$config['base_url'] = base_url()."index.php/modules/banner/index";
$config['total_rows'] = $total;
$config['per_page'] = $perPage;
$config['num_links'] = 4;
$config['uri_segment'] = 3; /* segment of your uri which contains the page number */
$this->pagination->initialize($config);
Hope this solves your problem
ok... try this...
$total = $this->bmodel->countResultsBanner();
$data['total'] = $total;
/* Comment out this part
$uri_segment = $this->uri->segment(4);
if($uri_segment == 0 || empty($uri_segment)){
$uri_segment = 0;
}
*/
$perPage = 5;
$config['base_url'] = base_url()."index.php/modules/banner/index";
$config['total_rows'] = $total;
$config['per_page'] = $perPage;
$config['num_links'] = 4;
//$config['cur_tag_open'] = '<b><span class="current_page">';
//$config['cur_tag_close'] = '</span></b>';
$this->pagination->initialize($config);
/*Change the following line*/
$result = $this->bmodel->getAllBanners($perPage,$this->uri->segment(5));
$data['result'] = $result;
$this->load->library('pagination');
$config['base_url']="http://localhost/CodeIgniter/pagination";
$config['per_page']=2;
$config['total_rows']= $this->db->get('record')->num_rows();
$this->pagination->initialize($config);
$data['query']= $this->db->get('record',$config['per_page'], $this->uri->segment(3));
$this->load->view('pagination',$data);
$config['uri_segment'] = num; /* where num is the uri segment where you have page number */
Try this, it might help.
class Admin_model extends CI_Model {
public function __construct() {
parent::__construct();
}
public function get_product($search, $page, $perpage) {
$page = $page - 1;
$page < 0 ? $page = 0 : $page = $page;
$from = $page * $perpage;
$query = $this->db
->select('*')
->from('tbl_product')
->limit($perpage, $from)
->get();
return $query->result();
}
}