I need to load a lot of data fetched from a mysql db in my webGL page. The approach I found is this: http://www.w3schools.com/php/php_ajax_database.asp
although the idea of a first fetching with PHP and then passing it to the webGL page isn't exciting, any alternative?
It is not very clear what you are trying to do, but if what you want is to display data to the user and you have too much data, than you should use paging/pagination.
This is the process roughly:
You need to read a portion of the data (lets say 100 records) and display it to the user in page one.
you have buttons that enable the user to move to other page.
on click, you send an ajax request with the pressed number and you know that you need pressed number * 100 to ((pressed number *100) + 100)
for example:
first page displays rows 0-100, user clicks next you get rows 100-200 and display them.
first page displays rows 0-100, user clicks number 4, you get rows 400 - 500 (4*100 to ((4*100) + 100)).
Related
I have 1000 images in a database. On page load I am randomly showing 60 images, when user scrolls I add 20 images through an AJAX request.
First method which I did was load all images into a container and then hide all and show 60 first images and on scroll start showing every 20 more. But the problem is that page loads all display none images as well (all 1k images).
Second method Load randomly 60 images from DB. Then on scroll Load extra 20 images:
var id_array=[];
$('.items').each(function(index, element) {
id_array.push($(this).data('id'));
});
$.post("request.php", {
send: "true",
json:JSON.stringify(id_array),
}, function(response) {
add_content(response);
});
I am running through all images to get their id, then am sending json to php to do an SQL query:
SELECT * FROM table WHERE id NOT IN (json, ids, right,here) ORDER BY rand() LIMIT 20
The problem is if there are 1000 images on screen and adding more, then
WHERE id NOT IN (1000, items)
will be very slow, right?
How I could improve this performance as it sometimes laggy and obviously not optimized?
I don't know if this is a best practice, but I thought about just fetching the image paths into an array, using shuffle() on it and storing it in a session variable.
You could then use array_slice() to return parts of it for each subsequent request.
If you are showing 20 images on page loads then pull first 20 records on page load. And then display it on you page. After that when user scroll down and reached bottom of the page, you can again do a ajax call for next 20 records and so on. And when user reached a point when there is no more records present do not call ajax any more.
This way you can optimize your codes and pulling 20 records will be faster then pulling 60 records.
Hope this idea will helps you to make your page loads faster in a better way.
I've got a table listing all users with their basic information, the last cell of the row is a 'view more', which should populate a second table with detailed information about the user. (see it as 2 panels, left and right, and right one (view more) only displays when view more is clicked).
All the data is stored on the database, I've already succesfully managed to build the first table. But the problem is how to handle the second one, I've build it, and depending a variable it loads content from one user or another, but I don't know how dynamically change that value and load the next user data on the fly. Thanks in advance.
You can't load data on the fly in php. the simple reason is because the http protocol doesn't work like that.
You basically have 3 diffirent options:
1. reload the page with another link
2. Use ajax to load the data async
3. Load all the data on the start and hide missing data on page load, and show it when you click more info
I am using php, html, css to create a bingo game (There will be nxn boxes. It will be filled in random order for 2 players. Players has to select numbers one by one. One who fills up rows or columns or diagonals five times wins the game).
I created player1.php and player2.php. I am having 9 boxes and populating it from databases. If user clicks a button I'll update the corresponding values as 0 so that when page loads I'll disable the button if the text is 0.
If player1 clicks a button in player1.php, it should reflect in player2.php. How can I do that? Is that possible by reloading page every second? Is it good to do that?
As short answer: no it is not.
Pure PHP is not the most suited for interactive game like that but there are few things you can do.
You could use javascript with Ajax to do this. Let's say you have X players (each using playerX.php although i would put it into player.php and determine each player by GET parameter or something). When one of the player clicks a link, it goes a server-side script, in PHP (possibly using Ajax if you want), that updates your database.
Now the tricky, each player page 'asks' the server every certain amount of time for the state of the game, and updates the page accordingly. The easiest way to do this is create a script (lets call it state.php) that will output data from your database using JSON. You can read the request using ajax and update your page using javascript.
I suggest you read about jQuery and Ajax (which can be also used with jQuery).
If you want to avoid having to refresh the page each time, learning javascript and ajax is your best bet.
I've asked this in a different post but finally realized what it is I'm actually trying to ask.
Is there anything simple that will allow me to load 5 rows from a MySQL table and then with an AJAX driven button, show 5 more, etc, etc (sorta like Facebook does with their 'View Older Posts' button (I am using PHP).
You can retrieve all of your data and store in DIVs, placing 5 records in every div and separating divs by id and then use javascript to show div on each click.
Create a JS var that stores your current record status.
Every time you load the 5 records, pass this value through ajax to your script to use in the MySQL "LIMIT $start, 5 "portion. Everytime you load, increment the variable by 5 and pass it back to your PHP page. It'll grab the next five, limiting 5 records.
I have an SQL query that returns an amount of tuples (about 50).
Now I need to display the results, 15 tuples at a time, then I will have a "view more" button to view the next 15 results.
Can you please help me how I can make this? The issue is that I cannot use the 'limits' because each time I run the query the results will be different, hence when pressing view more, I may get the same results of the same page.
thanks
If you can't use LIMIT, this means your script will have to fetch and load the 50 tuples. If you only want to display 15, you should look for a Javascript solution to hide the others and only show the active ones.
The JQuery Datatables is an EXCELLENT piece of work. You just load all the tuples in a table, and call the Datatable - that's it! You can later customize it to show more or less than 15 at a time.
Instead of reloading, you should put each "page" into a display:hidden div and using JS you show one div after another.
If you can only run the query once, you only have two options:
Send the entire resultset to the browser, hiding all but 15 rows. "View more" would simply unhide more rows.
Run the query once and cache the results, but only send back the rows requested to the browser. When more rows are requested from the browser, send back another set of cached rows. This is useful for progressively loading large or complex data sets.