Reputation: 15378
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();
}
Upvotes: 2
Views: 1098
Reputation: 1777
You really need more detail in your question. if you need a detailed percentage bar indicator like
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 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
Upvotes: 1
Reputation: 50966
The best way to use is JavaScript, but you can use
flush()
function, too
Upvotes: 0
Reputation: 5176
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:
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).
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.
Upvotes: 0
Reputation: 26921
Use echo
or var_dump
in the middle of your loop. Also implicit_flush might affect verbosity.
Upvotes: 0