zoukankan      html  css  js  c++  java
  • 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 "	" not '	' !!!
            $line = explode("	", $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>

    注意:要注意单引号''和双引号的区别,单引号''仅仅表示字符本身不代表其他任何特殊含义,其中的' '并不会转义解释成水平制表;而双引号""定义的一些特殊字符(如转义字符)和变量会被解析,如" "就表示水平制表符。

    表单显示结果:

    修订于2016/3/3  By野马菌

    爱上一匹野马,可是我家里没有草原o(╯□╰)o
  • 相关阅读:
    Java中返回参数值的几种状态
    Java中的二维数组
    Java foreach操作(遍历)数组
    Arrays 类操作 Java 的数组排序
    Java循环语句 for
    Java循环语句 while
    Java条件语句 switch case
    Java多重if....else if
    margin优化的一种思路
    5.命名规则
  • 原文地址:https://www.cnblogs.com/yemajun/p/5239154.html
Copyright © 2011-2022 走看看