ecabrera Posted January 21, 2012 Share Posted January 21, 2012 why is it that i have to click add to cart 2 time inorder for the eventname and eventinfo to show up if i click one time it add to the cart but it shows nothing <?php //if user attempts to add something to the cart if (isset($_POST['tid'])) { $tid = $_POST['tid']; $howmany = $_POST['howMany']; $wasFound = false; $i = 0; // If the cart session variable is not set or cart array is empty if (!isset($_SESSION["cart"]) || count($_SESSION["cart"]) < 1) { // RUN IF THE CART IS EMPTY OR NOT SET $_SESSION["cart"] = array(1 => array("item_id" => $pid, "quantity" => $howmany)); } else { // RUN IF THE CART HAS AT LEAST ONE ITEM IN IT foreach ($_SESSION["cart"] as $each_item) { $i++; while (list($key, $value) = each($each_item)) { if ($key == "item_id" && $value == $tid) { //there will recive a message $msg = "Item is already in the cart"; $wasFound = true; } // close if condition } // close while loop } // close foreach loop if ($wasFound == false) { array_push($_SESSION["cart"], array("item_id" => $tid, "quantity" => $howmany)); } } header("location: cart.php"); exit(); } ?> <?php //render the cart for the user to view on the page $cartOutput = ""; $cartTotal = ""; $product_id_array = ''; if (!isset($_SESSION["cart"]) || count($_SESSION["cart"]) < 1) { $cartOutput = "<h2 align='center'>Your shopping cart is empty</h2>"; } else { // Start the For Each loop $i = 0; foreach ($_SESSION["cart"] as $each_item) { $item_id = $each_item['item_id']; $sql = mysql_query("SELECT * FROM events WHERE id='$item_id' LIMIT 1"); while ($row = mysql_fetch_array($sql)) { $eventname = $row["eventname"]; $eventinfo = $row["eventinfo"]; $studentsprice = $row["studentsprice"]; } //adding the price of the tickets //getting the total $totalprice = $studentsprice * $howmany; setlocale(LC_MONETARY, "en_US"); //this will amke it so it looks like real money $totalprice = money_format("%10.2n", $totalprice ); $cartOutput = ""; //this is the table the will replace the table row below //this is in order $cartOutput.= "<tr>"; $cartOutput .= "<td>".$eventname."</td>"; $cartOutput .= "<td>".$eventinfo."</td>"; $cartOutput .= "<td>".$studentsprice."</td>"; $cartOutput .= "<td>".$howmany."</td>"; $cartOutput .= "<td>".$totalprice."</td>"; $cartOutput .= "<td>".X."</td>"; $cartOutput .= "</tr>"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/255465-array-and-session/ Share on other sites More sharing options...
darkfreaks Posted January 21, 2012 Share Posted January 21, 2012 ran it through php lint here is some stuff it came out with Warning: calling `array_push()' declared in modules/standard:950, argument no. 1: found type `mixed', required type `array' it is supposed to be array_push( 'array','mixed') Quote Link to comment https://forums.phpfreaks.com/topic/255465-array-and-session/#findComment-1309764 Share on other sites More sharing options...
ecabrera Posted January 21, 2012 Author Share Posted January 21, 2012 any advice Quote Link to comment https://forums.phpfreaks.com/topic/255465-array-and-session/#findComment-1309765 Share on other sites More sharing options...
darkfreaks Posted January 21, 2012 Share Posted January 21, 2012 yeah change your array_push structure around it is backwards Quote Link to comment https://forums.phpfreaks.com/topic/255465-array-and-session/#findComment-1309766 Share on other sites More sharing options...
ecabrera Posted January 21, 2012 Author Share Posted January 21, 2012 What do you mean like this array_push( array("item_id" => $tid, "quantity" => $howmany,$_SESSION["cart"])); Quote Link to comment https://forums.phpfreaks.com/topic/255465-array-and-session/#findComment-1309767 Share on other sites More sharing options...
darkfreaks Posted January 21, 2012 Share Posted January 21, 2012 yup Quote Link to comment https://forums.phpfreaks.com/topic/255465-array-and-session/#findComment-1309768 Share on other sites More sharing options...
ecabrera Posted January 21, 2012 Author Share Posted January 21, 2012 well i canged it like this if ($wasFound == false) { array_push( array("item_id" => $tid, "quantity" => $howmany, $_SESSION["cart"])); } and i makes the page say Server error Quote Link to comment https://forums.phpfreaks.com/topic/255465-array-and-session/#findComment-1309769 Share on other sites More sharing options...
darkfreaks Posted January 21, 2012 Share Posted January 21, 2012 syntax error you put the extra ) at the end instead of closing the array() Quote Link to comment https://forums.phpfreaks.com/topic/255465-array-and-session/#findComment-1309770 Share on other sites More sharing options...
ecabrera Posted January 21, 2012 Author Share Posted January 21, 2012 no i think i didnt put no extra ) Quote Link to comment https://forums.phpfreaks.com/topic/255465-array-and-session/#findComment-1309771 Share on other sites More sharing options...
ecabrera Posted January 21, 2012 Author Share Posted January 21, 2012 sry i think i did let me tested Quote Link to comment https://forums.phpfreaks.com/topic/255465-array-and-session/#findComment-1309772 Share on other sites More sharing options...
darkfreaks Posted January 21, 2012 Share Posted January 21, 2012 yes you did but i fixed it. array_push(array("item_id" => $tid, "quantity" => $howmany),$_SESSION['cart']); Quote Link to comment https://forums.phpfreaks.com/topic/255465-array-and-session/#findComment-1309773 Share on other sites More sharing options...
ecabrera Posted January 21, 2012 Author Share Posted January 21, 2012 lol i did the same thing and im still getting the server error Quote Link to comment https://forums.phpfreaks.com/topic/255465-array-and-session/#findComment-1309774 Share on other sites More sharing options...
darkfreaks Posted January 21, 2012 Share Posted January 21, 2012 think your problem is your passing it as a reference instead of a variable. try something like $a= array($tid => "item_id", $howmany => "quantity"); array_push($a,$_SESSION['cart']); Quote Link to comment https://forums.phpfreaks.com/topic/255465-array-and-session/#findComment-1309775 Share on other sites More sharing options...
ecabrera Posted January 21, 2012 Author Share Posted January 21, 2012 nope let me try something and i'll get back Quote Link to comment https://forums.phpfreaks.com/topic/255465-array-and-session/#findComment-1309776 Share on other sites More sharing options...
darkfreaks Posted January 21, 2012 Share Posted January 21, 2012 does it make a difference using single quotes? the examples i have seen all use them. when doing multidimensional arrays. also noticed // RUN IF THE CART IS EMPTY OR NOT SET $_SESSION["cart"] = array(1 => array("item_id" => $pid, "quantity" => $howmany)); should be: // RUN IF THE CART IS EMPTY OR NOT SET $_SESSION["cart"] = array(1 => array($pid => "item_id", $howmany => "quantity")); Quote Link to comment https://forums.phpfreaks.com/topic/255465-array-and-session/#findComment-1309777 Share on other sites More sharing options...
ecabrera Posted January 21, 2012 Author Share Posted January 21, 2012 i fixed it i replace with a fixed quantity to a editable quantity if (isset($_POST['tid'])) { $tid = $_POST['tid']; $wasFound = false; $i = 0; // If the cart session variable is not set or cart array is empty if (!isset($_SESSION["cart"]) || count($_SESSION["cart"]) < 1) { // RUN IF THE CART IS EMPTY OR NOT SET $_SESSION["cart"] = array(0 => array("item_id" => $tid, "quantity" => 1)); } else { // RUN IF THE CART HAS AT LEAST ONE ITEM IN IT foreach ($_SESSION["cart"] as $each_item) { $i++; while (list($key, $value) = each($each_item)) { if ($key == "item_id" && $value == $tid) { // That item is in cart already so let's adjust its quantity using array_splice() array_splice($_SESSION["cart"], $i-1, 1, array(array("item_id" => $tid, "quantity" => $each_item['quantity'] + 1))); $wasFound = true; } // close if condition } // close while loop } // close foreach loop if ($wasFound == false) { array_push($_SESSION["cart"], array("item_id" => $tid, "quantity" => 1)); } } header("location: cart.php"); exit(); } Quote Link to comment https://forums.phpfreaks.com/topic/255465-array-and-session/#findComment-1309778 Share on other sites More sharing options...
darkfreaks Posted January 21, 2012 Share Posted January 21, 2012 cool did it fix everything? if so please mark as solved. Quote Link to comment https://forums.phpfreaks.com/topic/255465-array-and-session/#findComment-1309779 Share on other sites More sharing options...
ecabrera Posted January 21, 2012 Author Share Posted January 21, 2012 Yea it did thanks for everything Quote Link to comment https://forums.phpfreaks.com/topic/255465-array-and-session/#findComment-1309781 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.