Jump to content

[SOLVED] stopping repeating values being added to array


gavin1512

Recommended Posts

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");

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.