教程分类

Product category

6.6、php案例实战(定义特殊形式的数组、设计购物车、设计多文件上传)

编辑:57自学网 发布日期:2019-11-30 18:08
  • 高清线路
  • 备用线路
  • 图文解说

目录:

6.6案例实战

6.6.1定义特殊形式的数组

6.6.2设计购物车

6.6.3设计多文件上传


6.6.1定义特殊形式的数组
<?php
$array = [
    "1" => "a",
    "2" => "b",
];
var_dump($array);
?>
<?php
$array = array(		//定义救组
    1    => "a",
    "1"  => "b",
    1.5  => "c",
    true => "d",
);
print_r($array); 		//输出数组元素
echo "<br>";
echo $array[0]; 		//抛出错误
echo $array[1]; 		//输出数组元素的值
?> 
<?php
$array = array(
    "a" => "first",
    "b" => "second",
    1   => 1,
    -1  => -1,
);
var_dump($array);
?> 
<?php
$array = array("a", "1");
var_dump($array);
?> 
<?php
$array = array("a", 5 => "1", "b");
var_dump($array);
?> 
6.6.2设计购物车
<!doctype html>
<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@2@3@4";
$arrayid=explode("@",$name);				//将商品ID的字符串转换到数组中
$arraynum=explode("@",$price);				//将商品数量的字符串转换到数组中
$arraycount=explode("@",$counts);			//将商品数量的字符串转换到数组中
//获取用户更新数据
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++){ 		 //for循环读取数组中的数据
?>
    <form name="form1_<?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]; ?>">
                <input type="submit" name="Submit" value="更改"></td>
            <td><?php echo $arraycount[$i]*$arraynum[$i]; ?></td>
        </tr>
    </form>
<?php
}
?>
</table>
</body>
</html>

6.6.3设计多文件上传

素材图片:/images/btn.jpg

素材图片:/upfile/1.jpg   2.jpg   3.jpg   4.jpg   5.jpg    (这5张图为测试图片,请自行随便找几张)

ok.php 文件代码:
<?php  
if(!is_dir("./upfile")){ 							//判断服务器中是否存在指定文件夹
	mkdir("./upfile");								//如果不存在,则创建文件夹
}
array_push($_FILES["picture"]["name"],"");			//向表单提交的数组中增加一个空元素   
$array=array_unique($_FILES["picture"]["name"]);    //删除数组中重复的值
array_pop($array);             						//删除数组中最后一个单元
for($i=0;$i<count($array);$i++){					//根据元素个数执行for循环
	$path="upfile/".$_FILES["picture"]["name"][$i];	//定义上传文件存储位置
	if(move_uploaded_file($_FILES["picture"]["tmp_name"][$i],$path)){	//执行文件上传操作
		$result=true;
	}else{
		$result=false;
	}
}
if($result==true){
		echo "文件上传成功,请稍等...";
		echo "<meta http-equiv=\"refresh\" content=\"1; url=test.php\">";
	}else{
		echo "文件上传失败,请稍等...";
		echo "<meta http-equiv=\"refresh\" content=\"1; url=test.php\">";
}
?>
多文件上传代码:
<!doctype html>
<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>
<h1>多文件同传(5个文件以内任意上传)</h1>
<table>
    <form action="ok.php" method="post" enctype="multipart/form-data" name="form1">
        <tr>
            <td>内容1:</td>
            <td><input name="picture[]" type="file" id="picture[]" size="30"></td>
        </tr>
        <tr>
            <td>内容2:</td>
            <td><input name="picture[]" type="file" id="picture[]" size="30"></td>
        </tr>
        <tr>
            <td>内容3:</td>
            <td><input name="picture[]" type="file" id="picture[]" size="30"></td>
        </tr>
        <tr>
            <td>内容4:</td>
            <td><input name="picture[]" type="file" id="picture[]" size="30"></td>
        </tr>
        <tr>
            <td>内容5:</td>
            <td><input name="picture[]" type="file" id="picture[]" size="30"></td>
        </tr>
        <tr>
            <td colspan="2"><input type="image" name="imageField" src="images/btn.jpg"></td>
        </tr>
    </form>
</table>
</body>
</html>


 
上一篇:6.5、PHP操作元素(查询指定、获取、添加、删除元素) 下一篇:第7章 使用PHP与网页交互