Jump to content

ARRAY AND SESSION


ecabrera

Recommended Posts

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>";
    } 
}
?>

Link to comment
Share on other sites

does it make a difference using single quotes? the examples i have seen all use them. when doing multidimensional arrays. :confused:

 

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

Link to comment
Share on other sites

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();
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.