简体   繁体   English

分页:获取总页数

[英]Pagination : get total number of page

I am making a web service for an application 我正在为应用程序制作Web服务

I have a SQL Query which gets data in pagination manner. 我有一个SQL查询以分页的方式获取数据。

I want the total number of pages for the data I get. 我想要获取的数据的总页数。

  $limit = 10; // limit of 10 records $offset = ($page_number -1) * $limit; // calculate offset $userQuery="SELECT faq_question, faq_answer FROM `faq` WHERE faq_event_id = $event_id LIMIT $limit OFFSET $offset"; 

Above is my Query. 以上是我的查询。

$page_number would be a parameter to be passed. $ page_number将是要传递的参数。

How can I get total number of page used to display all data based on certain condition. 如何根据特定条件获取用于显示所有数据的页面总数。

Thank You. 谢谢。

// I guess you use mysqli
// use another query without limitation
$result = $mysqli->query("SELECT faq_question, faq_answer FROM `faq` WHERE faq_event_id = $event_id");

// total row count
$row_cnt = $result->num_rows; 

// total pages
$pages = ceil($row_cnt/$limit);  

What I did here, first calculate total rows in database, then I divided total number of rows using limit. 我在这里所做的工作是,首先计算数据库中的总行数,然后使用limit除以总行数。 I used ceil for round value to up.( echo ceil(6.3); // 7 pages) 我使用ceil进行舍入取整。( echo ceil(6.3); // 7页)

You should have to get count of all records and then apply calculation on that. 您应该必须对所有记录进行计数,然后对其进行计算。

count(total_records)/total_records_per_page

it will give you the total pages which you want to get 它会给你你想要得到的总页面

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

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