I am working on a web project in php that could potentially have a lot of users. From an administrative point of view, I am attempting to display a table containing information about each user, and seeing as the users table could grow extremely large, I would like to use pagination.
-
On the backend, I created a service that expects a limit and an offset parameter that will be used to query the database for records within the appropriate range. The service returns the total count of records in the table, along with the records matching the query
public static function getUsersInfo($limit = 50, $offset=1) { $users_count = Users::count( array( "column" => "user_id" ) ); $users_info = array(); $users = Users::query() ->order('created_at') ->limit($limit, $offset) ->execute() ->toArray(); foreach ($users as $index => $user) { $users_info[$index]['user_id'] = $user['user_id']; $users_info[$index]['name'] = $user['first_name'] . " " . $user['last_name']; $users_info[$index] ['phone'] = $user['phone']; $users_info[$index] ['profile_image_url'] = $user['profile_image_url']; } $results = array( 'users_count' => $users_count, 'users_info' => $users_info ); return !empty($results) ? $results : false; }
-
On the frontend, what I would like to achieve ideally is, have the navigation displayed at the bottom of the table, with the typical previous, next buttons, and additionally a few numbers that allow the user to quickly navigate to a desired page if the page number displayed. This is what I have so far for the UsersController, with no pagination.
class UsersController extends ControllerBase { public function indexAction() { $usersObject = new Users(); $data = $usersObject->getUsers(); if ($data['status'] == Constants::SUCCESS) { $users = $data['data']['users_info']; $users_count = $data['data']['users_count']; $this->view->setVar('users', $users); } echo $this->view->render('admin/users'); } public function getUsersAction() { echo Pagination::create_links(15, 5, 1); } }
I don't have any working pagination yet, but I was thinking a good way to go would be to create a Pagination
library with a create_links
function that takes the
-
total_count
of records in the database, so I know how many pages are expected -
limit
so I know how many records to collect -
cur_page
so I know where to start retrieving from
So when that function is called with the correct parameters, it would generate the html code to achieve the pagination, and that in turn can then be passed to the view and displayed.
I have never done this before, but from the research I have done so far, it seems like this might be a good way to approach it. Any guidance, suggestions, or anything at all really, regarding this would be greatly appreciated.