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.

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:

// 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:

$selected_id = $_GET['id'] ?? null;
if ($selected_id) {
$selected_item = fetchProductById($selected_id);
}

Show comprehensive information about the selected item:

if (isset($selected_item)) {
echo "<h2>{$selected_item['name']}</h2>";
echo "<p>Category: {$selected_item['category']}</p>";
// Display additional details
}

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:

$search = $_GET['search'] ?? '';
$category = $_GET['category'] ?? '';
$products = fetchProducts($search, $category);

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

  • 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