首先在服务端存有文件orders.txt
将文件载入数组并显示,创建vieworder2.php
<?php
$DOCUMENT_ROOT =$_SERVER['DOCUMENT_ROOT'];
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>客户订单</title>
<style>
table,tr,th,td{
border-collapse: collapse;
border: 1px solid #ccc;
padding: 5px;
}
th{
background-color: #ccccff;
}
td{
text-align: right;
}
</style>
</head>
<body>
<h1>我们的商店</h1>
<h2>客户订单</h2>
<?php
//将文件载入
$orders=file("$DOCUMENT_ROOT/L03/files/orders.txt");
//统计元素个数
$number_of_orders=count($orders);
//错误信息提示
if($number_of_orders==0){
echo "<p><strong>没有订单信息,请再试一试!</strong></p>";
}
echo "<table>
<tr>
<th>订购日期</th><th>男装</th><th>鞋子</th><th>眼镜</th><th>总计</th><th>收货地址</th>
</tr>";
for($i=0;$i<$number_of_orders;$i++){
//按空格对元素进行分割
$line=explode("\t",$orders[$i]);
//将一个字符串转换为一个整数
$line[1]=intval($line[1]);
$line[2]=intval($line[2]);
$line[3]=intval($line[3]);
echo "<tr>
<td>$line[0]</td><td>$line[1]</td><td>$line[2]</td><td>$line[3]</td><td>$line[4]</td><td>$line[5]</td>
</tr>";
}
echo "</table>";
?>
</body>
</html>
最后显示的页面内容为:
补充知识点:
<?php
//创建一个数组
$products=array('cloths','shoes','glasses');
//创建一个1~10的升序数组
$numbers=range(1,10);
//创建一个1~10的奇数数组
$odds=range(1,10,2);
//创建一个a~z的字母数组
$letters=range('a','z');
//改变数组元素的内容
$products[0]='spinner';
//增加一个新元素到数组末尾
$products[3]='skit';
//显示数组内容
echo "$products[0] $products[1] $products[2] $products[3]";
//使用循环访问数组
echo '<br/>';
for($i=0;$i<26;$i++){//使用for循环
echo $letters[$i]." ";
}
echo '<br/>';
foreach($odds as $current){//使用foreach
echo $current." ";
}
//初始化关联数组
$prices=array('cloths'=>150,'shoes'=>300,'glasses'=>35);
//使用循环语句访问关联数组的3种方法
echo '<br/>';
foreach($prices as $key=>$value){//使用foreach
echo $key.'_'.$value.'<br/>';
}
reset($prices);//使用reset函数将当前元素重新设置到数组开始处
while($element=each($prices)){//使用each函数
echo $element['key'].'_'.$element['value'].'<br/>';
}
reset($prices);
while(list($product,$price)=each($prices)){//使用list函数
echo "$product - $price<br/>";
}
//创建一个二维数组
$products2=array( array('CLOTH','cloths',150),
array('SHOE','shoes',300),
array('GLASS','glasses',35));
//使用双重for来访问数组元素
for($row=0;$row<3;$row++){
for($column=0;$column<3;$column++){
echo ' | '.$products2[$row][$column];
}
echo ' |<br/>';
}
//创建一个二维关联数组
$products3=array(array('Code'=>'CLOTH','Description'=>'cloths','Price'=>150),
array('Code'=>'SHOE','Description'=>'shoes','Price'=>300),
array('Code'=>'GLASS','Description'=>'glasses','Price'=>35));
//使用for循环访问二维关联数组
for($row=0;$row<3;$row++){
echo '|'.$products3[$row]['Code'].'|'.$products3[$row]['Description'].'|'.$products3[$row]['Price'].'|<br/>';
}
//使用each函数和list函数来访问
reset($products3);
for($row=0;$row<3;$row++){
while(list($key,$value)=each($products3[$row])){
echo "|$value";
}
echo '|<br/>';
}
/************数组排序*****************/
//使用sort函数按字母升序进行排序
$products4=array('cloths','shoes','glasses');
sort($products4);
//使用sort函数按数字升序进行排序
$prices3=array(150,300,35);
sort($prices3);
//使用asort函数根据数组的每个元素值进行排序
$prices4=array('cloths'=>150,'shoes'=>300,'glasses'=>35);
asort($prices4);
//使用ksort函数根据数组的关键字进行排序
ksort($prices4);
//另外对应的反向排序方法有rsort()、arsort()、krsort();
//用户定义排序:usort()
$products5=array( array('CLOTH','cloths',150),array('SHOE','shoes',300), array('GLASS','glasses',35));
function compare($x,$y){
if($x[1]==$y[1]){
return 0;
}else if($x[1]<$y[1]){
return -1;
}else{
return 1;
}
}
usort($products5,'compare');
//对数组进行重新排序:随机排序shuffle()、反向排序array_reverse()
$numbers=range(1,10);
//$numbers=array_reverse($numbers);
$numbers=shuffle($numbers);
for($i=0;$i<3;$i++){//使用for循环
echo $numbers[$i]." ";
}
数组常用函数补充
current($array_name)——返回第一个元素;
next($array_name)——将指针前移,然后再返回新的当前元素;
each($array_name)——在指针前移一个位置之前返回当前元素;
reset($array_name)——将返回指向数组第一个元素的指针;
end($array_name)——将指针移到数组末尾;
prev($array_name)——将当前指针往回移再返回新的当前元素,与next()相反;
array_walk()——对数组的每一个元素应用任何函数;
count()、sizeof()、array_count_values()——统计数组元素个数;
extract()——通过一个数组创建一系列的标量变量;
更多数组的知识点请查看php手册_数组