Php 全局访问购物车会话值
这是我的购物车代码,我将所有值保存在全局购物车会话数组中。 这是我的密码:Php 全局访问购物车会话值,php,Php,这是我的购物车代码,我将所有值保存在全局购物车会话数组中。 这是我的密码: $cartOutput = ""; $cartTotal = ""; $pp_checkout_btn = ''; $product_id_array = ''; if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) { $cartOutput = "<h2 align='center'>Your s
$cartOutput = "";
$cartTotal = "";
$pp_checkout_btn = '';
$product_id_array = '';
if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) {
$cartOutput = "<h2 align='center'>Your shopping cart is empty</h2>";
} else {
// Start PayPal Checkout Button
//$pp_checkout_btn .= '<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
//<input type="hidden" name="cmd" value="_cart">
//<input type="hidden" name="upload" value="1">
//<input type="hidden" name="business" value="you@youremail.com">';
// Start the For Each loop
$i = 0;
foreach ($_SESSION["cart_array"] as $each_item) {
$item_id = $each_item['item_id'];
$sql = mysql_query("SELECT * FROM products WHERE id='$item_id' LIMIT 1");
while ($row = mysql_fetch_array($sql)) {
$product_name = $row["product_name"];
$price = $row["price"];
$details = $row["details"];
}
$pricetotal = $price * $each_item['quantity'];
$cartTotal = $pricetotal + $cartTotal;
//setlocale(LC_MONETARY, "en_US");
//$pricetotal = money_format("%10.2n", $pricetotal);
// Dynamic Checkout Btn Assembly
$x = $i + 1;
$pp_checkout_btn .= '<input type="hidden" name="item_name_' . $x . '" value="' . $product_name . '">
<input type="hidden" name="amount_' . $x . '" value="' . $price . '">
<input type="hidden" name="quantity_' . $x . '" value="' . $each_item['quantity'] . '"> ';
// Create the product array variable
$product_id_array .= "$item_id-"."$product_name".$each_item['quantity'].",";
// Dynamic table row assembly
$cartOutput .= "<tr>";
$cartOutput .= '<td><a href="product.php?id=' . $item_id . '">' . $product_name . '</a><br /><img src="inventory_images/' . $item_id . '.jpg" alt="' . $product_name. '" width="40" height="52" border="1" /></td>';
$cartOutput .= '<td>' . $details . '</td>';
$cartOutput .= '<td>Rs ' . $price . '</td>';
$cartOutput .= '<td><form action="cart.php" method="post">
<input name="quantity" onkeypress="return onlyNos(event,this);" type="text" value="' . $each_item['quantity'] . '" size="1" maxlength="2" />
<input name="adjustBtn' . $item_id . '" type="submit" value="change" />
<input name="item_to_adjust" type="hidden" value="' . $item_id . '" />
</form></td>';
//$cartOutput .= '<td>' . $each_item['quantity'] . '</td>';
$cartOutput .= '<td>' . $pricetotal . '</td>';
$cartOutput .= '<td><form action="cart.php" method="post"><input name="deleteBtn' . $item_id . '" type="submit" value="X" /><input name="index_to_remove" type="hidden" value="' . $i . '" /></form></td> ';
$cartOutput .= '</tr>';
$i++;
}
$cartOutput=”“;
$cartotal=“”;
$pp_checkout_btn='';
$product\U id\U array='';
如果(!isset($_会话[“购物车阵列”])| |计数($_会话[“购物车阵列”])<1){
$cartOutput=“您的购物车是空的”;
}否则{
//启动贝宝结账按钮
//$pp\U结帐\U btn.='
//
//
//';
//为每个循环启动
$i=0;
foreach($\会话[“购物车数组”]作为$each项目){
$item_id=$each_item['item_id'];
$sql=mysql\u query(“从产品中选择*,其中id='$item\u id'限制1”);
while($row=mysql\u fetch\u数组($sql)){
$product_name=$row[“product_name”];
$price=$row[“price”];
$details=$row[“details”];
}
$pricetotal=$price*$每件物品['quantity'];
$cartTotal=$pricetotal+$cartTotal;
//setlocale(LC_MONETARY,“en_US”);
//$pricetotal=货币单位格式(“%10.2n”,$pricetotal);
//动态检测Btn组件
$x=$i+1;
$pp\U结帐\U btn.='
';
//创建产品数组变量
$product\U id\U array.=“$item\U id-”“$product\U name”。$each\U item['quantity']。”,“;
//动态表行组装
$cartOutput.=“”;
$cartOutput.='
';
$cartOutput.=''.$details';
$cartOutput.='Rs'.$price';
$cartOutput.='
';
//$cartOutput.=''.$each_item['quantity'.'”;
$cartOutput.=''.$pricetotal';
$cartOutput.='';
$cartOutput.='';
$i++;
}
现在这是我的购物车数组,我用它在我的购物车页面上呈现。
现在,我如何从购物车数组中获取值。我不知道您是否创建了与订单相关的内容,但通常,您首先需要创建db表和关系。例如,您将有表订单,也将有表订单项目。它们之间的关系是一对多,这意味着订单可以有多个项目和一个项目可能只有一份订单 除此之外,您的系统中有产品,所以我猜您已经创建了产品表。您还可以在产品和订单项目之间建立关系。关系再次是一对多,一个项目可以有一个产品,而产品可以有多个项目 因此,您应该是这样的:
order table:
id
value
customer_email
...
order_item table:
id
order_id
product_id
...
一个非常好的实践是,当您创建订单项目表时,将产品中的所有数据放入订单项目表中,如名称、价格、税等
这一点很重要,因为您需要该数据是历史数据,否则可能会丢失价格。例如,您的订单项目表可能会显示:
order_item table:
id
order_id
product_id
name
price
...
假设订单项中只有product_id作为外键,因为您可能会想“好的,当我需要检查一个订单中的项目并需要查看价格时,我可以通过外键获取它并从product表中抓取”.但若您更改产品表中的价格会发生什么?那个么您可能会永远失去订单创建时的价格
这将是一个开始,之后您可以很容易地使用sql查询从order和order_item表中获取所有订单信息制作一个html表并显示您的数据库值。但是在后端,这些值应该存储在order表中。那么我该如何处理呢?当然可以。在db中制作一个order表并使用while循环从dbNo中获取值!我正在告诉您如何将特定的用户信息存储到order表中。您是否尝试
插入order(col1,col2)值('value1','value2')
?当然,您需要正确命名列。