I am just learning php and ended up at this tutorial http://www.w3schools.com/php/php_ajax_livesearch.asp
Till now my PHP search file looks the same as in example but i changed the if (strlen($q) > 0) to if (strlen($q) > 3) to display results after typing minimum 3 words. I am wondering how can i make the results paginate if there are lots of them?
Thanks
Roshan
You would need to page after obtaining the number of records from your database.
Paging should be done by the same script that echos your ajax response.
Here is how to page:
PHP MySQL paging.
The idea of search suggestions is to show the user what do you have in your site, in terms of relevance, that match his query. This being said, you should only throw a subset of results, being the first result the most relevant, instead of the whole file or database. In others words, you don't need pagination.
Show the user 5 or 10 items, if he doesn't find what he's after he'll keep writing until he finds it. If he doesn't finds it he will just search it. Remember that it is a suggestion and nothing more.
Related
I need to search details. I have written the code for searching ie, the textbox and search button is written in the homepage as well as the "search_result.php".
Pagination without searching is working well. the searching also is working without pagination.
So I guess if the search value from the homepage is stored in the session then may be I'll get that with pagination.
If there is something more. please help me.. :(
You haven't share code so am in dark what your actually doing or whats wrong.
But there is alternative.
I guess you should try: https://datatables.net/
You does not have to worry about the search results and pagination. datatables takes care of it.
Although datatable in this example fetch all records at once, that may affect on performance if your working with large no of records,
in that case go for : https://datatables.net/examples/data_sources/ajax.html
Let me know if it works for you.
I have used ng-table for my table which works well for pagination, sorting. and I have used ng-model for filtering table data (in short search).
My problem is when I do search it is searching but suppose the word I am searching is at page 3 so it not shows anything till I do not do show all records (till I do not remove pagination). I want to remove pagination automatically while search or anything so that the record will come on first page.
Help me, Thanks in advance.
I think you are currently using pagination in client side data.the better approach is to use the searching in server side return result using limit and offset.
I have a website were the user types in a city (f.e.: Washington), presses search and via ajax json it gets the entries from my database. I never know how many entries it got, so let's say this time I have 40 entries in my database where it says "Washington". Now a function appends all 40 entries on the site.
How do I get it to show only 4 entries and then add number buttons where the user can go to page "X" to see another 4 entries and so on?
second question:
And how do I tell the script to just add a specific amount of number buttons ( In this case just 10, since 10*4=40 already )?
I don't necessarily need a full code example, just a good explanation on how I can do this (without a plugin).
Here a simple drawing to clarify this:
Thank you very much in advance for any answer.
There is an excellent tutorial on tutsplus covering the kind of pagination as seen on your image. You would just need to modify the sql query in the script to match your requirements, point your ajax url to this pagination script, and ensure the results are returned as json. Hope this helps!
To setup the pagination you would need to use mysql LIMIT & OFFSET, also counting the total number of results and returning that in the JSON to set the correct number of pages in the pagination.
You would also need to pass the page number in the ajax call through a GET parameter so you can set the OFFSET correctly and return the correct page.
I tried some jQuery pagination plugins but they were a little buggy. So I chose pure PHP and made a really simple one myself, since the tutorials seemed a bit too overloaded to me.
I just pass pagenr via GET and in my script a set a specific amour of results to be displayed via Limit $start(=0 if pagenr=1), 10
If the pagenr is bigger then "1" it undergoes a for loop which adds up "10" to "$start" every time.
And with these values and COUNT(*) I do the pagination with for and if loops.
I don't know if I'm anywhere close to getting this to work, but I've been working on it for 3 hours now and though I've made progress, it's not working completely. I'm not that versed in PHP, so this has been a battle. Basically what I'm trying to do is this:
I'm using the Formidable Pro to capture two numbers from a user about a goal. The ID of the beginning number is 104. The ID of the goal in the form is 107.
Then on a separate page I'm using the http://wordpress.org/extend/plugins/progress-bar/ plugin to measure progress on the goal.
I need to perform calculations on the beginning number and the goal number and have the result inputted into the progress-bar plugin.
I can't even get a number to show up in the progress bar, so I don't know what else to do.
This is what I have:
[wppb option=red percent=inside progress="<?php echo do_shortcode('[frm-stats id=\"107\"]'); ?>"]
I figured that if I could at least get a number to show up in the progress bar then I could figure out the calculation part of the code...
Any thoughts?
There is no way to nest shortcodes right now. Maybe in the future.
I have a page that serves a list of files for users to download.
There can be hundreds of files there, and I would like to break them down into multiple pages and give users the option either to see them page by page or get them all displayed in one page.
I've never done anything like this and don't know how to do it.
Please help.
Thank you!
Ok, I had this question too once. This is basically the logic behind the pagination, I am not going to write code but if you need it let us know.
Basically, you need two values to create a pagination, a limit and a offset.
The limit is the amount of items your are displaying at the same time.
The offset is from where you started your query.
So, let's say you have 5 items in each page and 25 items total.
In your query, you have to limit 5,0 (the amount of items and the position the query will start).
Now, if you divide 5(limit)/25(total) and you'll get 5 (amount of pages).
Now in page 0 (the start) you can get the offset by multiplying the page number with the limit, so 0 (page) * 5 (limit) gives you 0 (in the first page you start from the offset 0).
Now in the 3rd page, you multiply 3(page) * 5 (limit) it gives you 15, which means in page 3 (or four if you take into account that you actually started at page 0) you will display from offset 16 to 20.
Finally in page 4 (which to your users will be page 5 because they started at page 1, not page 0) you will display from offset 21 to 25 which are all the items in your query.
I hope after reading this you understand the logic behind pagination, if you need help with the code, again, just let us know.
Are your filenames coming from a database? If so, check out OFFSET and LIMIT filters in your queries. If not, try counting up the number of files you have, divide them out into blocks, and then when you go to the next page let it know where to start.
Here's a hint,
print "<a href='page.php?offset=$offest&limit=$limit'> Next </a>"
It is better for you to try and write this on your own, pagination is a basic feature in PHP applications.
For someone that has never done anything like this it doesn't hurt to learn.
In order to do this you are going to need to pass values through the url and have your sql script change based on the limit value passed in the url.
Things to consider:
SQL limit - This is the amount of results you will receive.
Page number - This is the page of results that are returned.
Sort - This is how the results are going to be returned in the pagination.
page links - The list of pages. This is found by taking your result number, dividing by the limit and flooring the number.
The hard part about creating pagination is making it extendible and able to be used with other lists of information such as comments or user lists.
Maybe you try write one? This is very easy thing to write and you will not linked to someone code who can be writed in not good quality or some other else code or framework who can by deprecated in some time.
I know this is not solution for this question but this is a my first idea reading this topic.
Firstly you could try to write your own.
Alternatively you could use Smarty (a very well known PHP presentation framework) along with this pagination plugin.