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来发送给浏览器。
*/
首页文件 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 require 'lib.inc.php'; // LoadProducts() LoadProducts(); // Load products in $master_products_list // Make $curr_product global // Loop through all the products and pull up the product
foreach ($master_products_list as $prod_id => $product) {
if ($_POST[ordered]) { // If they have chosen the product array_push($_SESSION[cart][products], array(trim($_POST[id]), $_POST[quantity])); <html> <a href="cart.php">返回</a> 商品列表页面. <form action="<?php echo $PHP_SELF; ?>" method="post"> <input type="submit" value="添加至购物栏"> |
查看购物车的商品
代码如下 | 复制代码 |
<?php require 'lib.inc.php';
<html> <body> <h1>欢迎进入网上商店</h1> <?php $total += $quantity * <h2>商店待出售的商品</h2> ?> |
修改购物车的数量
代码如下 | 复制代码 |
<?php // Typecast to int, making sure we access the // Save the old number of products for display if ($_POST[quantity]) { // Update the number of items <html> |
功能页面,用户把购物车里面的内容保存到txt数据库
代码如下 | 复制代码 |
<?php
$fp = @fopen($filename, "r") //读取文件内容 @fclose($fp) //关闭文件 |
很简单,我们只用了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',
),
)?>