这篇文章主要介绍了关于php导入进度条类,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下
最近在做一个客户导入的功能,整理的一个导入的进度条类:
width = $width;
$this->total = $param['length'];
$this->start_time = time();
ob_end_clean();
}
/**
* 执行进度跟踪
* @param $num 执行的数据点,这个数字应该是0 到 数据的总长度
* @return bool
*/
public function run($num){
$successNum = $num - $this->faileCounter;
$percent = number_format($num / $this->total * 100,2) . '%';
echo "";
print str_pad("",100000);
ob_flush();
return true;
}
/**
* 占用内存跟踪
* @param $i 进度
* @param $progress_mem 占用内存
* @return bool
*/
public function run_mem($i,$progress_mem){
echo "";
print str_pad("",100000);
ob_flush();
return true;
}
/**
* 设置出错的点
* 例如导入excel的时候,有不符合模板规范的行数可以在这里设置
* @param $line 错误行数
* @param $errMes 错误信息
*/
public function setFaileLine($line,$errMes){
$line = intval($line);
if ($line != 0) {
$this->faileCounter++;
$this->faile = true;
$this->faileLines[$this->faileCounter]['line'] = $line;
$this->faileLines[$this->faileCounter]['errMessage'] = $errMes;
echo "";
print str_pad("",100000);
ob_flush();
return true;
}
}
public function getFaileCounter(){
return $this->faileCounter;
}
/**
* 获取出错的点
*
* @return string $html
*/
public function getFaileLine(){
$html = '';
if(!empty($this->faileLines)){
foreach($this->faileLines as $failline){
$html .= '错误位置:'.$failline['line'].', 错误信息:'.$failline['errMessage'].'
';
}
}else{
$html .= '没有错误信息';
}
return $html;
}
/**
* echo出进度条的html页面和js函数
* 应该在实例化类后立刻执行此方法,否则会失败
*
* @return unknown
*/
public function createHtml(){
echo "
width."px\">
此操作需要等待一段时间,在执行完毕之前,请不要关闭此页面
width."px\">
已用时: 时
分
秒
0%
";
return true;
}
}CI框架的调用:
$this->load->library('loading',array('length'=>$totle)); //加载进度条类,这是CI框架
$this->loading->createHtml();
//$start_mem = memory_get_usage();
foreach($customers as $key=>$customer){
$return = $this->insert_import($customer);
if($return != 1){
$this->loading->setFaileLine($key+1,$return); //记录错误
}else{
$this->loading->run($key+1); //成功进度条
}
//$progress_mem = memory_get_usage();
//$this->loading->run_mem($key,$progress_mem-$start_mem); //测试查看内存使用情况的
}以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!
相关推荐:
立即学习“PHP免费学习笔记(深入)”;











