douxuan4556 2016-04-03 18:41
浏览 47
已采纳

如何实现分页html php mysql

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.

  • 写回答

1条回答 默认 最新

  • donglu4159 2016-04-04 00:19
    关注

    It looks like you are you using some bespoke MVC-ish framework. While it does not answer your question exactly I have a few points:

    1. If you are looking at a lot of users, pagination is the least of your problems. You need to consider how the database is indexed, how the results are returned and much more.
    2. Without understanding the underlying database abstract layer / driver you are using it is difficult to determine whether or not your ->limit($limit, $offset) line will work correctly. The offset should probably default to 0, but without knowing the code it is hard to say.
    3. The ternary operator in your first method (return !empty($results) ? $results : false;) is currently valueless, because the statement before it will mean the variable will always be an array.
    4. Avoid echo statements in controllers. They should return to a templating engine to output a view.
    5. You Users class would be better named User, as the MVC framework implies that the 'Model' is a singular entity.

    While it is not a general rule, most pagination systems I have used have worked on a zero-index system (Page 1 is Page 0), so calculating the limit range is simple:

    $total_records = 1000;
    $max_records = 20;
    $current_page = 0;
    
    $offset = $max_records * $current_page;
    
    $sql = "SELECT * FROM foo LIMIT $offset, $max_records";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名