Skip to content

Master-Details in PHP

  • Master-Details is a UI pattern that displays a list of items (master) alongside detailed information about a selected item (details).
  • Common examples include email clients, product catalogs, and user management systems, etc.

There are several ways to implement the Master-Details pattern, in this example, we will use the separate page approach.

Both master list and details display on the same page:

  • Master list always visible.
  • Details panel updates based on selection.
  • Uses URL parameters for item selection.

Master and details on different pages:

  • Master page shows the list.
  • Clicking an item navigates to details page.
  • Requires back navigation to return to list.

Dynamic loading without page refresh:

  • Master list loads initially.
  • Details load via JavaScript/AJAX.
  • Smoother user experience.

Design tables with a clear relationship between master and detail records:

-- Master table (e.g., products)
CREATE TABLE products (
id INT PRIMARY KEY,
name VARCHAR(255),
category VARCHAR(100)
);
-- Details can be in the same table or related tables

Display all items in a summary format with selection links, allow users to click on a product to view the details:

// Fetch all products
$products = fetchAllProducts();
foreach ($products as $product) {
echo "<a href='?id={$product['id']}'>{$product['name']}</a>";
}

You can use URI parameters or forms to identify which item to show details for, in this example, we will use URI parameters:

// Get the selected item ID from the URI (stored in the $_GET superglobal).
$selected_id = $_GET['id'] ?? null;
// Fetch the selected item from the database (using the selected item ID).
if ($selected_id) {
$selected_item = fetchProductById($selected_id);
}

Show comprehensive information about the selected item, in this example, we will display the name and category of the selected item:

if (isset($selected_item)) {
echo "<h2>{$selected_item['name']}</h2>";
echo "<p>Category: {$selected_item['category']}</p>";
// Display additional details (e.g., description, price, etc.).
}

Allow users to easily move between items:

// Previous/Next navigation
$prev_id = getPreviousProductId($selected_id);
$next_id = getNextProductId($selected_id);
if ($prev_id) echo "<a href='?id=$prev_id'>Previous</a>";
if ($next_id) echo "<a href='?id=$next_id'>Next</a>";

Enable users to filter the master list, in this example, we will allow users to filter the master list by name and category:

// Get the search and category parameters from the URL.
$search = $_GET['search'] ?? '';
$category = $_GET['category'] ?? '';
// Fetch the products from the database (using the search and category parameters).
$products = fetchProducts($search, $category);

  • Load only necessary data for the master list.
  • Fetch detailed data only when item is selected.
  • Implement pagination for large datasets.
  • Use database indexing on frequently queried fields.
  • Highlight the currently selected item.
  • Provide clear navigation between items.
  • Show loading indicators for AJAX requests.
  • Maintain selection state during filtering.
  • Validate item IDs to prevent unauthorized access.
  • Handle cases where selected item doesn’t exist.
  • Implement proper error handling for database queries.
  • Use prepared statements to prevent SQL injection.