Home > Backend Development > PHP Tutorial > What are the common table operations in PHP programming?

What are the common table operations in PHP programming?

PHPz
Release: 2023-06-12 11:22:02
Original
1392 people have browsed it

In Web development, tables are the most basic and commonly used elements, and PHP is a popular server-side programming language. There are many common techniques and methods in table operations. This article will introduce common table operations in PHP programming.

  1. Display data table

In PHP, you can use the table tag in HTML to display the data table. It is worth noting that the table must be generated in a PHP script. The following is an example of a basic HTML table tag:

<table>
  <tr>
    <th>ID</th>
    <th>姓名</th>
    <th>年龄</th>
  </tr>
  <tr>
    <td>1</td>
    <td>张三</td>
    <td>20</td>
  </tr>
  <tr>
    <td>2</td>
    <td>李四</td>
    <td>25</td>
  </tr>
</table>
Copy after login
  1. Dynamic generation of data tables

If you need to get data from the database and display it on the web page, you can use PHP Loops and conditional statements to dynamically generate tables. The following is sample code:

<?php
  $conn = mysqli_connect('localhost', 'root', 'password', 'database');
  $sql = "SELECT * FROM users";
  $result = mysqli_query($conn, $sql);
?>

<table>
  <tr>
    <th>ID</th>
    <th>姓名</th>
    <th>年龄</th>
  </tr>
  <?php while($row = mysqli_fetch_array($result)): ?>
    <tr>
      <td><?php echo $row['id']; ?></td>
      <td><?php echo $row['name']; ?></td>
      <td><?php echo $row['age']; ?></td>
    </tr>
  <?php endwhile; ?>
</table>

<?php mysqli_close($conn); ?>
Copy after login
  1. Styles and styles

In addition to using HTML tags to create tables, styles and styles can also be added through CSS. The most common way is to add a class or ID attribute and style it in a CSS file. The following is sample code:

<table class="table-style">
  <tr>
    <th>ID</th>
    <th>姓名</th>
    <th>年龄</th>
  </tr>
  <tr>
    <td>1</td>
    <td>张三</td>
    <td>20</td>
  </tr>
  <tr>
    <td>2</td>
    <td>李四</td>
    <td>25</td>
  </tr>
</table>

<style>
  .table-style {
    border-collapse: collapse;
  }
  th, td {
    border: 1px solid #ccc;
    padding: 5px;
  }
  th {
    background-color: #eee;
  }
  tr:nth-child(odd) {
    background-color: #f2f2f2;
  }
</style>
Copy after login
  1. Sort and Filter

In order to facilitate users to find and sort data, you can add some controls for sorting and filtering data. This can be achieved by adding a form and button above the table. The following is a sample code:

<form method="post">
  <input type="text" name="search">
  <button type="submit" name="submit">搜索</button>
  <button type="submit" name="reset">重置</button>
</form>

<table>
  <tr>
    <th>ID</th>
    <th>姓名</th>
    <th>年龄</th>
  </tr>
  <?php if(isset($_POST['submit'])): ?>
    <?php
      $conn = mysqli_connect('localhost', 'root', 'password', 'database');
      $search = mysqli_real_escape_string($conn, $_POST['search']);
      $sql = "SELECT * FROM users WHERE name LIKE '%$search%'";
      $result = mysqli_query($conn, $sql);
    ?>
    <?php while($row = mysqli_fetch_array($result)): ?>
      <tr>
        <td><?php echo $row['id']; ?></td>
        <td><?php echo $row['name']; ?></td>
        <td><?php echo $row['age']; ?></td>
      </tr>
    <?php endwhile; ?>
    <?php mysqli_close($conn); ?>
  <?php else: ?>
    <?php 
      $conn = mysqli_connect('localhost', 'root', 'password', 'database');
      $sql = "SELECT * FROM users";
      $result = mysqli_query($conn, $sql);
    ?>
    <?php while($row = mysqli_fetch_array($result)): ?>
      <tr>
        <td><?php echo $row['id']; ?></td>
        <td><?php echo $row['name']; ?></td>
        <td><?php echo $row['age']; ?></td>
      </tr>
    <?php endwhile; ?>
    <?php mysqli_close($conn); ?>
  <?php endif; ?>
</table>
Copy after login

To sum up, common table operations in PHP programming include: displaying data tables, dynamically generating data tables, styles and styles, and sorting and filtering. By mastering these skills, you can improve the efficiency of PHP programming and the readability of tables.

The above is the detailed content of What are the common table operations in PHP programming?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template