<?php
include('../includes/header.php');
error_reporting(E_ALL);
echo("<h1>Grocery List</h1>");
// get values from POST
$step = isset($_POST['step']) ? $_POST['step'] : 0;
$items = isset($_POST['items']) ? $_POST['items'] : array();
$categories = isset($_POST['categories']) ? $_POST['categories'] : array();
// baisc input filtering
$step = htmlentities($step);
// scrub arrays (each element)
if (is_array($items))
{
foreach ($items as $k => $v)
{
$items[$k] = htmlentities($v);
}
}
if (is_array($categories))
{
foreach ($categories as $k => $v)
{
$categories[$k] = htmlentities($v);
}
}
if ($step == 0)
{
?>
<form action="grocery_list.php" method="POST" class="pad">
<p>Enter grocery items and a category for each one (leave unused rows blanks).</p>
<table class="groceryTable">
<tr>
<th>Item</th>
<th>Category</th>
</tr>
<?php for ($i = 0; $i < 10; $i++) { ?>
<tr>
<td><input type="text" name="items[]" size="30" placeholder="ex: Ice Cream"></td>
<td><input type="text" name="categories[]" size="30" placeholder="ex: Dairy"></td>
</tr>
<?php } ?>
</table>
<br>
<input type="hidden" name="step" value="1">
<input type="submit" name="submit" value="Build List">
</form>
<?php
}
else
{
// build grocery array
$groceries = array();
$count = count($items);
for ($i = 0; $i < $count; $i++)
{
$item = trim($items[$i]);
$category = trim($categories[$i]);
// skip completely blank rows
if ($item == '' && $category == '')
{
continue;
}
// if category is blank, give it a default
if ($category == '')
{
$category = "Uncategorized";
}
$groceries[] = array(
'item' => $item,
'category' => $category
);
}
echo('<h2>Results</h2>');
if (count($groceries) == 0)
{
echo('<p><strong>No items were entered.</strong> Please try again.</p>');
}
else
{
echo('<p><strong>Total items entered:</strong> ' . count($groceries) . '</p>');
// list unique categories
$category_list = array_column($groceries, 'category');
$unique_categories = array_unique($category_list);
sort($unique_categories);
echo('<p><strong>Categories used: </strong> ' . implode(', ', $unique_categories) . '</p>');
// sort groceries alphabetically by item name
$sorted = $groceries;
usort($sorted, function($a, $b) {
return strcmp(strtolower($a['item']), strtolower($b['item']));
});
echo('<h3>Alphabetical Grocery List</h3>');
echo('<table class="groceryTable">');
echo('<tr><th>Item</th><th>Category</th></tr>');
foreach ($sorted as $g)
{
echo('<tr>');
echo('<td>' . $g['item'] . '</td>');
echo('<td>' . $g['category'] . '</td>');
echo('</tr>');
}
echo('</table>');
// filter: show only "dairy" items
$dairy_items = array_filter($groceries, function($g) {
return strtolower($g['category']) == 'dairy';
});
echo('<h3>Dairy Items (if any)</h3>');
if (count($dairy_items) == 0)
{
echo('<p>No dairy items found.</p>');
}
else
{
echo('<ul>');
foreach ($dairy_items as $d)
{
echo('<li>' . $d['item'] . '</li>');
}
echo('</ul>');
}
}
?>
<form action="grocery_list.php" method="POST">
<input type="hidden" name="step" value="0">
<input type="submit" name="submit" value="Try Again">
</form>
<?php
}
include('../includes/footer.php');
?>