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.
Related
I have a PHP page which displays n number of records in form of table from database(till here pagination works fine). Using a search field i am triggering another query, which displays the results in 2 pages. The first page shows the result fine, but when i click on the 2nd page link, it displays the result from the initial query.
any suggestions would help me to identify the actual issue that is causing this.
Thanks!
As a was stuck with same issue earlier.
The solution is to pass searching input value along pagination parameters.
In your case pagination just take it's parameter and performs its working by excluding searching query.
So just passing searching input value(Query) you will get proper result.
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.
I'm creating a website that shows 20 search results from the searched query, by taking the results from the database. If, in the address bar, the user has $n=(number) the number of results would be that number. So I thought of creating a button that resends the request but with $n=$n+10 , but that would refresh the page and I want a way to keep the user on the page and show 10 more results quickly and easily. Any ideas?
There are only two ways. Regular way that refreshes the page and ajax. If you are new to Ajax, I suggest you to use jQuery functions. It makes everything much easier to load that extra data and display them without refreshing the page.
http://api.jquery.com/category/ajax/
I suggest you to start with jQuery.post() function since you are new to this.
Hey guys I am really messing up with this.
This is I'm doing: If there are 100 users to fetch from Database, I'm using pagination and showing 10 users at a time. when user will click on next page, he will get next 10 users through ajax(called ajax on click) and so on. I'm showing 10 page-links right now with first, next last and previous links.
This is how flow will go: On a.php created links and called ajax function with every link, passing url(b.php) & target(where I will get result), with url also passing clicked pageno., this pageno. will go to b.php and next 10 users will be shown with the help of ajax.
This is the problem: Currently I'm showing 1-10 links with first and last links, unable to show next and previous links because to redirect to next or previous, I am not getting the current page number on a.php i.e i'm passing to b.php. also links are created in foreach loop.
I am trying hard to get this done, but no success yet.
waiting for valuable reply.
Ok, I think I understand...
I think what you need to do is store the current page number as a JavaScript variable. Use the function that is doing the Ajax to update this variable and also update the next/previous links.
Hope this helps.
Update:
On second thoughts, here's a better idea. Having your ajax call directly in your link means you have to put together a url and update it, which is awkward and annoying. This isn't ideal. Instead it is better to store the variables and have some set functions work with them in a set way. So, you could have something like:
<a href="javascript:void(0)" onclick="gotoNextPage()">
Rather than passing them the number of the page to go to, you can store the current page number in a javascript variable and do something like this:
gotoNextPage(){
// lets assume the current page number is stored in a
// variable defined outside of this function called curr_page
curr_page++; // increment the current
call_ajax('user_info.php?pageno='+curr_page);
}
This way the code for the link doesn't ever change.
You can still pass other variables (such as 'order', 'perpage' etc) to gotoNextPage() but you might find you don't need to.
A couple of things to bear in mind:
1. Using jQuery or something similar would probably make things easier for you.
2. Anyone without javascript enabled will not be able to use your site. Consider changing it to something like
<?php
echo '<a href="your_main_page.php?page_number=', ($curr_page + 1), '" onclick="gotoNextPage()">';
?>
This way it would work for via the ajax method but would also work (if not as smoothly and with a page reload) for people without javascript. It's a bit more work for you though...so hat's your choice!
Hope this makes sense. It's been a long day!
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.