PHP进度条?

发布于 2024-08-08 23:52:09 字数 547 浏览 3 评论 0原文

有没有办法在php中显示进度条?我想让用户知道 php 脚本在做什么。

这是我的网站 http://www 的结果示例.bestsellprice.com/search.php?itemnum=0&keyword=harry+potter+book

我有一个静态加载图像,其中有一些文字显示“正在加载...”,当页面完全加载时,我隐藏加载div。我的 php 脚本转到 amazon api、ebay api 和屏幕抓取其他几个网站,而不是静态加载 div,我想使用以下内容更新该加载 div

Loading amazon.com

//progress amazon, when finish update正在加载 div

正在加载 ebay.com

等。

这可以吗?如果是这样,最好的方法是什么?提前感谢您的帮助!

Is there a way to show a progress bar in php? I like to let the user know what the php script is doing.

Here's an result example from my website http://www.bestsellprice.com/search.php?itemnum=0&keyword=harry+potter+book

I have a static loading image with some text that says "Loading...", when the page is fully loaded, I hide the loading div. My php script goes out to the amazon api, ebay api and screen scrapes a couple of other sites and instead of a static loading div I would like to update that loading div with the following

Loading amazon.com

//progress amazon, when finished update loading div

Loading ebay.com

etc. etc.

Is this possible to do? if so whats the best approach? Thanks ahead of time for the help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

月依秋水 2024-08-15 23:52:09

使用 AJAX 询问 PHP 目前正在做什么,并有一个队列系统来定义哪些任务已完成或未完成。这是一个非常基本的示例:

当任务启动时,让它设置以下变量:

$_SESSION['isEbayLoaded'] = false;
$_SESSION['isAmazonLoaded'] = false;

一旦 PHP 加载 eBay,它会将 isEbayLoaded 设置为 true,然后 AJAX 可以简单地询问 PHP 是否已加载 eBay,然后是亚马逊,然后是其他任何内容你需要的。 PHP 所要做的就是检查这些变量是否设置为 true,这意味着任务已完成。

Use AJAX to ask PHP what it's doing at the moment, and have a queue system that defines what tasks are completed or not. Here's a very basic example:

When the task is started, make it set the following variables:

$_SESSION['isEbayLoaded'] = false;
$_SESSION['isAmazonLoaded'] = false;

Once PHP loads eBay, it would set isEbayLoaded to true, and then AJAX could simply ask PHP if ebay was loaded yet, then amazon, then whatever else you needed. All PHP would have to do is check if those variables were set as true, which would mean that the tasks had been completed.

紫瑟鸿黎 2024-08-15 23:52:09

我之前已经用一个非常简单的解决方案完成了这一点:

ob_flush();

所需的只是创建一个具有适当类名的单个 div:

<div class="loading">
    Loading...
</div>

然后,我的 PHP 基本上是:

foreach ($sites as $site) {
   printAndFlush(<<<CSS
     <style>
       div.loading {
         background: url($site.png) no-repeat right center;
       }
     </style>
CSS;

   scrapeSite($site);
}

这将使用背景样式来显示您将要抓取的站点。

如果必须使用标记,则只需每次打印一个新的“Loading”div(带有唯一的 ID),以及将前一个 div 的样式设置为“display: none”的样式。

当然,请记住,某些浏览器在渲染任何内容之前至少需要 1024 字节(大约 4k!)。

I've accomplished this before with a really simple solution:

ob_flush();

All it required was creating a single div with appropriate class names:

<div class="loading">
    Loading...
</div>

Then, my PHP was basically:

foreach ($sites as $site) {
   printAndFlush(<<<CSS
     <style>
       div.loading {
         background: url($site.png) no-repeat right center;
       }
     </style>
CSS;

   scrapeSite($site);
}

This would use a background style to show which site you would be scraping.

If you must use markup, you can simply print out a new "Loading" div each time (with a unique ID), along with a style to set the previous div's style to "display: none".

Of course, remember that some browsers require at least 1024 bytes (somethings 4k!) before rendering anything.

装纯掩盖桑 2024-08-15 23:52:09

最好的方法是让您的 javascript 对服务器进行 ajax 调用并找出它的位置,以便您可以向用户提供一些反馈。

您可以从上传进度条开始,然后继续显示应用程序的进度。

The best approach is to have your javascript make ajax calls to the server and find out where it is at, so you can give some feedback to the user.

You can start with having a progress bar for uploading, and then you just continue to show the progress of the application.

顾挽 2024-08-15 23:52:09

我会对每个站点发出请求,并使用该页面的输出(XML、JSON 等)让其他页面知道发生了什么。

例如...伪代码:

  • 收集
  • 加载ajax页面的循环
  • 站点ajax页面返回带有状态消息的每个请求,例如-“正在加载亚马逊...”

就是这样!

I would make a request for each site, and use the output from that page (XML, JSON, whatever) to let the other page know what's going on.

For example...psuedo code:

  • gather sites
  • for loop that loads ajax page
  • ajax page returns each request with a status message e.g. - "Loading Amazon...."

That's it!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文