user520938
user520938

Reputation: 21

Showing a changing php value with Javascript?

I've got a script in php that continually grows an array as it's results are updated. It executes for a very long time on purpose as it needs to filter a few million strings.

As it loops through results it prints out strings and fills up the page until the scroll bar is super tiny. Instead of printing out the strings, I want to just show the number of successful results dynamically as the php script continues. I did echo(count($array)); and found the number at 1,232,907... 1,233,192 ... 1,234,874 and so forth printed out on many lines.

So, how do I display this increasing php variable as a single growing number on my webpage with Javascript?

Upvotes: 2

Views: 122

Answers (5)

Nigel
Nigel

Reputation: 1715

I'd suggest that you have a PHP script that returns the value in JSON format. Then in another php page you can do an AJAX call to the page and fetch the JSON value and display it. Your AJAX call can be programmed to run perhaps every 5 seconds or so depending on how fast your numbers output. Iframe though easier, is a bit outdated.

Upvotes: 0

deceze
deceze

Reputation: 522016

You need to find a way to interface with the process, to get the current state out of it. Your script needs to export the status periodically, e.g. by writing it to a database.

The easiest way is to write the status to a text file every so often and poll this text file periodically using AJAX.

Upvotes: 3

Matthew Flaschen
Matthew Flaschen

Reputation: 284786

You can use the Forever Frame technique. Basically, you have a main page containing an iframe. The iframe loads gradually, intermittently adding an additional script tag. Each script tag modifies the content of the parent page.

There is a complete guide available.

That said, there are many good reasons to consider doing more pre-computation (e.g. in a cron job) to avoid doing the actual work during the request.

Upvotes: 2

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798536

Have your PHP script store that number somewhere, then use AJAX to retrieve it every so often.

Upvotes: 3

Dan G
Dan G

Reputation: 1090

This isn't what you're looking for (I'm as interested in an answer to this..), but a solution that I've found works is to keep track of the count server-side, and only print every 1000/5000/whatever number works best, rather than one-by-one.

Upvotes: 0

Related Questions