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?
Have your PHP script store that number somewhere, then use AJAX to retrieve it every so often.
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.
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.
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.
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.
Related
Alright, so I have a PHP app that, in essence, fills up an array with references to elements in an XML file, does a shuffle() on the array to randomise it, then cycles through the array, displaying the data from the array (and ergo the XML file) on the screen.
My current code works fine - It fills the array, shuffles it, and displays the 0th index (which -is- random). My problem lies in the fact that I want to be able to reload the content on the page dynamically, without losing the data in the arrays, but not link to a different page. More specifically, I want to be able to cycle through the array on the click of a button (without totally reloading the page, losing the valuable data in the array).
I tried using some hidden form fields to load the values of the array into a temporary array, then feed them back in as the page reloads, but to no avail. I find a lot of bugs this way, and it's quite clearly a quick-n-dirty way of doing it.
Essentially, I want the code to do something like this:
$heaps_array = array(...); // Populated by, let's say 3 strings, for argument's sake
shuffle($heaps_array);
echo $heaps_array[0];
// User clicks the Next button...
// Get rid of the $heaps_array[0] from the page content, and...
echo $heaps_array[1];
// User clicks the Next button...
// Get rid of the $heaps_array[1] from the page content, and...
echo $heaps_array[2];
// User clicks the Next button...
...
PHP has a / is request based interpreter language so you need to run a script every time you need something from server. You can't have those arrays always in memory without using some storage engine (memcached, MySQL, you name it).
As others have said, you can send the array to the frontend and manipulate it in JavaScript. That way you won't load the server and have all data in memory.
Cant you send the array to the client side (the dirty way, not ajax) ?
Then display values with js.
i mean:
echo 'var array = new Array('.implode(',', $array).');';
Otherwise you will need more sofisticated method, the best is to build a mini webservice and get values through an AJAX query.
You have a few options here, but you need to understand that PHP is a server side language that runs and finishes before the browser even sees the page content. To do dynamic content, thats where javascript and ajax come into play. I recommend a javascript framework such as jQuery that makes AJAX calls simple.
Some ways to do it:
1) Output the entire php array into a javascript variable on page. Do all of the array sorting using javascript. No AJAX.
2) Use php to sort the array, store it in a session variable. Use AJAX to request new data from the array in session memory, resort the array if necessary and return the value you need.
3) Use an AJAX call to request and return the entire sorted PHP array. Use javascript to do with it as you wish.
Depending on the size of the data you wish to return, you may want to minimise amount of data request through AJAX and the client side processing, and just use PHP to do all of the array stuff then return the exact values you need.
Just throwing PHP's session management out there since no one has. http://php.net/session_start
I am using PHP and AJAX requests to get the output of a program that is always running and print it on a webpage at 5 second intervals. Sometimes this log file can get up to 2mb in size. It doesn't seem practical to me for the AJAX request to fetch the whole contents of this file every 5 seconds if the user has already gotten the full contents at least once. The request just needs to get whatever contents the user hasn't gotten in a previous request.
Problem is, I have no clue on where to begin to find what contents the user hasn't received. Any hints, tips, or suggestions?
Edit: The output from the program starts off with a time (HH:MM:SS AM/PM), everything after has no pattern. The log file may span over days, so there might not be just one "02:00:00 PM" in the file, for example. I didn't write the program that is being logged, so there isn't a way for me to modify the format in which it prints it's output.
I think using a head request might get you started along the right path.
check this thread:
HTTP HEAD Request in Javascript/Ajax?
if you're using jquery, it's a simple type change in the ajax call:
$.ajax({url: "some url", type: "HEAD".....});
personally, I would check the file size & date modified against the previous response, and fetch the new data if it has been updated. I'm not sure if you can fetch only parts of a file via ajax, but I'm sure this can be accomplished via php pretty easily, possibly this thread may help:
How to read only 5 last line of the text file in PHP?
It depends how your program is made and how does it print your data, but you can use timestamps to reduce the amount of data. If you have some kind of IDs, you should probably use them insteam of timestamps.
I am using PHP and JavaScript in my application.
I want to delete entries from the database, a process which requires a significant amount of time, thus I would like to report the progress to the end-user.
I'd like to know how to achieve this, could anyone explain the theory to me?
You need 2 scripts:
First for delete
Second for status
First make a request to the first script, and then make a second request to the second script that will report you a progress.
Then you can use any of the jQuery plugin to display the progress bar.
I want to create a very simple site, but unfortunately my php skills are weak. Basically, when a user shows up, I want to have a page with text and a blinking cursor (I can probably figure the cursor part out myself, but feel free to suggest). When a user types, I want it to show the text as they type, and when they hit enter (or click something/whatever), the text just typed will be sent to a database and then the page will update with that new text, for anybody else to see. The cursor will then be blinking on the next line down. So basically it's like a really simple wiki, where anyone can add anything, but nobody can ever remove what has been typed before. No logging in or anything. Can someone suggest the best way to go about this? I assume it will require a php call to the database to display the initial page, then another php request to send data, then another php request to display the new page. I just don't know the details. Thanks so much!
Bonus question 1: How can the page be updated dynamically, so if A sends text while B is typing, B sees the text A sent on B's page immediately?
Bonus question 2: What sorts of issues might arise if this database grows extremely large (say, millions of words), and how might I address these up front? If necessary, I could show only a small chunk of the (text-only) database on any given page, then have pagination.
If you only have one page, you don't need a database. All you need to do is save a text file on the server (use fopen() and related functions) that only gets appended to. If you have multiple pages, then a simple id (INTEGER), filetext (LARGEBLOB). (Note largeblob has a limit of 2^32 bytes).
For the user's browser part, you'll need to use Javascript and AJAX to inform the server of any updates. Just get in touch with a PHP script that (1) accepts the input and (2) appends it to a file.
Bonus question 1: How can the page be updated dynamically, so if A sends text while B is typing, B sees the text A sent on B's page immediately?
Also use the AJAX call to fetch new content (e.g. if you assign line numbers, then the browser just tells the script the last line it read, and the script returns all new lines past that point).
I assume it will require a php call to the database to display the initial page, then another php request to send data, then another php request to display the new page.
Pretty much. But only send the last 50 lines or so of the file when the browser visits it. You don't want to crash the browser.
Bonus question 2: What sorts of issues might arise if this database grows extremely large (say, millions of words), and how might I address these up front? If necessary, I could show only a small chunk of the (text-only) database on any given page, then have pagination.
Think in terms of bytes, not words, and you'll likely run into performance issues. You could cap file sizes or split up the storage into multiple files at a certain size so you don't have to scan pass content that will rarely be fetched.
I have some code that utilizes simplexml to retrieve some data.
I perform about 4 functions that each use simplexml, per entry in a database. So if i have 4 entries in the database, im running simplexml 16 times to load that content.
Problem is that it takes about a quarter of a second or so to load each item, so as the page loads, it trickles in and takes a second or two to load the entire page.
Is there anyway to easily speed this up, or cash this, or some better way of watching my page expand with content each time it loads?
Well, you only need to parse the XML once, and pass the parsed object to each of your functions.