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
Related
I have an array of data in PHP, being displayed in 25 table cells on a webpage. I also have a javascript function that determines when the user has scrolled to the bottom of the page. My question is, what are the steps to fetching another 25 elements or so from the PHP array and displaying 25 more?
I am aware that Javascript is a client side language whereas PHP is a server side language, but I'm not sure how that affects me...Thanks
Best...SL
Without seeing the code it's difficult, but I'll take a stab at it:
Use jQuery to hit the PHP once the user has scrolled to the bottom. Parse the data returned from the PHP and refresh your table.
So im doing a project and i have made a webpage, on this page i have a total value which is a float inside of a div called total. What i want to do is save this value to a database on my webspace (im hosted by 123 reg) so each user saves the total that they get and then the total in the database in the website is constantly updated and displayed again on the website as a "Global total".
I have no idea about how i would go about doing this any takers?
Ok so basically you might want to take a look at Javascript, especially how to submit a form using javascript:
http://www.javascript-coder.com/javascript-form/javascript-form-submit.phtml
Then you want to take a look on how to process this data in PHP.
The data should come in within the $_GET or $_POST variables.
And you might want to learn about SQL INSERT statements.
http://www.ntchosting.com/mysql/insert-data-into-table.html (somewhere around the bottom)
And then you might want to learn about using the MySQL SELECT-Statement. With that you can use a summation operation to get the sum of all totals and then using PHP echo that into the page again.
Or using AJAX you could load it into the page in a somewhat "live" action.
I'm making a call to a PHP script that returns a random element from an array using array_rand(). It echos out some text that I'm ajaxing into another page. I'd like to have it echo out each successive element each time I load the page. So basically, I'm trying to figure out a way to echo out the first element when I load the page once, then the second element when I refresh, and so on. The thought maybe there would be a way to do it with array_pop(), but it removes each element, and I want to keep the array intact.
I think next is what you're looking for. It returns the next element in an array without removing it. However, if you must do that in between page refreshes, you'll have to keep track of which element you're at yourself, because the array pointers will be lost on page refresh. Using a $_SESSION variasble could do the trick.
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.
How can I get the values from a html table and pass that to a controller in codeigniter?
I'm passing an array to a view. I walk that array and display its content in a table where the user can alter the table. For example, adding a row, or deleting one, but changing values is a possibility too. But then the user saves the data by pressing the button 'save'.
How do i get the data from the table and pass that to php?
When the "save" button is pressed you trigger a javascript function that uses the DOM to get at the values you want. You "select" a table cell and then use innerhtml to get the string inside it. Using unique html id's on your cells will make this easy. Collect all this data in an array and "send" it to your PHP via an Ajax POST request.
That's one way of going about it. Another way is to use Simplehtmldom where you use PHP instead of JS to get your values. This may be easier / more difficult depending on how good your JS is, but the methods are the same. Simplehtmldom uses a syntax that's quite similar to jquery's and in this case you put the load on the server instead of the client.