PHP循环中进度显示以及"假死"
理论上程序应该运行完500次然后才显示成功,但是基本只能运行个十来次页面就开始全白(同时网页中载入的标志也消失了),就不再往下继续执行程序了
求助这种问题应该如何解决,谢谢
另外,进度方面不知道应该如何处理。再次感谢
<?PHPfor ($i=1;$i<500;$i++){ file_put_contents("{i}.txt",$i); sleep(3); $s = $i/100; if($s==ceil($s)) { $s = $i/500; echo "进度:{$s}<BR>"; }}echo "OK";?>
回复讨论(解决方案)
3秒×500= 多长时间?
不死才怪
3秒×500= 多长时间?
没有设置运行时间,不死才怪
nnd,csdn给我500还让我多发了一次……
跟运行时候太长有关系,你还第次休息了3秒
本来是一个采集再写入数据库的程序,这里只是想表达大概的意思 代码少 易看
set_time_limit吧
以下是我原程序的主要内容
<?php require_once("config.php"); session_start(); if($_SESSION['admin']!="ok"){ header("Location: login.php"); exit; } $_GET['act']?$act=$_GET['act']:$act="view"; if(!in_array($act,array("save","scan","view"))){ $act="view"; } /* 保存后台设置 */ if($act=="save"){ $data=array( "appOAuthkey"=>$_POST["appOAuthkey"], "user"=>$_POST["user"], "pw"=>$_POST["pw"], "appOAuthID"=>$_POST["appOAuthID"], "accessToken"=>$_POST["accessToken"], "appOAuthkey"=>$_POST["appOAuthkey"], "lirun"=>$_POST["lirun"], "uin"=>$_POST["uin"] ); $db->row_update("config",$data,"id=1"); if($db->get_errno()){ alert_back("设置失败!"); }else{ alert_back("设置成功!"); } } $conf=$db->row_query_one("select * from config where id=1"); $data=$db->row_query("select * from data"); if($act=="scan"){ //$db->row_delete("data",""); $itemState = $_POST['itemState'];//商品状态 $PageSize = GetPageSize($itemState);//商品页数 for ($i=1; $i<=$PageSize; $i++) { $htm = GetItemList($itemState,$i); $xml = simplexml_load_string($htm); $item = $xml->itemList->item; $itemName = $item->itemName; $itemName = iconv('utf-8', 'gb2312', $itemName); $itemState = $item->itemState; $itemCode = $item->itemCode; $itemPrice = $item->itemPrice; $db->row_insert("data",array("ProName"=>$itemName,"item"=>$itemCode,"Price"=>$itemPrice,"itemState"=>$itemState,"add_time"=>$time)); //进度信息 PageSize } alert_back("扫描结束,共计信息数:{$PageSize}!"); }?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>价格及库存管理系统</title><link rel="shortcut icon" href="favicon.ico" /><link href="all.css" rel="stylesheet" type="text/css" /></head><body><div style="float:right;padding:10px;"><a href="login.php?act=logout">注销登录</a></div> <h1 id="拍拍平台-价格及库存管理系统">拍拍平台-价格及库存管理系统</h1> <form action="?act=save" method="post"> <table border="0" style="width:98%;" cellpadding="0" cellspacing="0" > <tr class="tit"><td colspan="2">系统设置</td></tr><tr><td>用户名:</td><td> <input class="input" type="text" name="user" value="<?php echo $conf['user'];?>"></td></tr><tr><td>用户密码:</td><td> <input class="input" type="password" name="pw" value="<?php echo $conf['pw'];?>"></td></tr><tr><td>uin:</td><td> <input class="input" type="text" name="uin" value="<?php echo $conf['uin'];?>"></td></tr><tr><td>appOAuthID:</td><td> <input class="input" type="text" name="appOAuthID" value="<?php echo $conf['appOAuthID'];?>"></td></tr><tr><td>appOAuthkey:</td><td> <input class="input" type="text" name="appOAuthkey" value="<?php echo $conf['appOAuthkey'];?>"></td></tr><tr><td>accessToken:</td><td> <input class="input" type="text" name="accessToken" value="<?php echo $conf['accessToken'];?>"></td></tr><tr><td>利润:</td><td> <input class="input" type="text" name="lirun" value="<?php echo $conf['lirun'];?>"> 单位:分</td></tr><tr><td></td><td><button type="submit">设置</button><button type="reset">重置</button></td></tr></table></form><br/><br /> <table border="0" style="width:98%;" cellpadding="0" cellspacing="0" > <tr class="tit"><td colspan="3">商品管理</td></tr> <form action="?act=scan" method="post"> <tr> <td style="width: 15%;">商品采集更新:</td> <td> <select name="itemState" style="width:210px;"> <?php foreach($itemStates as $k=>$v){ echo '<option value="'.$k.'">'.$v.'</option>'; } ?> </select> </td> <td> <button type="submit">采集更新数据</button> </td> </tr> </form></table><br /><br /> <table border="0" style="width:98%;" cellpadding="0" cellspacing="0" > <tr class="tit"><td>商品信息管理(共有<?php echo count($data);?>条信息)</td></tr><tr><td> <table style="width: 98%;"><tr class="tit1"><td>ID</td><td>商品名称</td><td>Item</td><td>价格</td><td>状态</td><td>添加时间</td><td>更新时间</td><td>操作</td></tr><?php $ii=0;foreach($data as $key=>$val){ echo '<tr><td>'.$val['id'].'</td><td>'.$val['ProName']."</td><td><a href=http://auction1.paipai.com/".$val['item']." target=_blank>".$val['item'].'</a></td><td>'.$val['Price'].'</td><td>'.$status_str[$val['itemState']].'</td><td>'.date("Y-m-d H:i:s",$val['add_time']).'</td><td>'.date("Y-m-d H:i:s",$val['up_time']).'</td><td><a href="?act=del&id='.$val['id'].'">删除</a></td></tr>'; $ii ++; //if ($ii>2) break;}?> </table></td></tr></table><script type="text/javascript">setTimeout("del_ifr()",5000);function del_ifr(){document.getElementById("ifr").innerHTML='';}</script></body></html>
set_time_limit吧
加入了这个 程序能顺利运行结束(时间比较长点)
但是这个进度显示好像得运行结束了才一起显示出来。。。
php是一次计算一次输出,即使像CLI那样边计算边输出,也要不断刷屏才能实现进度条效果
进度在客户端显示,你觉得php会在服务器端计算->客户端显示->服务器端计算->客户端显示->……这样么?
如果你想要这样效果,你就要把循环拆开,客户端收到第一个数据,然后ajax向服务器发出next请求,php再计算下一个,向客户端发结果,客户端再ajax……

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

An official introduction to the non-blocking feature of ReactPHP in-depth interpretation of ReactPHP's non-blocking feature has aroused many developers' questions: "ReactPHPisnon-blockingbydefault...
