首先设置参数,如图:
参数提交的下面的程序执行:($_GET['start'],$_GET['end'],$_GET['submit'])
这里的进度条是用在网站中商品生成静态页面的功能。整个代码如下
<?
/*
* ===========================================*
* Copyright (c) 2010 - 2011*
* 主页: http://tdd.pzhl.net*
* 信箱: taohuadong1237@126.com*
* 作者: Tao Dongdong
* 最后更新:2011-01-05
* ===========================================*
*/
include_once 'config.php';
if( $_GET['submit'] != '生成静态' )
{
echo '<script>alert("权限错误");history.back();</script>';
}
$start = trim($_GET['start']);
if( $_GET['end'] )
{
$where_str = 'WHERE ID >= \''.$start.'\' AND ID <= \''.$end.'\'';
}else{
$where_str = 'WHERE ID >= \''.$start.'\' ';
}
$sql1 = ' SELECT COUNT(*) FROM PRODUCTS '.$where_str;
$query1 = exequery( $sql1 );
if( $rrs = fetch_array( $query1 ) )
{
$TOTAL = $rrs[0];//生成静态页总数
}
set_time_limit(0);
$width = 500; //显示的进度条长度,单位 px
$total = $TOTAL;
$pix = $width / $total; //每条记录的操作所占的进度条单位长度
$progress = 0; //当前进度条长度
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/transitional.dtd">
<html>
<head>
<title>执行进度条</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<style>
body, div input { font-family: Tahoma; font-size: 9pt }
</style>
<script language="JavaScript">
<!--
function updateProgress(sMsg, iWidth)
{
document.getElementById("status").innerHTML = sMsg;
document.getElementById("progress").style.width = iWidth + "px";
document.getElementById("percent").innerHTML = parseInt(iWidth / <?php echo $width; ?> * 100) + "%";
}
//-->
</script>
</head>
<body>
<div style="margin: 4px; padding: 8px; border: 1px solid gray; background: #EAEAEA; width: <?php echo $width+8; ?>px">
<div><font color="gray">执行进度</font></div>
<div style="padding: 0; background-color: white; border: 1px solid navy; width: <?php echo $width; ?>px">
<div id="progress" style="padding: 0; background-color: #FFCC66; border: 0; width: 0px; text-align: center; height: 16px"></div>
</div>
<div id="status"> </div>
<div id="percent" style="position: relative; top: -30px; text-align: center; font-weight: bold; font-size: 8pt">0%</div>
</div>
<?php
ob_flush();
flush(); //将输出发送给客户端浏览器
$j = 0;
$sql = ' SELECT * FROM PRODUCTS '.$where_str.' ORDER BY ID DESC ';
$query = exequery( $sql );
while( $rows = fetch_array( $query ) )
{
$url = "http://tdd.pzhl.net/product_view.php?id=".$rows['ID'];
$content = file_get_contents($url);
$name = $rows['NAME'];
$new_name = $name.".html";
file_put_contents($new_name, $content);//以商品名称为文件名创建文件
$j++;
echo $name.".html</br>";
$progress += $pix;
?>
<script language="JavaScript">
updateProgress("共<?php echo $total; ?>条数据需要处理,现在正在操作第<?php echo $j; ?>条数据,ID为:<?php echo $rows['ID']; ?>,请稍候....", <?php echo min($width, intval($progress)); ?>);
</script>
<?
ob_flush();
flush(); //将输出发送给客户端浏览器,使其可以立即执行服务器端输出的 JavaScript 程序。
}
echo '你所做的操作完全成功!<br><a href="javascript:history.go(-1)">【返回上页】</a>';
?>
<script language="JavaScript">
updateProgress("操作完成!", <?php echo $width; ?>);
</script>
<?php
flush();
?>
</body>
</html>
进度条效果图: