Home >Backend Development >PHP Problem >How to implement paging and deletion functions in php

How to implement paging and deletion functions in php

PHPz
PHPzOriginal
2023-04-19 10:06:13665browse

With the rapid development of the Internet, Web development technology is becoming more and more mature. As one of the most widely backed development languages, PHP has evolved into a powerful language that can be used to build complex applications. When building web applications, the implementation of pagination and deletion features is very important because they can greatly improve the user experience and simplify user interaction with the website. In this article, we will introduce how to implement paging and deletion functions using PHP.

  1. Paging function implementation

When we need to display a large amount of data, displaying all the data on the same page will make the page very slow and may even cause browsing The server crashes. To deal with this problem, we can implement paging functionality.

The basic idea of ​​paging is to divide data into different pages for display. Generally speaking, we should display summary information about the data on the page, such as the title, picture, price, etc. of the data. Additionally, we need to provide some page links to allow users to explore more data.

In PHP, we can use MySQL or other database systems to manage data. In order to implement paging, we need to know the following information:

  • The page number currently to be displayed
  • How many pieces of data each page should contain
  • How many pieces of data are there in the database in total? Article data

Here, we will use MySQL as our database system. Let's assume we have a table called "products" that contains information about products. We will use the following PHP code to display them in pages:

<?php
  $page_num = isset($_GET[&#39;page&#39;]) ? $_GET[&#39;page&#39;] : 1;
  $rows_per_page = 10;

  // connect to MySQL database
  $conn = mysqli_connect(&#39;localhost&#39;, &#39;username&#39;, &#39;password&#39;, &#39;database&#39;);
  if (!$conn) {
    die(&#39;Failed to connect to database: &#39; . mysqli_connect_error());
  }

  // get total number of rows
  $result = mysqli_query($conn, &#39;SELECT COUNT(*) as count FROM products&#39;);
  $row = mysqli_fetch_assoc($result);
  $total_rows = $row[&#39;count&#39;];

  // calculate the number of pages
  $total_pages = ceil($total_rows / $rows_per_page);

  // calculate the starting row index
  $start = ($page_num - 1) * $rows_per_page;

  // retrieve the data
  $sql = "SELECT * FROM products LIMIT $start, $rows_per_page";
  $result = mysqli_query($conn, $sql);

  // display the data
  while ($row = mysqli_fetch_assoc($result)) {
    echo $row[&#39;product_name&#39;] . &#39; - &#39; . $row[&#39;product_price&#39;] . &#39;<br>';
  }

  // display the pagination links
  for ($i = 1; $i <= $total_pages; $i++) {
    if ($i == $page_num) {
      echo &#39;<b>' . $i . '</b> ';
    } else {
      echo '<a href="?page=&#39; . $i . &#39;">' . $i . '</a> ';
    }
  }

  // close the connection
  mysqli_close($conn);
?>

In this code snippet, we first get the current page number and the number of rows to be displayed per page. Next, we will connect to the MySQL database and use the "COUNT" function to get the number of rows in the table. We then calculate the total number of pages and calculate the index of the starting data row to retrieve. Finally, we retrieve the data from the database and display them. To display paginated links, we use a "for" loop followed by an "else" statement containing a link to a specific page.

  1. Delete function implementation

In web applications, the delete function is very common. For example, we can delete rows in the database by clicking on the delete button. In order to implement the delete operation, we will need a few things to pay attention to.

First, we need to determine the ID of the row we want to delete. Here we can use HTML code to generate a table and place a delete button at the end of each row. When this button is clicked, we can pass the corresponding row ID to the PHP script.

Secondly, we need to write a PHP script to handle the delete request. In this article, we will use MySQL database as our data storage system. Let's assume we have a table called "products" that contains information about products. We will use the following PHP code to delete a product:

<?php
  // connect to MySQL database
  $conn = mysqli_connect(&#39;localhost&#39;, &#39;username&#39;, &#39;password&#39;, &#39;database&#39;);
  if (!$conn) {
    die(&#39;Failed to connect to database: &#39; . mysqli_connect_error());
  }

  // get the product id to delete
  $product_id = $_GET[&#39;product_id&#39;];

  // delete the product
  $sql = "DELETE FROM products WHERE id = $product_id";
  mysqli_query($conn, $sql);

  // close the connection
  mysqli_close($conn);

  // redirect to the product list page
  header(&#39;Location: products.php&#39;);
?>

Here, we first connect to the MySQL database and get the ID of the product to be deleted. We then use the "DELETE" statement to delete the corresponding row from the database. Finally, we close the database connection and redirect the page to the product list page.

  1. Summary

In this article, we introduced how to use PHP to implement paging and deletion functions. To implement paging, we need to know the page number currently to be displayed, how many pieces of data each page should contain, and how many pieces of data there are in the database in total. To implement the delete function, we need to get the ID of the row to be deleted and use the "DELETE" statement in the PHP script to delete the row from the database. By using these two features, we can greatly improve the user experience of our web applications.

The above is the detailed content of How to implement paging and deletion functions in php. 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