简体   繁体   English

LIMIT在页面中查看数据库中的所有数据

[英]LIMIT View All Data from Database in Page

I'm currently making a student's directory using PHP and MYSQL. 我目前正在使用PHP和MYSQL制作学生的目录。
However I can't control how many students would register and join the site so I'm having a bit of problem in producing the all_students.php page. 但是我无法控制要注册和加入该网站的学生人数,因此在制作all_students.php页面时遇到了一些问题。

The all_students.php page would get all users from the table called students_users in my database. all_students.php页面将从数据库中名为“ students_users的表中获取所有用户 So normally: 所以通常:

$sql = "SELECT * FROM students_users";
$result = mysqli_query($dbcon,$sql);
while ($data=mysqli_fetch_assoc($result)) {
    $name = $data['name'];
    $age = $data['age'];
...
    echo $name;
    echo $age;
...

It's currently working however I noticed that the more users I got, the page becomes as longer and becoming a burden for the users because they have to scroll down for example to names that starts with letter "T". 目前它正在运行,但是我注意到我获得的用户越多,页面变得越长,并成为用户的负担,因为他们必须向下滚动到例如以字母“ T”开头的名称。

Any ideas how can I divide the users to display to a single page (like the search results on Google where it displays about 10 results and then it has this Page 1 | Page 2 | Page 3 |... ? 有什么想法可以将用户划分为一个页面(例如Google上的搜索结果,其中显示大约10个结果,然后显示此Page 1 | Page 2 | Page 3 |... ?)。

I already tried this: 我已经试过了:
"SELECT * FROM students_users Limit 20";
and
"SELECT * FROM students_users LIMIT 20,20"; for the next page and so on 用于下一页等等

My only problem is that, I don't know how exactly is my number of users will grow and that for example if I have say: 400 users and I want to divide the all_students.php in to viewing theses users using like 10 Pages - Limit 20 will not be enough. 我唯一的问题是,我不知道我的用户数量会增长多少,例如,如果我说:400个用户,我想将all_students.php划分为使用10个页面查看这些用户- Limit 20还不够。 I have to use Limit 50 or something right? 我必须使用Limit 50或其他正确的功能?

How can I make it automatically divides all my users in to displaying within the 10 Pages view? 如何使其自动将 所有用户 划分10页视图中的显示?

Example: 例:
10 users - 1 user per page 10个用户-每页1个用户
100 users - 10 users per page 100个用户-每页10个用户
500 users - 50 users per page 500位使用者-每页50位使用者
etc. 等等

without having to constantly change the Limit code as my number of users grow? 无需随着用户数量的增长而不断更改Limit代码?

首先,您需要计算总用户数,然后才能计算每页的记录。

Do the tricks to generate limit dynamically . 做一些技巧动态地产生limit Divide user_count by 10 as you want to ten view page 您想将user_count除以10即可查看十个页面

$sql = " SELECT count(name) as user_count FROM students_users "; 
$result = mysqli_query($dbcon,$sql);
$result = mysqli_fetch_assoc($result);

$number_of_user = $result['user_count'];
$limit = $number_of_user/10;   // Divide user_count by 10 as you want to ten view page 
$limit = round($limit, 0, PHP_ROUND_HALF_DOWN);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM