【发布时间】:2016-01-29 13:53:39
【问题描述】:
尊重所有人,
我在这里尝试制作购物车功能。我是数组创建的新手。在我的门户网站上,我有两种类型的页面 1) 包和 2) 产品。
现在我想用这个页面做什么,用户/客户可以从产品页面中选择单个项目,还可以获得包含多个项目的完整包。
使用 Mysql 和我在下面声明的简单 php 代码显示具有数量属性的项目列表。当客户选择“将此包添加到购物车”按钮时,我想使用 post 方法将此数组值添加到购物车会话数组值中。
我需要如果购物车会话数组已经有一些项目,那么包裹中的新项目将插入到购物车会话数组中。如果购物车中不包含任何商品,则启动购物车会话数组。
在这里,我向您展示我的测试代码。我在哪里尝试发布数组值并显示在页面上。但是如果将其添加到购物车会话数组中,下一部分是什么。
<?php
session_start();
$_SESSION['cart']=array();
include('php_scripts/db.php');
$pkg_id='1';
$sql="select * from `pkg_itm` where `pkg_id`='$pkg_id' order by `itm_id` ASC";
$run_sql=mysql_query($sql);
$count=mysql_num_rows($run_sql);
if(isset($_POST['submit'])){
if ( isset( $_POST['my_pkg'] ) )
{
echo '<table>';
foreach ( $_POST['my_pkg'] as $diam )
{
// here you have access to $diam['top'] and $diam['bottom']
echo '<tr>';
echo ' <td>', $diam['itm'], '</td>';
echo ' <td>', $diam['qty'], '</td>';
echo '</tr>';
}
echo '</table>';
}
}
?>
<html>
<head>
</head>
<body>
<h1><?php echo $pkg_id;?></h1>
<form action="" method="post" name="pkg">
<table>
<tr>
<td>
Item Id
</td>
<td>
Qty
</td>
</tr>
<?php
$i=0;
$my_pkg=array();
while($row=mysql_fetch_array($run_sql))
{
?>
<tr>
<td>
<?php echo $row['itm_id'];?>
<input type="hidden" value="<?php echo $row['itm_id'];?>" name="my_pkg[<?php echo $i;?>][itm]">
</td>
<td>
<input type="text" value="<?php echo $row['qty'];?>" name="my_pkg[<?php echo $i;?>][qty]" >
</td>
</tr>
<?php
// Push data to array
$i=$i+1;
}
?>
</table>
<input type="submit" name="submit">
</form>
</body>
</html>
【问题讨论】:
标签: session multidimensional-array shopping-cart