php script to sort the elements of an array allowing user to select one 2 many via browser using nginx

To allow a user to select and sort array elements via a browser, you need a combination of HTML for the user interface and PHP for server-side processing (which works seamlessly with Nginx). The Nginx server will typically pass PHP requests to a PHP-FPM process, which handles the execution of the PHP script. 

Below are the necessary HTML and PHP scripts. 

1. Nginx Configuration

Ensure Nginx is set up to handEnsure Nginx is set up to handle PHP files. The configuration usually involves passing .php requests to php-fpm via a location block

server {
    listen 80;
    server_name your_domain.com;
    root /var/www/html; # Your project root directory

    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
        include fastcgi_params;
        # Adjust the socket path based on your PHP version and OS
        fastcgi_pass unix:/run/php/php8.2-fpm.sock; 
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
    }
}

2. The PHP Script (index.php)

This single PHP file contains both the HTML form and the logic to handle the user’s sorting choice. 

<?php
// Define the initial array
$items = ['Apple', 'Orange', 'Banana', 'Grape', 'Kiwi', 'Peach'];
$sort_order = 'asc'; // Default sort order

// Check if the form was submitted
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['sort_order'])) {
    $sort_order = $_POST['sort_order'];
    $selected_items = isset($_POST['items']) ? $_POST['items'] : [];

    // Sort the original array based on the selected order
    if ($sort_order == 'asc') {
        sort($items); // Sort in ascending order
    } else {
        rsort($items); // Sort in descending order
    }
    
    // In a real application, you'd process the selected items here (e.g., save to DB)
    // For this example, we just acknowledge the selection
    if (!empty($selected_items)) {
        echo "<div style='color: green; padding: 10px; border: 1px solid green; margin-bottom: 15px;'>";
        echo "You selected the following items (after sorting):<br><ul>";
        foreach ($selected_items as $item) {
            echo "<li>" . htmlspecialchars($item) . "</li>";
        }
        echo "</ul></div>";
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Array Sorting and Selection</title>
</head>
<body>
    <h2>Sort and Select Items</h2>

    <form method="POST" action="index.php">
        <label for="sort_order">Choose a sort order:</label>
        <select name="sort_order" id="sort_order" onchange="this.form.submit()">
            <option value="asc" <?php if ($sort_order == 'asc') echo 'selected'; ?>>Ascending</option>
            <option value="desc" <?php if ($sort_order == 'desc') echo 'selected'; ?>>Descending</option>
        </select>
        <noscript><input type="submit" value="Apply Sort"></noscript>
        
        <h3>Available Items (Currently Sorted):</h3>
        <!-- The 'name="items[]"' syntax sends selections as a PHP array -->
        <select name="items[]" multiple size="5" style="width: 200px;">
            <?php foreach ($items as $item): ?>
                <option value="<?php echo htmlspecialchars($item); ?>">
                    <?php echo htmlspecialchars($item); ?>
                </option>
            <?php endforeach; ?>
        </select>
        <br><br>
        <input type="submit" value="Submit Selections">
    </form>
</body>
</html>

How it works:

  1. Initial Load: The page loads, the initial array $items is defined, and the list of items is displayed in the <select multiple> box.
  2. User Sorts: When the user changes the “Sort Order” dropdown, the form submits automatically (via onchange="this.form.submit()").
  3. PHP Sorts: The PHP script receives the $_POST['sort_order'] value, sorts the original $items array using sort() or rsort(), and re-renders the HTML with the array in the new order.
  4. User Selects: The user can then select one or many items using Ctrl+Click (Windows/Linux) or Cmd+Click (Mac) and click “Submit Selections”.
  5. PHP Processes Selections: The PHP script then receives the selected items in the $_POST['items'] array and displays them. 

Leave a Reply