如何显示进度?

发布于 2024-11-29 07:34:58 字数 144 浏览 3 评论 0原文

我有一个周期。它持续了相当长的时间,我需要可视化整个过程。怎么办?

while ($csvIterator->next()) {
 //a lot of code...
 visualizeProcess();
 }

I have a cycle. it holds quite a long time, I need to visualize the process work. how to do this?

while ($csvIterator->next()) {
 //a lot of code...
 visualizeProcess();
 }

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

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

发布评论

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

评论(4

Bonjour°[大白 2024-12-06 07:34:58

你的问题确实需要更多细节。如果您需要像 loading bar 这样的详细百分比条形指示器

,那么您需要将循环数据分成几部分(使用迭代计数/迭代总数或读取的字节数等)。

本指南将为您提供必要的信息这样做的知识。

另一方面,如果您只需要一个加载指示器,例如著名的 ajax load gif 那么您要做的就是(让我们假设您有 jquery 并且您的脚本仅处理)是

$("#myButton").click(function(){
    $("#myLoaderGif").show();
    $.get("myPHPScript.php", function(data){
           alert("Data Loaded: " + data);
           $("#myLoaderGif").hide();
    })});

此示例假设您有一个 标记,其中包含加载图像和一些带有 id = 的元素myButton 启动脚本并且您正在使用 get 检索数据

You really need more detail in your question. if you need a detailed percentage bar indicator like loading bar

then you need to divide your loop data into pieces (either using the iteration count/total number of iterations or bytes read etc).

THIS GUIDE will provide you with the necessary knowledge to do that.

If, on the other hand you just need a loading indicator like the famous ajax load gif then all you gatta do (let's assume you have jquery and your script only processes) is

$("#myButton").click(function(){
    $("#myLoaderGif").show();
    $.get("myPHPScript.php", function(data){
           alert("Data Loaded: " + data);
           $("#myLoaderGif").hide();
    })});

This example assumes you have an <img id="myLoaderGif"> tag with your loading image and some element with id = myButton that initiates the script and you're using get to retrieve data

雪花飘飘的天空 2024-12-06 07:34:58

编辑

- 这假设您正在将 PHP 用于基于 Web 的应用程序。

简而言之,没有简单的方法可以做到这一点。在很多领域,您需要巧妙地设计应用程序来处理它(即:如果您正在一块一块地构建页面,那么如果它应该是安全的,会发生什么?您需要实现 SSL 和确保所有 AJAX 调用都已加密)。基本上,您需要做的是让脚本在块中可执行,而不是使用一个循环来独立完成整个操作。例如,有一个 php 文件将一个整数作为查询字符串,然后使用该整数执行不同的操作。一旦整数等于某个数字,您就可以确定您需要做的所有事情都已完成,因此进度达到 100%。您不能让 PHP 将页面一块一块地发送回浏览器,而您似乎打算这样做。您需要使用 AJAX 调用(假设这是一个 Web 应用程序而不是命令行应用程序)。执行如下操作:

  1. 创建一个通过查询字符串接收 int 的 PHP 文件。让传递的整数指示 PHP 脚本执行的操作(即:0 = 获取分配的所有会话变量,1 = 连接到数据库并获取需要解析的数据并将其推送到会话中,2 = 解析 。

  2. 创建一个 PHP 文件,该文件将作为显示进度的基本页面提供给客户端。在此页面中,您将需要使用 Javascript 使用各种查询字符串请求 1 中提到的页面。您发出的 AJAX 调用具有成功和失败处理程序,在这些处理程序中,您将能够更改页面上显示的内容以反映流程的进展情况。

简而言之,由于 Web 应用程序的分布性质,没有简单的方法来为 Web 应用程序制作进度条。我建议您研究 GET 和 POST 请求的工作方式、Web 服务器的功能以及 PHP 处理程序与 Apache 一起工作的方式,以便更好地了解为什么您的初始方法不可行。

EDIT

-This assumes you are using PHP for web-based applications.

In short, there is no easy way to do this. There are a lot of areas in which you would need to design your application smartly to handle it (ie: if you're building a page piece by piece then what happens if its supposed to be secured? You'll need to implement SSL and make sure that all AJAX calls are encrypted). Basically what you need to do is have the script executable in blocks, instead of having a loop that does the operation in its entirety on its own. For instance, have a php file that takes an integer as a query string and then uses that integer to do a different action. Once the integer is equal to a certain number you can be sure that everything you needed to do has been done and therefore progress makes it to 100%. You cannot have PHP send a page piece by piece back to a browser, which is what it seems you are proposing to do. You need to use AJAX calls (assuming this is a web application instead of a command line application). Do something like the following:

  1. Create a PHP file that takes in an int via query string. Have the integer that is passed along dictate the action that the PHP script undertakes (ie: 0 = get all session variables allotted, 1 = connect to the database and get the data you need to parse and push it into session, 2 = parse the data from session and do something).

  2. Create a PHP file that will be served to the client as the base page that will show the progress. From this page you will need to use Javascript to request the page mentioned in 1 with the various query strings. The AJAX calls that you issue have success and failure handlers, and in these you will be able to change what is being displayed on the page to reflect how the process is coming along.

In short there is no easy way to do progress bars for web applications due to the nature of their distribution. I suggest that you look in to the way GET and POST requests work, the functionality of web servers, and the way that the PHP handler works w/ Apache to get a better idea of why your initial approach isn't viable.

分分钟 2024-12-06 07:34:58

在循环中间使用 echovar_dump。另外 implicit_flush 可能会影响详细程度。

Use echo or var_dump in the middle of your loop. Also implicit_flush might affect verbosity.

幸福%小乖 2024-12-06 07:34:58

最好的使用方法是 JavaScript,但您

flush()

也可以使用函数

The best way to use is JavaScript, but you can use

flush()

function, too

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