![]() |
How to loop pagination links using for loop instead of using foreach loop - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24) +--- Thread: How to loop pagination links using for loop instead of using foreach loop (/showthread.php?tid=82072) |
How to loop pagination links using for loop instead of using foreach loop - venance - 06-08-2022 Hello everyone, I'm trying to loop link in my pagination template view, I want to be able to check various condition with the counter, for example: PHP Code: <?php for($i = 1; $i <= count($pager->links()); $i++): ?> PHP Code: <?php foreach ($pager->links() as $link) : ?> Thank you in advance! RE: How to loop pagination links using for loop instead of using foreach loop - webdeveloper - 06-08-2022 I really don't know why you need this, but did you try it like below? If it's associative array, it should work. PHP Code: <?php for($i = 0; $i < count($pager->links()); $i++): ?> RE: How to loop pagination links using for loop instead of using foreach loop - venance - 06-08-2022 Thank you @webdeveloper very much! It's working as expected, But I have a question about setting up the counter to begin with 1 instead of 0, PHP Code: <?php I would like to hear any advice or alternatives of doing the same with ci4 pager. RE: How to loop pagination links using for loop instead of using foreach loop - includebeer - 06-08-2022 Why not just add the key to the foreach like this: PHP Code: <?php foreach ($pager->links() as $i => $link) : ?> RE: How to loop pagination links using for loop instead of using foreach loop - venance - 06-08-2022 Thank you very much @webdeveloper and @includebeer !. Since I get trouble about extending Pager class, I have decided to add some methods to straight to the system with namespace: CodeIgniter\Pager in codeigniter4/framework/system/Pager/Pager.php. So I can get the page number only without full url. If is any way to extend pager class I would appreciate. Pager.php PHP Code: public function getNextPageNumber(string $group = 'default') Here its's my custom pagination working. PHP Code: <?php I would like to hear from you about what I did, Also to improve the best pagination that looks like this : prev 1 2 3 4 5 . . . 10 20 Next to the final results. RE: How to loop pagination links using for loop instead of using foreach loop - InsiteFX - 06-09-2022 getCurrentPageNumber() This method returns the page number of the current page. getPageCount() This method returns total number of pages. RE: How to loop pagination links using for loop instead of using foreach loop - venance - 06-09-2022 (06-09-2022, 12:11 AM)InsiteFX Wrote: getCurrentPageNumber() (06-09-2022, 12:11 AM)InsiteFX Wrote: getCurrentPageNumber() RE: How to loop pagination links using for loop instead of using foreach loop - InsiteFX - 06-09-2022 Then you must have something wrong in your code because I use both of those for displaying Page 1 of 25 etc; And they work just fine. RE: How to loop pagination links using for loop instead of using foreach loop - venance - 06-09-2022 (06-09-2022, 04:09 AM)InsiteFX Wrote: Then you must have something wrong in your code because I use both of those for displaying |