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
https://forums.phpfreaks.com/topic/255465-array-and-session/
Share on other sites

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')

 

Link to comment
https://forums.phpfreaks.com/topic/255465-array-and-session/#findComment-1309764
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
https://forums.phpfreaks.com/topic/255465-array-and-session/#findComment-1309777
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
https://forums.phpfreaks.com/topic/255465-array-and-session/#findComment-1309778
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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