How can PHP developers effectively link tables in a database to display hierarchical data like pages and subpages?

To effectively link tables in a database to display hierarchical data like pages and subpages, PHP developers can utilize SQL queries with JOIN operations to retrieve data from multiple tables based on their relationships. By using JOIN operations, developers can connect parent pages with their respective subpages and display them in a hierarchical structure on the web page. Additionally, developers can use recursive functions to traverse through the hierarchical data and display it in a tree-like format.

<?php
// Assuming we have two tables: pages (containing page_id and page_name) and subpages (containing subpage_id, page_id, and subpage_name)

// Establish database connection
$pdo = new PDO('mysql:host=localhost;dbname=your_database', 'username', 'password');

// Retrieve hierarchical data using JOIN operation
$query = "SELECT p.page_name, sp.subpage_name
          FROM pages p
          LEFT JOIN subpages sp ON p.page_id = sp.page_id";
$stmt = $pdo->query($query);

// Display hierarchical data
echo "<ul>";
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    echo "<li>{$row['page_name']}";
    if ($row['subpage_name']) {
        echo "<ul><li>{$row['subpage_name']}</li></ul>";
    }
    echo "</li>";
}
echo "</ul>";
?>