用session 做一个简单的购物车,我的思想如下,
1 通过点击连接进行传递商品代号$id,
2 读取txt文件中产品信息,将每条信息写入到一个数组里面
3 然后将,$id 与数组中每条信息的id对比,找到相应数据, 取出产品 id 与 price
4 将 id 与price 存入数组$shoppingcart[0]["code"]与 $shoppingcart[0]["price"]中, 然后由数组读出,显示购物信息,
5 下次 点击传入另一个$id 找到对应的 id 与price, 存入数组第二个 index中($shoppingcart[1]["code"]与 $shoppingcart[1]["price"])中, 然后运算现实价格,总价格等购物信息! 遇到问题:做到第四步的时候没有问题,但是,一个新的$id传进来的时候, 原来存入数组的数据就没了,不知道为什么, 请哪位朋友教教我! 我的代码如下:我这里设置数组的index为session变量,用来保证,每次存取的时候,存到下一个index中去 。 <?php <?php session_start();?>
if(! isset($_SESSION['user']) )
echo "you are not login, please login or register user";
else
{
$user=$_SESSION['user'];
$id=$_GET['id'];
$lines = file("singleproduct.txt"); $count=0;
foreach ($lines as $line_num => $line) {
$count=$count+1;
$new=preg_split("/;/", $line);
$total[$count] = array(
code => $new[0],
img => $new[1],
price => $new[2],
country =>$new[3],
descript =>$new[4],
);
if($id==$total[$count]["code"])
{ $code=$id;
$price=$total[$count]["price"];
break;
}
}
if(! isset($_SESSION['number']) )
{
session_register("number");
$_SESSION['number']=0; }
$secc=$_SESSION['number'];
$shopping[$secc]=array(
code => $id,
price => $price,
);
echo "this is test for secc".$secc."this is test for secc<br />"; echo "this is test for code id".$shopping[$secc] ["code"]."this is test for code id<br />";
echo "this is test for price".$shopping[$_SESSION['number']] ["price"]."this is test for price<br />"; echo $shopping[$_SESSION['number']] ["code"];
echo $_SESSION['number'];
echo "<h2>Hello ".$user." your selected list</h2><br /><br /> ";
for($i=0;$i<$_SESSION['number'];$i++){
$testcode= $shopping[$i]["code"];
$testprice= $shopping[$i]["price"];
echo $testcode."this is special test"; // 问题就在这里过不去,当新的$id传过来的时候, 只重复当前$id的code 跟price, 上次的没有
echo $testprice."this is special test"; //这里每次都是 重复显示新传进来$id,对应的值。 }
$_SESSION['number']++;
echo $_SESSION['number'];
}
?>
1 通过点击连接进行传递商品代号$id,
2 读取txt文件中产品信息,将每条信息写入到一个数组里面
3 然后将,$id 与数组中每条信息的id对比,找到相应数据, 取出产品 id 与 price
4 将 id 与price 存入数组$shoppingcart[0]["code"]与 $shoppingcart[0]["price"]中, 然后由数组读出,显示购物信息,
5 下次 点击传入另一个$id 找到对应的 id 与price, 存入数组第二个 index中($shoppingcart[1]["code"]与 $shoppingcart[1]["price"])中, 然后运算现实价格,总价格等购物信息! 遇到问题:做到第四步的时候没有问题,但是,一个新的$id传进来的时候, 原来存入数组的数据就没了,不知道为什么, 请哪位朋友教教我! 我的代码如下:我这里设置数组的index为session变量,用来保证,每次存取的时候,存到下一个index中去 。 <?php <?php session_start();?>
if(! isset($_SESSION['user']) )
echo "you are not login, please login or register user";
else
{
$user=$_SESSION['user'];
$id=$_GET['id'];
$lines = file("singleproduct.txt"); $count=0;
foreach ($lines as $line_num => $line) {
$count=$count+1;
$new=preg_split("/;/", $line);
$total[$count] = array(
code => $new[0],
img => $new[1],
price => $new[2],
country =>$new[3],
descript =>$new[4],
);
if($id==$total[$count]["code"])
{ $code=$id;
$price=$total[$count]["price"];
break;
}
}
if(! isset($_SESSION['number']) )
{
session_register("number");
$_SESSION['number']=0; }
$secc=$_SESSION['number'];
$shopping[$secc]=array(
code => $id,
price => $price,
);
echo "this is test for secc".$secc."this is test for secc<br />"; echo "this is test for code id".$shopping[$secc] ["code"]."this is test for code id<br />";
echo "this is test for price".$shopping[$_SESSION['number']] ["price"]."this is test for price<br />"; echo $shopping[$_SESSION['number']] ["code"];
echo $_SESSION['number'];
echo "<h2>Hello ".$user." your selected list</h2><br /><br /> ";
for($i=0;$i<$_SESSION['number'];$i++){
$testcode= $shopping[$i]["code"];
$testprice= $shopping[$i]["price"];
echo $testcode."this is special test"; // 问题就在这里过不去,当新的$id传过来的时候, 只重复当前$id的code 跟price, 上次的没有
echo $testprice."this is special test"; //这里每次都是 重复显示新传进来$id,对应的值。 }
$_SESSION['number']++;
echo $_SESSION['number'];
}
?>
{
session_register("number");
$_SESSION['number']=0; }
$secc=$_SESSION['number'];
$shopping[$secc]=array(
code => $id,
price => $price,
);
echo "this is test for secc".$secc."this is test for secc<br />"; 这段是存数组的,我看不 出什么问题啊,能帮我看看吗? 谢谢