PHP笔记:设计一个购物车,综合运用PHP各种数组,实现更新数组中的元素值。

<!DOCTYPE>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
body {
    width: 600px;
    margin: 40px auto;
    font-family: 'trebuchet MS', 'Lucida sans', Arial;
    font-size: 14px;
    color: #444;
}
table {
 *border-collapse: collapse; /* IE7 and lower */
    border-spacing: 0;
    width: 100%;
}
table {
    border: solid #ccc 1px;
    -moz-border-radius: 6px;
    -webkit-border-radius: 6px;
    border-radius: 6px;
    -webkit-box-shadow: 0 1px 1px #ccc;
    -moz-box-shadow: 0 1px 1px #ccc;
    box-shadow: 0 1px 1px #ccc;
}
table tr:hover {
    background: #fbf8e9;
    -o-transition: all 0.1s ease-in-out;
    -webkit-transition: all 0.1s ease-in-out;
    -moz-transition: all 0.1s ease-in-out;
    -ms-transition: all 0.1s ease-in-out;
    transition: all 0.1s ease-in-out;
}
table td, table th {
    border-left: 1px solid #ccc;
    border-top: 1px solid #ccc;
    padding: 10px;
    text-align: left;
}
table th {
    background-color: #dce9f9;
    background-image: -webkit-gradient(linear, left top, left bottom, from(#ebf3fc), to(#dce9f9));
    background-image: -webkit-linear-gradient(top, #ebf3fc, #dce9f9);
    background-image: -moz-linear-gradient(top, #ebf3fc, #dce9f9);
    background-image: -ms-linear-gradient(top, #ebf3fc, #dce9f9);
    background-image: -o-linear-gradient(top, #ebf3fc, #dce9f9);
    background-image: linear-gradient(top, #ebf3fc, #dce9f9);
    -webkit-box-shadow: 0 1px 0 rgba(255,255,255,.8) inset;
    -moz-box-shadow: 0 1px 0 rgba(255,255,255,.8) inset;
    box-shadow: 0 1px 0 rgba(255,255,255,.8) inset;
    border-top: none;
    text-shadow: 0 1px 0 rgba(255,255,255,.5);
}
table td:first-child, table th:first-child {
    border-left: none;
}
table th:first-child {
    -moz-border-radius: 6px 0 0 0;
    -webkit-border-radius: 6px 0 0 0;
    border-radius: 6px 0 0 0;
}
table th:last-child {
    -moz-border-radius: 0 6px 0 0;
    -webkit-border-radius: 0 6px 0 0;
    border-radius: 0 6px 0 0;
}
table th:only-child {
    -moz-border-radius: 6px 6px 0 0;
    -webkit-border-radius: 6px 6px 0 0;
    border-radius: 6px 6px 0 0;
}
table tr:last-child td:first-child {
    -moz-border-radius: 0 0 0 6px;
    -webkit-border-radius: 0 0 0 6px;
    border-radius: 0 0 0 6px;
}
table tr:last-child td:last-child {
    -moz-border-radius: 0 0 6px 0;
    -webkit-border-radius: 0 0 6px 0;
    border-radius: 0 0 6px 0;
}
</style>
</head>
<body>
<?php
//初始化数据
$name = "平板电脑@数码相机@智能手机@瑞士手表";    //定义商品名称字符串
$price ="14998@2588@2666@66698";    //定义商品价格字符串
$counts = "1@0@0@0";    //定义页面加载后初始化数量
$arrayid=explode("@",$name);    //将商品ID的字符串转换到数组中
$arraynum=explode("@",$price);    //将商品价格的字符串转换到数组中
$arraycount=explode("@",$counts);   //将商品数量的字符串转换到数组中
?>
<?php
//获取用户更新数据
if(isset($_POST["Submit"]) && $_POST["Submit"]==true){
 $id=$_POST["name"];       //获取要更改的元素名称
 $num=$_POST["counts"];      //获取更改的值
 $key=array_search($id, $arrayid);      //在数组中搜索给定的值,如果成功返回键名 
 $arraycount[$key]=$num;      //更改商品数量
 $counts=implode("@",$arraycount);   //将更改后的商品数量添加到购物车中
}
?>
<h1>购物车<h1>
<table>
 <tr>
        <th>商品名称</th>
        <th>价 格</th>
        <th>数量</th>
        <th>金额</th>
 </tr>
<?php
for($i=0;$i<count($arrayid);$i++){
?>
 <form name="form_<?php echo $i;?>" method="post" action="">
  <tr>
   <td><?php echo $arrayid[$i];?></td>
   <td><?php echo $arraynum[$i];?></td>
   <td>
    <input name="counts" type="text" id="counts" value=<?php echo $arraycount[$i]; ?> size="8">
    <input name="name" type="hidden" id="name" value="<?php echo $arrayid[$i]; ?>"><!--隐藏只是在网页页面上面不显示输入框,但是虽然隐藏了,还是具有form传值功能。-->
    <input name="Submit" type="submit" value="更改">
   </td>
   <td><?php echo $arraycount[$i]*$arraynum[$i]; ?></td>
  </tr>
 </form>
<?php
}
?>
</table>
</body>
</html>

输出:
在这里插入图片描述
代码释疑:

explode    ( string $delimiter   , string $string   [, int $limit  ] ) : array

此函数返回由字符串组成的数组,每个元素都是 string 的一个子串,它们被字符串 delimiter 作为边界点分割出来。

在页面脚本中,首先初始化数据变量$name、$price、$counts,它们分别以字符串的形式存储了商品名称,价格和订购数量。然后,使用explode()函数将它们转换为数组。接着,使用for循环将它们显示在页面中。

当用户修改订购数量时,单击“更改”按钮提交时,则使用array_search()找到用户修改的商品名称对应的下标(默认0开始),更新订购数,再使用implode()将改变后的数组元素重新拼接成新的字符串保持并显示。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值