Skip to content
PHP Logo

How can PHP scripts be structured to separate the functionality of displaying a list of items versus displaying detailed information about a specific item?

To separate the functionality of displaying a list of items versus displaying detailed information about a specific item, you can use conditional statements in your PHP script. You can create separate functions or sections of code for each type of display and use parameters or variables to determine which one to execute based on user input.

php
<?php
// Display list of items
function displayItemList() {
    // Code to fetch and display list of items
}

// Display detailed information about a specific item
function displayItemDetails($itemId) {
    // Code to fetch and display detailed information about the item with ID $itemId
}

// Check user input to determine which display function to call
if(isset($_GET['itemId'])) {
    $itemId = $_GET['itemId'];
    displayItemDetails($itemId);
} else {
    displayItemList();
}
?>