PHP: Avoid undefined index? -


every time post value not equal list of values set in array return: undefined index error, made if statement not working.

here's if statement:

 if ($products[$_post['product']] == $_post['product']) { everything;} else { echo "this item not available"; } 

edit2:

seen current situation avoiding warning wont because i'm dealing several factors, example list of items in shopping cart, if invalid product not removed, added shopping list session.

this full script:

<?php  session_start();  //getting 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,      );      if( isset($_post['product']) ){        //saving 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'] $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;             }          /*if ($new_item['item'] != $products[$new_item['item']]) {         echo "this item not available";}*/      //listing         echo  "<b>shopping list</b></br>";         foreach($_session['list'] $key => $item) {                echo 'product .'. $key. ' '. $item['item'], ' ', $item['quantity'], ' units: ', $item['price']. '<br />';             }  }  else { echo "this item not available"; }  echo "</br> <a href='index.html'>return index</a> </br>";  //printing session var_dump($_session);  session_destroy();  ?> 

i'm bit confused code. looks array has same key , value, so:

$products['saucepan'] = 'saucepan' 

perhaps trying this, check whether product exists in products array:

if(isset($_post['product']) && array_key_exists($_post['product'], $products)) {   // stuff } else {   echo "this item not available"; } 

Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -