Home  >  Article  >  Backend Development  >  Tag Usage in PHP: Understanding Columns and Rows

Tag Usage in PHP: Understanding Columns and Rows

王林
王林Original
2024-04-09 10:06:02538browse

PHP tags are used to organize tabular data, including: column labels (

) define header columns, used for column titles; row labels ( ) define a row, including cells; colspan and rowspan attributes Can be used to merge cells; in practice, labels can be used to create employee data tables, including ID, name and age fields.

Tag Usage in PHP: Understanding Columns and Rows

Usage of tags in PHP: A closer look at columns and rows

In PHP, tags are used to combine and organize HTML Structural elements for data in tables. They are typically used to define columns and rows in tables.

Column labels ()

labels define header columns. It usually contains the title or description of the column.
echo "<th>姓名</th>";
echo "<th>年龄</th>";

Row label ()

label defines a row. It contains the cells in the table.
echo "<tr>";
echo "<td>约翰</td>";
echo "<td>30</td>";
echo "</tr>";

Merge cells

You can use the colspan and rowspan attributes to merge cells.

echo "<table border='1'>";
echo "<tr>";
echo "<th colspan='2'>个人信息</th>";
echo "</tr>";
echo "<tr>";
echo "<td rowspan='2'>姓名</td>";
echo "<td>约翰</td>";
echo "</tr>";
echo "<tr>";
echo "<td>30</td>";
echo "</tr>";
echo "</table>";

Practical case: display employee data

The following PHP script demonstrates how to use tags to create an employee data table:

<?php
$employees = array(
    array('id' => 1, 'name' => '约翰', 'age' => 30),
    array('id' => 2, 'name' => '玛丽', 'age' => 25)
);
?>
<!DOCTYPE html>
<html>
<head>
    <title>员工数据</title>
</head>
<body>
    <table>
        <tr>
            <th>ID</th>
            <th>姓名</th>
            <th>年龄</th>
        </tr>
        <?php foreach ($employees as $employee) : ?>
            <tr>
                <td><?php echo $employee['id']; ?></td>
                <td><?php echo $employee['name']; ?></td>
                <td><?php echo $employee['age']; ?></td>
            </tr>
        <?php endforeach; ?>
    </table>
</body>
</html>

Run this script An HTML table will be generated containing data for employee ID, name, and age.

The above is the detailed content of Tag Usage in PHP: Understanding Columns and Rows. 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