PHP自学3——在html的<table>标签中显示用户提交表单
为了更好地显示用户提交表单,本节将在上一节的基础上将读取的用户表单显示在html的<table>标签中,这一节将用到和数组有关的知识。
本节代码将从外部文件(.txt文件)中读取信息于指定数组中,然后对逐条订单进行处理,最后将处理后数据显示于<table>表单之中。
表单读取文件——viewOrders2.php文件:
<html>
<head>
<title>Wayne's Drink Shop - Customer Orders</title>
</head>
<body>
<?php
//Get the root of the Web Server's Document in a variable
$DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT'];
?>
<h1>Wayne's Drink Shop</h1>
<h2>Customer Orders</h2>
<?php
//load the file's data into the array
$orders = file("$DOCUMENT_ROOT/../orders/DrinkOrder.txt");
//Handle the error in reading data from file
if(!$orders){
echo "<p><strong>Can't fetch files data</strong></p>";
exit;
}
$orderNum = count($orders);
//Output data in the html's table tag
echo "<table border=\"1\"><tr bgcolor=\"#CCCCCC\">".
"<th>Order Date</th>".
"<th>Milk</th>".
"<th>Coke</th>".
"<th>Tea</th>".
"<th>Coffee</th>".
"<th>Juice</th>".
"<th>Total</th>".
"<th>Address</th></tr>";
for($i = 0; $i < $orderNum; ++$i){
//Warning:You must use "\t" not '\t' !!!
$line = explode("\t", $orders[$i]);
$line[1] = intval($line[1]);
$line[2] = intval($line[2]);
$line[3] = intval($line[3]);
$line[4] = intval($line[4]);
$line[5] = intval($line[5]);
//iterator the $line
foreach($line as $data){
echo "<td>". $data. "</td>";
}
echo "</tr>";
}
echo "</table>";
?>
</body>
</html>
注意:要注意单引号''和双引号的区别,单引号''仅仅表示字符本身不代表其他任何特殊含义,其中的'\t'并不会转义解释成水平制表;而双引号""定义的一些特殊字符(如转义字符)和变量会被解析,如"\t"就表示水平制表符。
表单显示结果:
修订于2016/3/3 By野马菌
爱上一匹野马,可是我家里没有草原o(╯□╰)o
浙公网安备 33010602011771号