无法使用php会话变量将多个值发送到另一个页面 [英] Unable to send multiple values to another page using php session variable

查看:61
本文介绍了无法使用php会话变量将多个值发送到另一个页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图制作一个迷你购物车,用户可以在那里购买商品。

一切正常,但是当我退房时,只有购​​物车中的第二个产品被加载到结账选项中不是第一个产品,这意味着如果购物车只有1个产品,它运作良好,但是当添加第二个产品时,它需要第二个产品,而不是第一个产品。请纠正我错误的地方。



这是我到目前为止所做的代码:

Am trying to make a mini shopping cart where the user can buy goods.
Everything's working fine but when I check out, only the second product in the cart gets loaded in the check out option and not the first product, this means if the cart has only 1 product, it works well and good, but when added second product, it takes the second product and not the first one. Please correct me where am making mistake.

Here's the code I have done so far:

<?php
// Session Start
session_start();
include 'storescripts/dbconnect.php';

?>
<?php
// Add To The Cart
if (isset($_POST['pid'])) {
	$id = $_POST['pid'];
	$wasFound = false;
	$i = 0;
	if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) {
		$_SESSION["cart_array"] = array(
									0 => array(
										"item_id" => $id, "quantity" => 1
										)
									);
	} else {
		foreach ($_SESSION["cart_array"] as $each_item) {
			$i++;
			while (list($key, $value) = each($each_item)) {
				if ($key == "item_id" && $value == $id) {
					array_splice($_SESSION["cart_array"], $i - 1, 1, array(array("item_id" => $id, "quantity" => $each_item['quantity'] + 1)));
					$wasFound = true;
				}
			}
		}
		if ($wasFound == false) {
			array_push($_SESSION["cart_array"], array("item_id" => $id, "quantity" => 1));
		}
	}
	header("Location: cart.php");
	exit();
}
?>
<?php
// Empty The Cart
if (isset($_GET['cmd']) && $_GET['cmd'] == "emptycart") {
	unset($_SESSION["cart_array"]);
}
?>
<?php
// Adjust The Item Quantity
if (isset($_POST['itemToAdjust']) && $_POST['itemToAdjust'] != "") {
	$itemToAdjust = $_POST['itemToAdjust'];
	$itemQuantity = $_POST['itemQuantity'];
	$itemQuantity = preg_replace('#[^0-9]#i', '', $itemQuantity);
	if ($itemQuantity >= 100) {
		$itemQuantity = 99;
	}

	if ($itemQuantity < 1) {
		$itemQuantity = 1;
	}
	$i = 0;
	foreach ($_SESSION["cart_array"] as $each_item) {
		$i++;
		while (list($key, $value) = each($each_item)) {
			if ($key == "item_id" && $value == $itemToAdjust) {
				array_splice($_SESSION["cart_array"], $i - 1, 1, array(array("item_id" => $itemToAdjust, "quantity" => $itemQuantity)));
			}
		}
	}
}

?>
<?php
// Remove Item If User Choose To
if (isset($_POST['idToRemove']) && $_POST['idToRemove'] != "") {
	$keyToRemove = $_POST['idToRemove'];
	if (count($_SESSION["cart_array"]) <= 1) {
		unset($_SESSION["cart_array"]);
	} else {
		unset($_SESSION["cart_array"]["$keyToRemove"]);
		sort($_SESSION["cart_array"]);
	}
}
?>
<?php
// Render The Cart For The User To View
if (!isset($_SESSION['product_name'])) {
    $_SESSION['product_name'] = array();
}
if (!isset($_SESSION['price'])) {
    $_SESSION['price'] = array();
}
include 'moneyFormat.php';
$cartOutput = "";
$cartTotal = "";
$cartTotalPrice = "";
$cartTotalPriceString = "";

if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) {
	$cartOutput = "<h2 align='center'>Your Shopping Cart Is Empty.</h2>";
} else {
	$i = 0;
	foreach ($_SESSION["cart_array"] as $each_item) {
		$item_id = $each_item['item_id'];
		$sql = "SELECT *
					FROM products
						WHERE id='".$item_id."'
					LIMIT 1";
		$res = mysqli_query($con, $sql);

		while ($row = mysqli_fetch_array($res)) {
			$productName = $row['product_name'];
			//$_SESSION["product_name"] = $productName;
			$price = $row['price'];
			$details = $row['details'];
		}

		$priceTotal = $price * $each_item['quantity'];
		$cartTotal += $priceTotal;

		//echo $_SESSION["product_name"] . "<br />";
		for ($i=0; $i < count($_SESSION["product_name"]); $i++) { 
			echo $_SESSION["product_name"]."<br />";
		}
		/*if (count($_SESSION["product_name"]) > 0) {
			echo $_SESSION["product_name"] . "<br />";
		}*/

		//setlocale(LC_MONETARY, "en_IN");
		$priceTotal = money_format("%10.2n", $priceTotal);
		$price = money_format("%10.2n", $price);

		$cartOutput .= '<tr>';
		$cartOutput .= '<td><a href="product.php?id='.$item_id.'">'.$productName.'</a><br />
						<img src="inventory_images/'.$item_id.'.jpg" alt='.$productName.' width="82" height="70" border="1">
						</td>';
		$cartOutput .= '<td>'.$details.'</td>';
		$cartOutput .= '<td style="text-align: center;"> '.$price.'</td>';
		$cartOutput .= '<td style="text-align: center;">
							<form action="cart.php" method="POST">
							<input type="text" name="itemQuantity" size="1" maxlenght="2" value="'.$each_item['quantity'].'"/>
							<input type="submit" name="btnAdjustQuantity'.$item_id.'" value="Change" />
							<input type="hidden" name="itemToAdjust" value="'.$item_id.'" />
							</form>
						</td>';
		//$cartOutput .= '<td style="text-align: center;">'.$each_item['quantity'].'</td>';
		$cartOutput .= '<td style="text-align: center;"> '.$priceTotal.'</td>';
		$cartOutput .= '<td style="text-align: center;">
							<form action="cart.php" method="POST">
							<input type="submit" name="btnDeleteQuantity'.$item_id.'" value="Remove" />
							<input type="hidden" name="idToRemove" value="'.$i.'" />
							</form>
						</td>';
		$cartOutput .= '</tr>';
		$i++;
	}
	$cartTotalPrice = money_format("%10.2n", $cartTotal);
	$cartTotalPriceString = "<div align='right' style='font-size: 19px; margin-removed 15px;'>Cart Total: ". $cartTotalPrice . "</div>";

}
?>
<?php
$totalItems = 0;
$checkOut = "";
$quantity = 0;

if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) {
	$cartOutput = "<h2 align='center'>Your Shopping Cart Is Empty.</h2>";
} else {
	foreach ($_SESSION["cart_array"] as $each_item) {
		while (list($key, $value) = each($each_item)) {
			if ($key == 'quantity' && $each_item['quantity'] > 0) {
				$totalItems += $value;
				$quantity = $totalItems;
			}
		}
	}
}

if ($totalItems > 0) {
	$checkOut = '<a href="storescripts/render_cart.php?checkout=YES&Product='.$productName.'&Quantity='.$quantity.'&Price='.$cartTotalPrice.'">';
	$checkOut .= '<input type="submit" value="Check Out" name="checkOut" style="float: right;"/>';
	$checkOut .= '</a>';
}
?>

推荐答案

_POST [' pid'])){
_POST['pid'])) {


id =
id =


_POST [' PID'];
_POST['pid'];


这篇关于无法使用php会话变量将多个值发送到另一个页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆