gongpex Posted December 7, 2012 Share Posted December 7, 2012 Hello all I had tried to make so that session display value according on product that choose by user. But it's still fail, this is my code : CODE 1 : if($product_id && !productExists($product_id)) { die("Error. Product Doesn't Exist");}switch($action) { //decide what to do case "add": $_SESSION['cart'][$product_id]++; //add one to the quantity of the product with id $product_id break; case "remove": //$_SESSION['cart'][$product_id]--; //remove one from the quantity of the product with id $product_id /*if($_SESSION['cart'][$product_id] == 0)*/ unset($_SESSION['cart'][$product_id]); //if the quantity is zero, remove it completely (using the 'unset' function) - otherwise is will show zero, then -1, -2 etc when the user keeps removing items. break; case "empty": unset($_SESSION['cart']); //unset the whole cart, i.e. empty the cart. break; case "change": $_SESSION['cart'][$product_id]=+$qty; if(preg_match("/[.]/",$qty)){$_SESSION['cart'][$product_id]=+1;} if($_SESSION['cart'][$product_id] == 0){unset($_SESSION['cart'][$product_id]);} break; } CODE 2 : foreach($_SESSION['cart'] as $product_id => $quantity) { //get the name, description and price from the database - this will depend on your database implementation. //use sprintf to make sure that $product_id is inserted into the query as a number - to prevent SQL injection $sql = sprintf("SELECT p_id, p_title, p_price, p_image FROM product WHERE p_id = %d;", $product_id); $result = mysql_query($sql); //Only display the row if there is a product (though there should always be as we have already checked) if(mysql_num_rows($result) > 0) { list($p_id, $p_title, $p_price, $p_image) = mysql_fetch_row($result); $line_cost = $p_price * $quantity; //work out the line cost $total = $total + $line_cost; //add to the total cost echo" <div class=\"b_content\"> <form method=\"post\" action=\"?p=basket&action=change&id=$product_id\"> <div class=\"b_img\"><img src=\"product/$p_image\" width=\"105px\" height=\"152px\" border=\"1\"/></div> <div class=\"b_detail\"> <div class=\"b_columnL\">Book Name : </div> <div class=\"b_columnR\"><b>$p_title</b></div> <div class=\"b_columnL\">Price Per Book : </div> <div class=\"b_columnR\">$p_price</div> <div class=\"b_columnL\">Total Item : </div> <div class=\"b_columnR\"><input type=\"text\" name=\"qty\" style=\"width:20px;text-align:center;border:1px solid #000066;\" maxlength=\"3\" value=\"$quantity\" /></div> <div class=\"b_columnL\">Total Price : </div> <div class=\"b_columnR\">USD $line_cost</div> <div class=\"b_columnL\"><a href=\"basket.php?action=remove&id=$product_id\">Delete</a></div> <div class=\"b_columnR\"><input type=\"submit\" value=\"RECALCULATE\" /></div> </div> </form> </div>"; } } Information :- Code 1 purposed to tell computer to add or delete or change product.- Code 2 purposed to display product according on data that displayed by code 1. My point : How to display value of $_SESSION['cart'][$product_id] without using foreach php ? Note : I had tried using code like this but it's not successful: <?echo $_SESION['cart'][$product_id][0];?> please someone help me Thanks Link to comment Share on other sites More sharing options...
birbal Posted December 7, 2012 Share Posted December 7, 2012 what does it show when you tried to print it? error? how is your array is strcuctured? you can use print_R() or var_dump() if you want to see the structure of array. if you need to show all array element regardless of its structure loop is way to go. if you want a specific element you need to know the structure of array 1 Link to comment Share on other sites More sharing options...
satishpoul Posted December 10, 2012 Share Posted December 10, 2012 yes.. first use var_dump($arrayname); it returns all the elements. then u will get idea abt ur solution. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now