Changing the URL/URI of pagination in Codeigniter - codeigniter

I'm using the pagination class in Codeigniter to page out some info from my database.
It's all working perfectly, however, i'm trying to tidy up the URLs that people use to access the pages.
Currently, i'm pulling out the offset from the URL and have set it to 20 results per page:
http://www.mysite.com/page/20
So if you go to (in this instance) page two, it starts from db entry 20.
The trouble is that firstly, since i've set it to 20 results per page, it means I get odd URLs like http://www.mysite.com/page/60 which would be page 4.
Secondly it's a double figure which i'd like to tidy up.
Basically want I want to do is set the URLs to look like:
http://www.mysite.com/page/2
http://www.mysite.com/page/3
http://www.mysite.com/page/4
etc...
I set the use page numbers to TRUE, but the trouble is it then sets the offset to 2, 3, 4 etc when it should be 20, 40, 60.
Any ideas? I was thinking of some kind of calculation in the model to work out how many entries it should offset based on the individual page number from the URL (when use page numbers is set to TRUE). I can't seem to get it right though...

In codeigniter number in url means: row per page
So if you need to change per page number with page number change your code like this:
Controller
class news extends CI_Controller{
function index($page = 'page', $page_number = 0){
$row_per_page = 20; // define page per page
// for example if page number == 4 then offset 80
$offset = $page_number * $row_per_page
// we need to request data from database
// we need this query "SELECT * FROM `mytable` limit 80,20"
// offset = 80 AND per page = 20
$this->db->get('mytable', $offser, $row_per_page);
$this->load->library('pagination');
// you can change this url by URI Routing and other solutions.
/*
change application/config/routes.php to nice url
$route['news/page/(:num)'] = "news/index/page/$1";
*/
$config['base_url'] = 'http://example.com/index.php/news/index/page/';
// Change $count_of_rows with your query num_rows you can use SQL_CALC_FOUND_ROWS
$config['total_rows'] = $count_of_wors;
$config['per_page'] = $row_per_page;
$this->pagination->initialize($config);
echo $this->pagination->create_links();
}
}
This is very simple. Enjoy CI

Related

increment function increments by 2 not with 1

Every time user clicks post then that post view should be incremented by 1.
But instead of 1 it is incrementing by 2.
There are many some pages where this post can be clicked.
I have tried using increment function
$view = PostAd::where('id',$id)->first();
$view->increment('viewcount',1);
full code
$view = PostAd::find($id);
$view->increment('viewcount',1);
$data['ads'] = PostAd::find($id);
$data['post']= PostAd::with('postimage')->where('id',$id)->get();
$data['postimage'] = PostAd::with('pimage')->where('id',$id)->get();
$data['details']= PostAd::with('category')->where('id',$id)->first();
$data['comments'] = Comment::where('post_id',$id)->get();
$data['favourite'] = Favourite::where('post_id',$id)->first();
$data['identify'] = PostAd::with(['category','category.children'])->get();
Answer is simple. Use this...
$view->increment('viewcount');
Increment by a custom count (COUNT)
$view = PostAd::where('id',$id)->first();
$view->increment('viewcount',COUNT);
Ex: Increment by 5
$view->increment('viewcount',5);
Read more here
You can use default 1 increment.
$query->increment('viewcount');
If you need custom increment, use
$query->increment('viewcount',increment_value);
OR
PostAd::where('id', $id)
->update('viewcount' => DB::raw('viewcount + 1'));
If you use pagination with json, for example, after it loads the page request, a new request will be created to add ?page=1.
Try to keep the pagination code only in the view that requires pagination. Create a .js file to be listed only on pages that require this functionality. Not globally.

Simple pagination in datatable using ajax without sending total count from server

I'm using DataTables 1.10.5. My table uses server side processing via ajax.
$('#' + id).dataTable({
processing: true,
serverSide: true,
ajax: 'server-side-php-script-url',
"pagingType": "simple_incremental_bootstrap"
});
Everything will work properly if I send 'recordsTotal' in the server response. But I don't want to count the total entries because of performance issues. So I tried to use the pagination plugin simple_incremental_bootstrap. However it is not working as expected. The next button always return the first page itself. If I give 'recordsTotal' in server response this plugin will work properly. I found out that If we don't give 'recordsTotal', the 'start' param sent by datatable to server side script is always 0. So my server side script will always return the first page.
According to this discussion, server side processing without calculating total count is not possible because “DataTables uses the record count that is passed back to it to deal with the paging controls”. The suggested workaround is “So the display records are needed, but it would be possible to just pass back a static number (like 1'000'000 or whatever) which would make DataTables think there are a million rows. You could hide the information element if this information is totally bogus!”
I wonder if anybody have a solution for this. Basically I want to have a simple pagination in my datatable with ajax without sending total count from server.
A workaround worth to try..
If we don't send recordsTotal from server, the pagination won't work properly. If we send a high static number as recordsTotal, table will show an active Next button even if there is no data in next page.
So I ended up in a solution which utilizes two parameters received in ajax script - 'start' and 'length'.
If rows in current page is less than 'limit' there is no data in next page. So total count will be 'start' + 'current page count'. This will disable Next button in the last page.
If rows in current page is equal to or greater than 'limit' there is more data in next pages. Then I will fetch data for next page. If there is at least one row in next page, send recordsTotal something larger than 'start + limit'. This will display an active Next button.
Sample code:
$limit = require_param('length');
$offset = require_param('start');
$current_page_data = fn_to_calculate_data($limit, $offset); // in my case, mysqli result.
$data = “fetch data $current_page_data”;
$current_page_count = mysqli_num_rows($current_page_data);
if($current_page_count >= $limit) {
$next_page_data = fn_to_calculate_data($limit, $offset+$limit);
$next_page_count = mysqli_num_rows($next_page_data);
if($next_page_count >= $limit) {
// Not the exact count, just indicate that we have more pages to show.
$total_count = $offset+(2*$limit);
} else {
$total_count = $offset+$limit+$next_page_count;
}
} else {
$total_count = $offset+$current_page_count;
}
$filtered_count = $total_count;
send_json(array(
'draw' => $params['draw'],
'recordsTotal' => $total_count,
'recordsFiltered' => $filtered_count,
'data' => $data)
);
However this solution adds some load to server as it additionally calculate count of rows in next page. Anyway it is a small load as compared to the calculation total rows.
We need to hide the count information from table footer and use simple pagination.
dtOptions = {};
dtOptions.pagingType = "simple";
dtOptions.fnDrawCallback = function() {
$('#'+table_id+"_info").hide();
};
$('#' + table_id).dataTable(dtOptions);

Return laravel query results in chunks

I have an Update model in my Laravel/Vue.js app, instead of retrieving and displaying all results at once in my component, I want to return them in chunks of fives and place a NEXT FIVE UPDATES link in my component to display the next 5 update records, much like in pagination. I tried this:
$update = Update::limit(5)->get();
This does not achieve my desired result. Is there a Laravel method i can use in my laravel backend to send the results in chunks of 5 to my Vue.Js component and then make my NEXT FIVE UPDATES link display the next 5 records.
If you're able to send some kind of page or offset value to the backend then you could use laravel's skip and take methods: https://laravel.com/docs/5.7/queries#ordering-grouping-limit-and-offset
$skip = $request->page; //assuming this variable exists...
$limit = 5;
$update = Update::skip($skip * $limit)->take($limit)->get();

How to echo page number with codeigniter pagination library?

I am using the codeigniter pagination library to page my search results. I am using a limit and an offset to generate the results.
I would like to know how, apart from in the pagination links, echo the number of the page the user is currently viewing.
For example;
You are currently viewing page X out of Y results.
How is this possible??
This also gives the current page no
$currentPage = floor(($this->uri->segment(n)/$config['per_page']) + 1);
where n is the segment
echo "Current Page:".$currentPage;
You could extend the Pagination class (system/libraries/Pagination.php).
Create a file called MY_Pagination.php and put it in application/libraries/
Add this code to your extend pagination class;
class MY_Pagination extends CI_Pagination {
public function __construct() {
parent::__construct();
}
public function current_place() {
return 'You are currently viewing page '.$this->cur_page.' out of '.ceil(($this->total_rows/$this->per_page)). 'results';
}
}
Don't forget to make sure the class extension prefix is set correctly to MY_ in the config.php (about line 109)
Then you can access it in your controller like;
$current = $this->pagination->current_place();
Which you can pass into your view.
Depending on how you are setting up the offset. Ours is setup with an offset of the perpage. So our default is 10 per page. So for page one the offset is 0. For page two the offset is 10 and so on. So for me to get the current page I would take the offset divided by the current limit plus 1. So ($cur_page/$per_page)+1 would get me the current page.

Bug with pagination when 2 variables in the url on CodeIgniter

I am still learning with the CodeIgniter framework. Today, I find myself faced with the problem of pagination.
Indeed I would have access to data via an id which requires a variable in the url and another one for the pagination.
This is my foos_by_day function (this is not the real name, it's just an example) in my controller called foo that returns an array of foos on a specific day.
These foos come from my database and go to my controller get_foos_by_day ($day) via the model called model_foos.
function foos_by_day($day) {
$configpages['base_url'] = 'http://www.exemple.com/ci/index.php/foo/foos_by_days/'.$foo."/";
$configpages['total_rows'] = count($this->model_foos->get_foos_by_day($foo));
$configpages['per_page'] = 10;
$config['uri_segment'] = 4;
$this->pagination->initialize($configpages);
$data['foos'] = $this->model_foos->get_foos_by_day($jour, (int)$this->uri->segment(4), $configpages['per_page'] );
$this->load->view('foo_view.php', $data);
}
And this is my view:
<?php foreach ($foos as $element): ?>
<div>
Name of the foo : <?=$element->name?><br />
Date of the foo : <?=$element->date?><br />
</div>
<?php endforeach; ?>
<?php echo $this->pagination->create_links(); ?>
I get something strange in my view when I input : http://www.example.com/ci/index.php/foo/foos_by_day/2011-09-28/
The numbering of my 32 foos starts at 4 (the last page). It displays correctly 10 foos per pages but when I click on another page, the selected page stays the fourth.
Moreover, the links behind the page numbers gives me the following statement:
First
<
2
3
<strong>4</strong>
So : nothing, then 20, 10 and 20 (again). In short there is a bug in the pagination.
Have you ever been faced to this kind of situation (with a pagination error)? If so, how did you solve it?
How do you think I should proceed to find a regular pagination?
Thanks in advance.
P.S. : I didn't delete the index.php file but I have learned to do it on an other CodeIgniter.
Change
$config['uri_segment'] = 4;
to
$configpages['uri_segment'] = 4;

Resources