Home > Article > Backend Development > What are the common table operations in PHP programming?
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.
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>
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); ?>
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>
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>
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!