<!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
:
}
table
{
*border
-collapse
: collapse
;
border
-spacing
: 0;
width
: 100%;
}
table
{
border
: solid
-moz
-border
-radius
: 6px
;
-webkit
-border
-radius
: 6px
;
border
-radius
: 6px
;
-webkit
-box
-shadow
: 0 1px
1px
-moz
-box
-shadow
: 0 1px
1px
box
-shadow
: 0 1px
1px
}
table tr
:hover
{
background
:
-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
border
-top
: 1px solid
padding
: 10px
;
text
-align
: left
;
}
table th
{
background
-color
:
background
-image
: -webkit
-gradient(linear
, left top
, left bottom
, from(
background
-image
: -webkit
-linear
-gradient(top
,
background
-image
: -moz
-linear
-gradient(top
,
background
-image
: -ms
-linear
-gradient(top
,
background
-image
: -o
-linear
-gradient(top
,
background
-image
: linear
-gradient(top
,
-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);
$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()将改变后的数组元素重新拼接成新的字符串保持并显示。