Home  >  Article  >  Backend Development  >  PHP array implements paging

PHP array implements paging

WBOY
WBOYOriginal
2023-05-24 18:42:081022browse

With the continuous development of Internet technology, the development of Web applications is becoming more and more important. In Web applications, data display and paging are one of the traditional requirements. How to achieve fast paging of data is the key to web application performance and user experience.

In PHP, arrays are commonly used data types. This article will introduce how to use PHP arrays to implement fast paging functionality.

  1. Data preparation

First, we need to get the data from the database or other data source and store it in a PHP array. Suppose we have an array $data containing 100 records. Each record contains three fields: id, name and age.

  1. Paging parameters

Next, we need to define the paging parameters, including the number of records displayed on each page $page_size and the current page number $page_num. These parameters can be obtained from the front end via GET or POST.

  1. Paging calculation

According to the paging parameters, we need to calculate the paging situation of the data. Specifically, based on $page_size and $page_num, the corresponding record is extracted from the $data array and stored in the new array $page_data.

The code is implemented as follows:

// 计算总记录数
$total_num = count($data);
// 计算总页数
$total_page = ceil($total_num / $page_size);
// 获取当前页数
$page_num = isset($_GET['page_num']) ? $_GET['page_num'] : 1;
// 计算起始位置
$start_pos = ($page_num - 1) * $page_size;
// 获取当前页数据
$page_data = array_slice($data, $start_pos, $page_size);
  1. Paging display

Finally, based on the data in the $page_data array, we can implement the paging display function. The specific implementation method can be to use HTML elements such as table tags to display data.

The code is implemented as follows:

// 分页展示
echo "<table>";
echo "<tr><th>id</th><th>name</th><th>age</th></tr>";
foreach ($page_data as $i => $row) {
  echo "<tr>";
  echo "<td>".$row['id']."</td>";
  echo "<td>".$row['name']."</td>";
  echo "<td>".$row['age']."</td>";
  echo "</tr>";
}
echo "</table>";

// 分页导航
echo "<div class="pagination">";
if ($page_num > 1) {
  echo "<a href="?page_num=".($page_num-1)."">上一页</a>";
}
for ($i = 1; $i <= $total_page; $i++) {
  if ($i == $page_num) {
    echo "<a class="active">$i</a>";
  } else {
    echo "<a href="?page_num=$i">$i</a>";
  }
}
if ($page_num < $total_page) {
  echo "<a href="?page_num=".($page_num+1)."">下一页</a>";
}
echo "</div>";

The above code implements the display of paging data and the paging navigation function. We can customize implementation according to specific needs.

Summary:

This article introduces the basic ideas and implementation methods of using PHP arrays to implement paging functions. In actual development, performance optimization and security control can also be improved based on this.

The above is the detailed content of PHP array implements paging. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn