Displaying SQL results using PHP - php

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.

Related

Best practice ajax loading more images(content) randomly

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.

Best way to show number of results in a filter using Ajax

First, a quick explanation of my current situation:
My webpage contains a list of (about 40, categorized) filters and a list of 400+ products.
I get the list of products using Ajax. By default 20 products are shown, but when you scroll to the bottom more products are added. Users can filter the results by clicking the desired option. It's like a simplified ebay. Only difference is i don't reload the page when i click on a filter, but instead i use Ajax to get my products.
Now, ebay has these numbers next to every filter option, which shows the amount of results you'd get if you click that option. My question is, What is the best way to show these numbers?
My approach would be to run a sql query (SELECT COUNT(id) FROM sometable WHERE filter='something') for every filter option. However that means i have to run about 40 queries on load, and every time the user clicks an option (using Ajax). This seems like heavy server load, or am i wrong?
I'm going with my first approach, but now i'm using UNION queries to get all counts. This way i only have to run 1 query instead of 1 query for every filter.

PHP JQUERY AJAX PAGINATION

I'm just looking for some advice in this post.
I am currently using pagination via a $_GET variable in the url. However I am not paginating by first calculating a count and then using a LIMIT clause. Instead I am retrieving all values to paginate, then placing them in an array which is then sliced, so the first 10 items of the array are displayed on the first page, and the next 10 on the second page, etc. There is a reason for this madness.
However, this results in the entire query running every time a user clicks a different page number.
Is it at all possible to instead use jQuery/AJAX, so that I can run the query once, then simply cycle through the array without having to reload the page?
Any help would be greatly appreciated.
Thanks
Yeah if you have to get everything out in your query anyway you might as well divide everything up into pages in one go and put them on your page ready to be hidden/shown. Then your page links/buttons will just need to hide the current page and show the page clicked. Shouldn't be too much work.

Lazy load from database as user scrolls down page (similiar to Twitter and Facebook)

I have a coupon code site. On some category pages I'm limiting it to display only 50 coupons as there are potentially 1000's of coupons for each category. I don't display them all as it would be hard on the server, and the browser will hang. Is it possible to lazy load more coupons as the user keeps scrolling down?
The only lazy load plugins I've found are for images. Anything out there that will work with PHP and loading data from a MySQL db?
Any help is appreciated. Thanks!
You will definitely need javascript or jquery for this. Just use an onScroll event when the scroll-bar is for example at 75% call a php-file using AJAX, pass 2 parameters for the query. Using the SQL Limit you need to set the start-indicator and the number of rows you want to collect: SELECT * FROM tbl_coupons LIMIT 50,20
You can use let the php output the html you want to use and insert the html straight into the Dom or if you want to parse the data using JS you can use *json_encode* to create a json-object from an array of rows.
do you mean like this one http://www.webresourcesdepot.com/dnspinger/
Personally I would do it with ajax, as soon as the browser detects a scroll it start to load the next coupons from the db, once they are loaded it will display them. I'm not aware of a script that would do this automatically, but it's not difficult to code with jquery.
Another idea would be to cache the coupons page, assuming they don't change every second it could be a feasible solution.

jquery ajax pagination with php question

First off I would like to say that I have done ample research on jquery pagination tutorials and scripts and the ones I have tried had worked, but just not the way I wanted them to.
What I am looking for is a jquery+php pagination that has 5 buttons. One for the first page, last page, next page, previous page, and manually enter a page to go to (which would also display the current page). I am new to pagination and have no idea how to integrate or alter current scripts, but I'm willing to learn as long as I can have the 5 buttons stated above and use php mysql queries. Thanks for the help!
UPDATE: I can do this with only php (would have to reload the page) but I do not know how to do this with jquery
UPDATE2: I figured out how to have the user manually input page number, but I still do not know how to do the other for buttons. For the next and previous buttons, I think I need to return the current page, but I have no idea how to do this :/
The trick is you use limit and offset parameters in your mysql query. Depending on what page number of the result you are serving, you can modify the ordered mysql query to fetch the result from that offset. It's pretty simple to work with that after you understand how to paginate in mysql queries.
In order to accomplish this you simply need to enable your application to make an ajax call to fetch the 'more info' results. That ajax call should take two parameters, an OFFSET and a LIMIT. Simply store these values locally in the application (via hidden elements, JS cookies, etc) to reference. Next time the 'more info' button is pressed you increment the OFFSET by the LIMIT and repeat.
NOTE: It's important that your query that is fetching the results is also using an ORDER BY. If you are relying on the server to order the results the same automatically you could potentially repeat results if the data is being updated often.

Categories