1

I'm using two arrays, one is a POST string (product), and another is a predefined set of values(products).

They work together as:

$products[$_POST['product']]  

This is okay if the POST value is exactly one of the following:

 $products = array(      
     'Pineaple' => 500, 'Banana' => 50, 'Mango' => 150,       
     'Milk' => 500, 'Coffe' => 1200, 'Butter' => 300,      
     'Bread' => 450, 'Juice' => 780, 'Peanuts' => 800,      
     'Yogurt' => 450, 'Beer' => 550, 'Wine' => 2500,    
 );    

Otherwise it will pop Undefined Index messages. Since this is a SESSION, I need to somehow keep a list of items, but remove the invalid ones from the array and echo a message.

Please check the screenshot: http://img36.imageshack.us/img36/9121/20110430004019.jpg

This is the script I'm using:

<?php
session_start();

//Getting the list
 $_SESSION['list'] = isset($_SESSION['list']) ? $_SESSION['list'] : array();    

 //stock    
 $products = array(      
     'Pineaple' => 500, 'Banana' => 50, 'Mango' => 150,       
     'Milk' => 500, 'Coffe' => 1200, 'Butter' => 300,      
     'Bread' => 450, 'Juice' => 780, 'Peanuts' => 800,      
     'Yogurt' => 450, 'Beer' => 550, 'Wine' => 2500,    
 );    

 //Saving the stuff    
 $new_item = array(      
     'item' => $_POST['product'],       
     'quantity' => $_POST['quantity'],     
     'code' => $_POST['code'],      
     'price' => $products[$_POST['product']] * $_POST['quantity'],    

 );

$new_product = true;    
foreach($_SESSION['list'] as $key => $item) {      
    if ($item['item'] == $new_item['item']) {        
    $_SESSION['list'][$key]['quantity'] += $new_item['quantity'];        
    $_SESSION['list'][$key]['price'] = 
            $products[$new_item['item']] * $new_item['quantity'];        
    $new_product = false;
    }    
}   

if ($new_product) {      
    $_SESSION['list'][] = $new_item;        
}    

//listing    
echo  "<b>SHOPPING LIST</b></br>";    
foreach($_SESSION['list'] as $key => $item) {       
    echo 'Product .'. $key. ' '. $item['item'], ' ', 
          $item['quantity'], ' units: ', $item['price']. '<br />';    
}

echo "</br> <a href='index.html'>Return to index</a> </br>";

//Printing session
var_dump($_SESSION);

//session_destroy();
?>
2
  • 1
    You did not specify what makes an item invalid. Is there a database which holds the valid item ids or something? Commented Apr 30, 2011 at 7:48
  • I edited the question, I hope to be concise. Commented Apr 30, 2011 at 7:52

2 Answers 2

3

You should use isset or array_key_exists to check whether the key exists before reading it:

if (isset($products[$_POST['product']])) {
    // product in stock
} else {
    // product not in stock
}

And if you use the same key for your product list in $_SESSION['list'], you don’t need to iterate the whole list to find the same product in the list:

if (isset($_SESSION['list'][$_POST['product']])) {
    // update product in list
    $_SESSION['list'][$_POST['product']]['quantity'] += $_POST['quantity'];
    $_SESSION['list'][$_POST['product']]['price'] = $products[$_POST['product']] * $_POST['quantity'];
} else {
    // add product to list
    $_SESSION['list'][$_POST['product']] = array(
        'item'     => $_POST['product'],
        'quantity' => $_POST['quantity'],
        'code'     => $_POST['code'],
        'price'    => $products[$_POST['product']] * $_POST['quantity'],
    );
}

Note that this will always only add items to the list. You should add some functionality to also update and remove items. And don’t forget to validate the data before using it (e.g. so that people don’t get refund if they enter a negative quantity).

Sign up to request clarification or add additional context in comments.

2 Comments

As a general rule this isn't quite right, try this: $a = array('foo' => null); var_dump(isset($a['foo'])); var_dump(array_key_exists('foo', $a)); I always advise people to use array_key_exists().
@Robin: You’re right. But I doubt that he well be using null in an array of prices.
0

I would imagine you want to use the array_intersect_assoc function. it returns the items that are in both of your arrays.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.