<?php
include('includes/header.php');
?>
<!DOCTYPE html>
<html>
<head>
<title>Favorite Fruits - Checkbox Version</title>
<style>
body { font-family: Arial; max-width: 800px; margin: auto; }
.results { background: #f9f9f9; padding: 15px; margin-top: 20px; }
</style>
</head>
<body>
<h2>Select Your Favorite Fruits</h2>
<form method="post">
<input type="checkbox" name="fruits[]" value="Apple"> Apple<br>
<input type="checkbox" name="fruits[]" value="Banana"> Banana<br>
<input type="checkbox" name="fruits[]" value="Orange"> Orange<br>
<input type="checkbox" name="fruits[]" value="Strawberry"> Strawberry<br>
<input type="checkbox" name="fruits[]" value="Blueberry"> Blueberry<br>
<input type="checkbox" name="fruits[]" value="Mango"> Mango<br>
<input type="checkbox" name="fruits[]" value="Pineapple"> Pineapple<br><br>
<input type="submit" value="Analyze Selection">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (!empty($_POST["fruits"])) {
$fruits = $_POST["fruits"];
echo "<div class='results'>";
echo "<h3>1. Selected Fruits:</h3>";
print_r($fruits);
echo "<h3>2. Total Fruits Selected:</h3>";
echo count($fruits);
// Sort alphabetically
$sortedFruits = $fruits;
sort($sortedFruits);
echo "<h3>3. Sorted Alphabetically:</h3>";
print_r($sortedFruits);
// Reverse order
$reversedFruits = array_reverse($fruits);
echo "<h3>4. Reversed Order:</h3>";
print_r($reversedFruits);
// Remove duplicates (just for demonstration)
$uniqueFruits = array_unique($fruits);
echo "<h3>5. Duplicates Removed:</h3>";
print_r($uniqueFruits);
// Search example
$search = "Apple";
echo "<h3>6. Searching for $search:</h3>";
if (in_array($search, $fruits)) {
echo "$search is in your selection!";
} else {
echo "$search is NOT in your selection.";
}
// Associative array: Fruit => Name Length
echo "<h3>7. Associative Array (Fruit => Length of Name):</h3>";
$fruitLengths = [];
foreach ($fruits as $fruit) {
$fruitLengths[$fruit] = strlen($fruit);
}
print_r($fruitLengths);
echo "</div>";
} else {
echo "<p>Please select at least one fruit.</p>";
}
}
include('includes/footer.php');
?>
</body>
</html>