首页 > 编程技术 > php

php 生成word文档并下载代码

发布时间:2016-11-25 16:28

php教程 生成word文档并下载代码
include("include/conn_mysql教程.php");
  $id=$_GET["id"];
  $sql="select * from  down where id=$id ";
  $result=mysql_query($sql);  
  $row=mysql_fetch_row($result);
  $file2=$row["upload"];
 
function downFile($sFilePath)
{
   if(file_exists($sFilePath)){
       $aFilePath=explode("/",str_replace("\","/",$sFilePath),$sFilePath);
       $sFileName=$aFilePath[count($aFilePath)-1];
       $nFileSize=filesize ($sFilePath);
       header ("Content-Disposition: attachment; filename=" .basename($sFileName) );
       header ("Content-Length: " . $nFileSize);
       header ("Content-type: application/octet-stream");
       mb_convert_encoding(readfile($sFilePath),"utf-8","GB2312");
   }
   else
   {
       echo("文件不存在!");
   }
}

downFile($file2);
/*
这个生成word生成文档程序是从mysql数据库教程读取数据保存文件再用header来发送给浏览器。
*/

本文章来给各位朋友讲一下php 产品进销存管理系统开发实例教程哦。我们先看看有那些功能

首页文件 index.html

<HTML>
<HEAD>
<TITLE> 产品进销存管理系统 </TITLE>
</HEAD>

<BODY>

<table border="0" width="100%" cellpadding="2" height="12">
<tr>
<td width="100%">
      <h2 align=center> 产品进销存管理系统</h2>
    </td>
</tr>
<tr>
<td width="100%" height="6">
        <p align="center">
  <a href="index.html"><font color="#008000">返回首页</font></a>
  <font color="#8EB4D9">|</font>
  <a href="reg.php"><font color="#008000">注册</font></a>
  <font color="#8EB4D9">|</font>
  <a href="login.php"><font color="#008000">登录</font></a>
        <font color="#8EB4D9">|</font>
  <a href="addcompany.php"><font color="#008000">添加厂商</font></a>
  <font color="#8EB4D9">|</font>
  <a href="addproduct.php"><font color="#008000">添加产品</font></a>
        <font color="#8EB4D9">|</font>
  <a href="buy.php"><font color="#008000">进货</font></a>
  <font color="#8EB4D9">|</font>
  <a href="sell.php"><font color="#008000">销售</font></a>
  <font color="#8EB4D9">|</font>
  <a href="browse.php"><font color="#008000">查看存货</font></a>
        <font color="#8EB4D9">|</font>
  <a href="logout.php"><font color="#008000">注销</font></a>
    </td>
</tr>
<tr><td height=30></td></tr>
<tr><td align=center>
      <h2><font color=blue >欢迎光临进销存管理系统!</font></h2>
</td></tr>
</table>
</BODY>
</HTML>

 

本文章主要是一个php初学者写的原创的php日历控件 ,可以显示当前日期与今天是星期几,是否为闰年, 可自动选择上一年或下一年,月份日期也是一样的。

简单的php教程日历类控件代码实例
/*

*/
date_default_timezone_set("Etc/GMT-8");

class Calendar{

var $T = array();
var $datesOFmonth = array('1'=>'31','2'=>'28','3'=>'31','4'=>'30','5'=>'31','6'=>'30','7'=>'31','8'=>'31','9'=>'30','10'=>'31','11'=>'30','12'=>'31');
var $Y,$M,$D;

function set($time){
$this->T = getdate($time);
$this->Y = $this->T['year'];
$this->M = $this->T['mon'];
$this->D = date('d',$time);
}

function isRun(){
return ($this->Y%400==0 || ($this->Y%4==0 && $this->Y%100==0)) ? 1 : 0;
}

function first(){
$time = mktime(0,0,0,$this->M,1,$this->Y);
$time = getdate($time);
return $time['wday'];
}

function html(){
$isRun = $this->isRun();
$this->datesOFmonth[2] = $isRun==1 ? 29: 28;
$html .= "<table style='border:solid 1px black;'>n";
$html .= "<tr><th><a href=''>上一月</a></th><th colspan='5'>{$this->Y}年 {$this->M}月</th><th><a href=''>下一月</a></th><tr>n";
$html .= "<tr><td>星期天</td><td>星期一</td><td>星期二</td><td>星期三</td><td>星期四</td><td>星期五</td><td>星期六</td></tr>n";
$html .= "<tr>n";
$first = $this->first();
for($i=0; $i<$first; $i++){
$html .= "<td></td>";
}
$count = $this->datesOFmonth[$this->M]+$first;
for ($i=1; $i<= $this->datesOFmonth[$this->M]; $i++){
$style = $i==$this->D ? ' style="color:red;font-weight:bold;"' : '' ;
$html .= "<td align='center'{$style}>$i</td>";
if (($i==7%$first || ($i+$first)%7==0) && $i<$count){
$html .= "</tr>n<tr>";
}
}
$count = 7-$count%7;
if ($count<7){
for ($i=0; $i<$count; $i++){
$html .= "<td></td>";
}
}
$html .= "</tr>n";
$html .= "</table>n";
return $html;
}
}

$calendar = new Calendar();
$calendar->set(time());
echo $calendar->html();

这里我们为你提供个简单的php购物车代码哦,从增加购物产品与发生购买了,在商城开发中,这个功能是少不了的,我们不需要数据库,用了txt文本文件来操作用户购物的内容。

增加商品到购物车

 代码如下 复制代码

<?php
//
// add_item.php:
//  Add an item to the shopping cart.
//
session_start();
if (session_is_registered('cart')) {
    session_register('cart');
}

require 'lib.inc.php'; // LoadProducts()

LoadProducts(); // Load products in $master_products_list

// Make $curr_product global
$curr_product = array();

// Loop through all the products and pull up the product
// that we are interested in

 

foreach ($master_products_list as $prod_id => $product) {
    if (trim($prod_id) == trim($_GET[id])) {
        $curr_product = $product;
    }
}


// Register our session
//session_register('cart');
//if(session_is_registered('cart')) echo "已经注册";

 

if ($_POST[ordered]) {  // If they have chosen the product

    array_push($_SESSION[cart][products], array(trim($_POST[id]), $_POST[quantity]));
    $_SESSION[cart][num_items] += $_POST[quantity];
 
}
?>

<html>
<head>
    <title>
    <?php if ($_POST[ordered]) {  ?>
        已经添加 <?php echo $curr_product[name]; ?> 到您的购物篮
    <?php } else {  ?>
        添加 <?php echo $curr_product[name]; ?> 到您的购物篮
    <?php } ?>
    </title>
</head>
<body>
<?php if ($_POST[ordered]) {  ?>
    <h1><?php echo $curr_product[name]; ?>
        添加至购物篮成功</h1>

    <a href="cart.php">返回</a> 商品列表页面.
<?php }  else {  ?>
    <h1>添加 <?php echo $curr_product[name]; ?> 到您的购物篮</h1>

    <form action="<?php echo $PHP_SELF; ?>" method="post">
    商品名称: <?php echo $curr_product[name]; ?>
    <br>
    商品说明: <?php echo $curr_product[desc]; ?>
    <br>
    商品单价: RMB<?php echo $curr_product[price]; ?>
    <br>
    商品数量: <input type="text" size="7" name="quantity">
    <input type="hidden" name="id" value="<?php echo $_GET[id]; ?>">
    <input type="hidden" name="ordered" value="1">

    <input type="submit" value="添加至购物栏">
    </form>
<?php } ?>
</body>
</html>

查看购物车的商品

 代码如下 复制代码

<?php
//
// cart.php:  www.111cn.net
//
session_start();

require 'lib.inc.php';
//判断购物篮会话变量cart是否注册,不注册则注册cart变量
if (session_is_registered('cart')) {
    session_register('cart');
}


// 如果购物篮没有初始化,则初始化购物篮
if (!isset($_SESSION[cart][num_items])) {
    $_SESSION[cart] = array("num_items" => 0,
                  "products"  => array());
}


// From site_lib.inc, Loads the $master_products_list array
LoadProducts(); //载入物品列表
?>

<html>
<head>
    <title>演示会话跟踪的购物篮程序</title>
</head>

<body>

<h1>欢迎进入网上商店</h1>

<?php
if ($_SESSION[cart][num_items]) {  // If there is something to show
?>
<h2>当前在购物篮里的物品</h2>
<br>
<table border="2" cellpadding="5" cellspacing="2">
<tr>
    <th>
        商品名称
    </th>
    <th>
        商品说明
    </th>
    <th>
        单价
    </th>
    <th>
        数量
    </th>
    <th>&nbsp;
       
    </th>
</tr>
<?php
  
    // Loop through the products
    foreach ($_SESSION[cart][products] as $i => $product) {
        $product_id = $product[0];
        $quantity   = $product[1];

        $total += $quantity *
                  (double)$master_products_list[$product_id][price];
?>
<tr>
    <td>
        <?php echo $master_products_list[$product_id][name]; ?>
    </td>
    <td>
        <?php echo $master_products_list[$product_id][desc]; ?>
    </td>
    <td>
        <?php echo $master_products_list[$product_id][price]; ?>
    </td>
    <td>
        <form action="change_quant.php" method="post">
        <input type="hidden" name="id" value="<?php echo $i; ?>">
        <input type="text" size="3" name="quantity"
                value="<?php echo $quantity; ?>">
    </td>
    <td>
        <input type="submit" value="数量更改">
        </form>
    </td>
</tr>
<?php
    }
?>
<tr>
    <td colspan="2" ALIGN="right">
       <b>合计: </b>
    </td>
    <td colspan="2">
        RMB:<?php echo $total; ?>
    </td>
 <td>&nbsp;</td>
</tr>
</table>
<br>
<br>
<?php
}
?>

<h2>商店待出售的商品</h2>
<br>
<i>
    我们提供以下商品待售:
</i>
<br>
<table border="2" cellpadding="5" cellspacing="2">
<tr>
    <th>
        商品名称
    </th>
    <th>
        商品说明
    </th>
    <th>
        单价
    </th>
    <th>&nbsp;
       
    </th>
</tr>
<?php
    // Show all of the products
    foreach ($master_products_list as $product_id => $item) {
?>
<tr>
    <td>
        <?php echo $item[name]; ?>
    </td>
    <td>
        <?php echo $item[desc]; ?>
    </td>
    <td>
        $<?php echo $item[price]; ?>
    </td>
    <td>
        <a href="add_item.php?id=<?php echo $product_id; ?>">
            添加至购物篮
        </a>
    </td>
</tr>
<?php
    }

?>
</table>

修改购物车的数量

 代码如下 复制代码

<?php
//
// change_quant.php:
//   Change the quantity of an item in the shopping cart.
//
session_start();
if (session_is_registered('cart')) {
    session_register('cart');
}

// Typecast to int, making sure we access the
// right element below
$i = (int)$_POST[id];

// Save the old number of products for display
// and arithmetic
$old_num = $_SESSION[cart][products][$i][1];

if ($_POST[quantity]) {
    $_SESSION[cart][products][$i][1] = $_POST[quantity]; //change the quantity
} else {
    unset($_SESSION[cart][products][$i]); // Send the product into oblivion
}

// Update the number of items
$_SESSION[cart][num_items] = ($old_num >$_POST[quantity]) ?
                   $_SESSION[cart][num_items] - ($old_num-$_POST[quantity]) :
                   $_SESSION[cart][num_items] + ($_POST[quantity]-$old_num);
?>

<html>
<head>
    <title>
        数量修改
    </title>
</head>
<body>
    <h1> 将数量: <?php echo $old_num; ?> 更改为
         <?php echo $_POST[quantity]; ?></h1>
    <a href="cart.php">返回</a> 商品列表页面.
</body>
</html>

功能页面,用户把购物车里面的内容保存到txt数据库

 代码如下 复制代码

<?php
//物品数组
$master_products_list = array();


//载入物品数据函数
function LoadProducts() {
    global $master_products_list;
    $filename = 'products.txt';

    $fp = @fopen($filename, "r")
        or die("打开 $filename 文件失败");
    @flock($fp, 1)
        or die("锁定 $filename 文件失败");

    //读取文件内容
    while ($line = fgets($fp, 1024)) {
        list($id, $name, $desc, $price) = explode('|', $line); //读取每行数据,数据以| 格开
        $id = trim($id); //去掉首尾特殊符号
        $master_products_list[$id] = array("name" =>  $name, //名称
                                           "desc" =>  $desc, //说明
                                           "price" => $price); //单价
    }

    @fclose($fp)  //关闭文件
        or die("关闭 $filename 文件失败");
}
?>

很简单,我们只用了4个文件就实现用php 做好购物车功能哦,好了这只是一款简单的php购物车代码更复杂的需要考虑更多更好。

//发送留言

require("global.func.php教程");
$fid=isset($_GET["id"])?intval($_GET["id"]):0;
$page=isset($_GET["page"])?intval($_GET["page"]):1;
if($_SERVER["REQUEST_METHOD"]=="POST")
{
 if(!file_exists("1.php"))
 {
  writeFile("1.php",$array=array());
 }
 $arr=require("1.php");
 $post=array();
 unset($_POST["Submit"]);
 foreach($_POST as $k=>$v)
 {
  $post[$k]=addslashes(htmlspecialchars($v));
 }
 $post["content"]=str_replace(" ","<br>",$post["content"]);
 $post["id"]=count($arr)+1;
 $post["date"]=time();
 $post["fid"]=$fid;
 $post["ip"]=$_SERVER["REMOTE_ADDR"];
 $arr[]=$post;

 writeFile("1.php",$arr);
 
 echo "<script>location.href="index.php?page=$page"</script>";
 exit;
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>发帖</title>
<script>
var arr=new Array();

     function ckusername()
     {
      if(document.form1.username.value.length<3)
      {
       document.getElementById("user_msg").innerHTML="<font color="red"><b>用户名不能小于3个字符</b></font>";
       arr[0]=1;
      }else
      {
      document.getElementById("user_msg").innerHTML="<font color="green"><b>√</b></font>";
      arr[0]=0;
      }
      
      
     }
     function ckcontent()
     {
      
      if(document.form1.content.value.length<8||document.form1.content.value.length>255)
      {
       document.getElementById("content_msg").innerHTML="<font color="red"><b>内容8-255个字符</b></font>";
       arr[1]=1;
      }else
      {
       document.getElementById("content_msg").innerHTML="<font color="green"><b>√</b></font>";
       arr[1]=0;
      }
     }
     
     function c()
     {
      if(arr.length<2)
      {
       return false;
      }
      for(x in arr)
      {
       if(arr[x]==1)
       {
        return false;
       }
      }
      
      form1.submit();
     
     }
     
</script>
</head>
<body>
<form name="form1" method="post" action="">
  <table width="100%"  border="0">
    <tr>
      <td>用户
      <input name="username" type="text" id="id" onBlur="ckusername()" ></td>
    </tr>
 <tr>
      <td><div id="user_msg"></div>
      </td>
    </tr>
    <tr>
      <td><textarea name="content" cols="100" rows="6" id="content" onBlur="ckcontent()"></textarea></td>
    </tr>
 <tr>
      <td><div id="content_msg"></div>
      </td>
    </tr>
    <tr>
      <td><div align="center">
        <input type="button" name="Submit" value="提交" onClick="c();">
      </div></td>
    </tr>
  </table>
</form>
</body>

</html>

<?php
ini_set("display_errors",0);
require("global.func.php");
?>

显示像163评论
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.111cn.net/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style>
.b{
border:1px dotted #e3e3e3;
word-wrap:break-word;
word-break:break-all;
}


</style>

<title>PPC备用论坛(递归限制15层+主题)</title>
</head>
<body>
<?php
echo "<a href="post.php?page=1"> 发帖</a><br>";
if(file_exists("1.php"))
{
 $__array=require("1.php");
}
else
{
 $__array=array();
}
$page=isset($_GET["page"])?intval($_GET["page"]):1;
$pagesize=5;
$offset=($page-1)*$pagesize;
$__str="";
function getTotal()
{
 global $__array;
 $i=0;
 foreach($__array as $k=>$v)
 {
  if($v["fid"]==0)
  {
   $i=$i+1;
  }
 }
 return $i;
}
function getFid0()
{
 global $__array,$__str;
 
 foreach ($__array as $k=>$v)
 {
  if($v["fid"]==0)
  {
   $fid0[]=$v;
  }
  
 }
 return array_reverse($fid0);
}
$fid0=getFid0();
function show($fid0,$pagesize,$offset)
{
 global $__str,$page;
 foreach (array_slice($fid0,$offset,$pagesize,true) as $k=>$v)
 {
  
   $__str.="<blockquote class="b"><p>";
   $__str.=$v["ip"]."用户:".$v["username"]."内容:".$v["content"]."<a id="a1" href="post.php?id=".$v["id"]."&page=$page">回复</a>";
   showChild($v["id"]);
   $__str.="</p></blockquote>";
   
  
  
 }
}

function showChild($id,$i=0)
{
 global $__array,$__str,$page,$offset;
 
 
 if($i==15)//递归层数
 {
  return;
 }
 $i++;
 foreach($__array as $k=>$v)
 {
  if($v["fid"]==$id)
  {
   $__str.= "<blockquote class="b"><p id="a1">";
   $__str.= $__array[$k]["ip"]."用户:".$__array[$k]["username"]."内容:".$__array[$k]["content"]."<a href="post.php?id=".$v["id"]."&page=$page">回复</a>";
   showChild($v["id"],$i);
   $__str.= "</p></blockquote>";
  }
 }
}

show($fid0,$pagesize,$offset);
$total=getTotal();

echo $__str;
pageft($total,$pagesize,"index.php");
echo "<br>".$pagenav;
?>
</body>
</html>

global.func.php文件代码如下

<?php

function writeFile($id,$array)
{
 $content="<?php return ".var_export($array,true)."?>";
 return safe_file_put_contents($id,$content);
}
/**
*写入文件
*@param string $filename
*@param string $contents
*@return boolean
*/
function safe_file_put_contents($filename,&$contents)
{
 $fp=fopen($filename,"wb");
 if($fp)
 {
  flock($fp,LOCK_EX);
  fwrite($fp,$contents);
  flock($fp,LOCK_UN);
  fclose($fp);
  return true;
 }else
 {
  return false;
 }
}


/**
*读取文件
*@param string $filename
*@return $data|false
*/
function safe_file_get_contents($filename)
{
 $fp=fopen($filename,"rb");
 if($fp)
 {
  flock($fp,LOCK_SH);
  clearstatcache();
  $filesize=filesize($filename);
  if($filesize>0)
  {
   $data=fread($fp,$filesize);
  }else
  {
   $data=false;
  }
  flock($fp,LOCK_UN);
  fclose($fp);
  return $data;
 }else
 {
  return false;
 }
}
function pageft($total,$displaypg=10,$url=''){
global $page,$firstcount,$pagenav,$_SERVER;

$GLOBALS["displaypg"]=$displaypg;

if(!$page) $page=1;

if(!$url){ $url=$_SERVER["REQUEST_URI"];}

$parse_url=parse_url($url);
@$url_query=$parse_url["query"];
if($url_query){
$url_query=ereg_replace("(^|&)page=$page","",$url_query);

$url=str_replace($parse_url["query"],$url_query,$url);

if($url_query) $url.="&page"; else $url.="page";
}else {
$url.="?page";
}

$lastpg=ceil($total/$displaypg);
$page=min($lastpg,$page);
$prepg=$page-1;
$nextpg=($page==$lastpg ? 0 : $page+1);
if(!$page) $page=1;
$firstcount=($page-1)*$displaypg;


$pagenav.=" <a href='$url=1' class='s1'>首页</a> ";
if($prepg) $pagenav.=" <a href='$url=$prepg' class='s1'>上页</a> "; else $pagenav.="上页 ";
if($nextpg) $pagenav.=" <a href='$url=$nextpg' class='s1'>下页</a> "; else $pagenav.="下页 ";
$pagenav.=" <a href='$url=$lastpg' class='s1'>尾页</a>";

$pagenav.=" 到 <select name='topage' size='1' onchange='window.location="$url="+this.value'> ";
for($i=1;$i<=$lastpg;$i++){
if($i==$page) $pagenav.="<option value='$i' selected>$i</option> ";
else $pagenav.="<option value='$i'>$i</option> ";
}
$pagenav.="</select> 页";
}
?>

//保存的1.php文件数数组形式,如下格式

<?php return array (
  0 =>
  array (
    'username' => 'test',
    'content' => '111cn.net',
    'id' => 1,
    'date' => 1253853347,
    'fid' => 0,
    'ip' => '116.226.2.200',
  ),
  1 =>
  array (
    'username' => 'aaaaaaaaaaad',
    'content' => 'www.111cn.net',
    'id' => 2,
    'date' => 1253857672,
    'fid' => 0,
    'ip' => '116.226.6.188',
  ),
 
)?>

 

标签:[!--infotagslink--]

您可能感兴趣的文章: