首先设置参数,如图:
参数提交的下面的程序执行:($_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 '';
}
$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; //当前进度条长度
?>
执行进度条body, div input { font-family: Tahoma; font-size: 9pt }
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";
$progress += $pix;
?>
updateProgress("共<?php echo $total; ?>条数据需要处理,现在正在操作第<?php echo $j; ?>条数据,ID为:<?php echo $rows['ID']; ?>,请稍候....", <?php echo min($width, intval($progress)); ?>);
ob_flush();
flush(); //将输出发送给客户端浏览器,使其可以立即执行服务器端输出的 JavaScript 程序。
}
echo '你所做的操作完全成功!【返回上页】';
?>
updateProgress("操作完成!", <?php echo $width; ?>);
flush();
?>
进度条效果图: