zoukankan      html  css  js  c++  java
  • 数据写入文本文件并读出到浏览器的PHP代码

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
           <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
          <title>gwbjx123</title>
          <link rel="stylesheet" style type="text/css" href="style/basic.css"/>
        </head>
     <body>
        <form action="eg1.php" method="post">
         <table cellspacing="2" cellpadding="0" border="0">
          <tr>
             <th width="80" bgcolor="red">Item</th>
             <th width="200" bgcolor="red">Quantity</th>
          </tr>
          <tr>
              <td width="100" bgcolor="green">Tires</td>
              <td><input type="text" name="tires" size="5"/></td>
          </tr>
          <tr>
              <td width="100" bgcolor="green">oil</td>
              <td><input type="text" name="oil" size="5"/></td>
          </tr>
          <tr>
              <td width="100" bgcolor="green">Spark Plugs</td>
              <td><input type="text" name="spark" size="5"/></td>
          </tr>
          <tr>
              <td width="100" bgcolor="green">Address</td>
              <td><input type="text" name="address" size="30"/></td>
          </tr>
          <tr>
           <td colspan="2" align="center"><input type="submit" value="submit"/></td>
          
          </tr>
          </table>
         </form>
      </body>
    </html>


    以下是PHP文件:eg1.php

    <?php
       $tires = $_POST['tires'];
       $oil = $_POST['oil'];
       $spark = $_POST['spark'];
       $address = $_POST['address'];
       $outputstring = $tires." ".$oil." ".$spark." "
          .$address." ";
          echo"文件存储中......<br/>";
       $fp = fopen("egtext.txt","ab");//a是追加
        flock($fp,LOCK_EX);
        if($fp){
       fwrite($fp,$outputstring,strlen($outputstring));
        }
       flock($fp,LOCK_UN);
       fclose($fp);
       echo "文件存储完毕。"
    ?>

    <html>
      <body>
       
        <h1>如下是数据显示</h1>
        <hr width="80%" height="1" color="red">
        <?
          $fp = fopen("egtext.txt","rb");
          flock($fp,LOCK_EX);
           while(!feof($fp)){
              $order = fgets($fp,999);
              echo $order."<br/>";
            }
            flock($fp,LOCK_UN);
            fclose($fp);
        ?> 
     </body>

    </html>

    <html>
       <h2>bob's记录表</h2>
       <? 
          $orders = file("egtext.txt");//file文件载入文本文件,文件中每行为1个元素(记录)。
          $count_orders = count($orders);//统计共有多少条记录。
          if($count_orders==0){
              echo "<p> No orders pending</p>";
           }
          
           echo "<table border="1">";
           
           echo "<tr><th bgcolor="#ccccff">Tires</th>
                 <th bgcolor="#ccccff">oil</th>
                 <th bgcolor="#ccccff">spark</th>
                 <th bgcolor="#ccccff">address</th>
                 
           </tr>";
           for($i=0;$i< $count_orders;$i++){
              $line = explode(" ",$orders[$i]);// 的双引号一定要双引号。否则数据显示错误
               
               $line[0]=intval($line[0]);      //可能因为添加数据的时候代码中为" "分割
                 $line[1]=intval($line[1]);
                  $line[2]=intval($line[2]);
              echo "<tr><td align="right">".$line[0]."</td>
                        <td align="right">".$line[1]."</td>
                        <td align="right">".$line[2]."</td>
                        <td align="right">".$line[3]."</td>
                    </tr>";
              
             
            }
          echo "</table>";
       ?> 
    </html>

  • 相关阅读:
    MySQL日志概述
    MySQL事务概述
    MySQL存储引擎
    Linux软件安装,RPM与YUM
    使用PuTTY在Windows中向Linux上传文件
    Linux网络基础
    Java正则表达式实例详解
    Javascript正则构造函数与正则表达字面量&&常用正则表达式
    常用sql语句及案例(oracle)
    oracle数据导入/导出(2)
  • 原文地址:https://www.cnblogs.com/freeze89/p/3794161.html
Copyright © 2011-2022 走看看