The program I'm working on is essentially a receipt maker for a restaurant. There is a drop down list which contains a bunch of foods taken from an array. The array includes the price for each item, and as of right now, the program will display any food item selected from the drop-down, list the individual prices, and add them up for a total. The problem I have is if someone selects the same item more than once, I have no way to register the quantity of said item. Basically, I need some code that checks the current selection against the past selections to see if their are any duplicates. I don't really know how to do that. Here is my code so far. Please take a look and pass on any advice. Thanks.
You can store your selected values in a session and using the in_array function of PHP check if there already is a value of that selection in the session. Once the value is false, you can add the selection without duplicating an other value.
Edit; Let's say you have an selectbox called item and you post on change. The value will be stored in $_POST['item'];
You can do this if the page is posted:
<?PHP
if(!in_array($_POST['item'], $_SESSION['items'])) {
$_SESSION['items'][] = $_POST['item']; // add posted item to array value items in session
}
?>
Don't forget the session_start(); on the very top of the page.
Use some sort of unique product ID for the array keys and before adding a new product into the array, possibly creating a duplicate, check if its ID already exists through the array key. If it exists, just run something like:
if (isset($products[$id]) {
$products[$id]['quantity'] = $products[$id]['quantity']++;
} else {
// Create new array item as you're doing now...
}