gavin1512 Posted January 9, 2008 Share Posted January 9, 2008 Good Afternoon, I am having some issues when adding items to an array, it is a style of shopping basket and the if statement I am using is still allowing multiples of one product to be added to the basket. Below is my code: <?php session_start(); if (isset($_SESSION['username']) == false){ header("Location:home.php"); exit(); } $product = array(); $product['productnumber']= $_GET['productnumber']; $product['type']= $_GET['type']; $product['description']= $_GET['description']; $product['price']= $_GET['price']; $product['quantity'] = $_GET['quantity']; $_SESSION['order'] [] = $product; $found = false; if(isset($_SESSION['order'])){ foreach($_SESSION['order'] as $key => $anotherproduct) { if ($_SESSION['order'] [$key] ['productnumber'] == $_GET['productnumber']) { $found = true; } } } //add the product to the cart if not there already if ($found ==false) { $_SESSION['order'] [] = $product; } header("Location:productsearch.php"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/85168-solved-stopping-repeating-values-being-added-to-array/ Share on other sites More sharing options...
Ken2k7 Posted January 9, 2008 Share Posted January 9, 2008 PHP has a function called in_array() to check if something is in an array. http://us.php.net/manual/en/function.in-array.php Quote Link to comment https://forums.phpfreaks.com/topic/85168-solved-stopping-repeating-values-being-added-to-array/#findComment-434512 Share on other sites More sharing options...
gavin1512 Posted January 9, 2008 Author Share Posted January 9, 2008 thank you for your assistance I will work on implementing that function now. Quote Link to comment https://forums.phpfreaks.com/topic/85168-solved-stopping-repeating-values-being-added-to-array/#findComment-434527 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.