CodeIgniter Forums
Pagination negative offset - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Development (https://forum.codeigniter.com/forumdisplay.php?fid=6)
+--- Forum: CodeIgniter 3.x (https://forum.codeigniter.com/forumdisplay.php?fid=17)
+--- Thread: Pagination negative offset (/showthread.php?tid=76765)



Pagination negative offset - DarkKnight - 06-17-2020

Imagine page has Pagination,when use follow code to implement Pagination limit

Code:
$data ['list'] = $this->test_model->get_list ( $per_page, $this->uri->segment ( 2 ));

Now lets say user modify segment to -20 (negative value), code will give error


Code:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-20, 20' at line 4

LIMIT -20, 20


how we can solved this issue?

currently I'm using following code to avoid mention issue..is any better way?

Code:
$offset = NULL;
if (ctype_digit($this->uri->segment ( 4 ))) {
$offset = $this->uri->segment ( 4 );
}

$data ['list'] = $this->test_model->get_list ( $per_page, $offset);


Thank You


RE: Pagination negative offset - paulkd - 07-04-2020

Hi,

You could use the max function.

Code:
$data ['list'] = $this->test_model->get_list(max(0, $per_page), $offset);



RE: Pagination negative offset - CINewb - 07-16-2020

You can check that the page isn't negative.

Something like this:

Code:
$page = $this->uri->segment ( 2 ) && $this->uri->segment ( 2 ) > 0 ? (int) $this->uri->segment ( 2 ) : 1;