Multiple result sites (pagenumber buttons) - php

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.

Related

Notification System that loops through first ten database entries

I have looked for days now and cannot seem to find any direct examples of what I am trying to accomplish, that I can reference.
I am trying to create a simple, elegant notification system, that pulls a persons image, name (in text format), and a predefined message (selected from drop down menu), from a database, and then displays the info in an elegant little "profile like" layout, on a webpage or smartphone. The only feature that I want the app to have is an auto refresh setup (using AJAX maybe?) that cycles through the latest ten entries into the database, in a continual loop.
I already have the MySql database set up, as well as the form which supplies the information that I want show, into the database -- but I can't for the life of me figure out how to pull that info into a nice little alert, and get it to cycle through the latest ten database entries.
Thank you so much, in advance, for any assistance you can provide. I'm ok with databases, and Php, but I'm racking my brain trying to figure out how to get it to display and cycle through the first ten entries.
Thanks again!
If you have an id column or some sort of timestamp column, you can use ORDER BY and LIMIT in MySql to extract only the last X recrods.
For example:
SELECT * FROM profiles ORDER BY id DESC LIMIT 10
That will extract the top 10 id's, where in a standard id column, that will be the last 10 records.
As for formating the display - that is way to wide, and there are a lot of ways to do so.
I think you should have PHP file with SQL query SELECT ... ORDER BY id LIMIT 10, and use json_encode to JSON encode the returned array. JSON is easy to use with AJAX with JavaScript.
And about AJAX - I would use jQuery and use jQuery.getJson from PHP file do sleep and loop it
EDIT: On refresh you will do a new JSON parse, and remove last elements of container with cards, and use new elements

Load more results like on facebook/twitter

Maybe you've seen on Facebook/Twitter this effect. I want to load more results and append them to the existent ones. No problem so far. How can I keep track of how many results I have yet, to know from where on to query next time?
I have a number of answers:
Wrap the results into a div with an id, results1, results 2 ...and then get the last id and multiply it by how many results are in a container
Keep the number in a variable, reset it every time the list is requested after you come from another page.
Take a look at this.
It worked fine for me.
http://youhack.me/2010/05/14/an-alternative-to-pagination-facebook-and-twitter-style/
I'm just posting a link rather than an answer, because you can go through the code and will be able to solve the problem by yourself.

Pagination in PHP

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.

sequentially show mysql data using php

I have a php code that fetches data from mysql. The data has (say) 15 rows. I want to display only 5 rows at a time to the user, with links to each of the set (3 in this case) such that when a user clicks on either of the links, the same page will show the corresponding results. Since, php code has the final result set, I don't want a solution that involves me to navigate to other pages and possibly re-calculate the next set of solutions (5~10 or 10~15). How can I do this? Thanks in advance.
If I am using javascript or ajax, how can I achieve this? I don't know javascript much.
You can try loading everything in your page and simulating the pagination thanks to javascript.
An example in jQuery here
It sounds like you want to send the Data as a complete set to the client but not let him display everything to the user. So use Javascript to just show you the pages 1*page_number to 5*page_number (with a for-loop).
By far, the easiest solution in this situation is a Dom-based Grid system. I recommend checking out Datatables. In essence, the Datatables code will take a fully-formatted html table and reformat it to include only the amount of rows you tell it to, with paging on the bottom as you've requested. In addition, you can turn on such features as filtering, toggle-able length, sorting, and selection. Once you get the hang of it, the additional code takes no more than a minute per table and the features are outstanding.
This is really straightforward. Use PHP to output ALL of the table, with all the rows. The only gotcha here is to include the full html, including <thead> and <tbody> Give the table an id such as "example"
Now, include the files that you download from the datatables site--datatables.js and Jquery.js. Instantiate the jquery like this:
$(document).ready(function() {
$('#example').dataTable();
} );
That's it. As you can see from the examples, it's a really cool tool. Good luck.

I need help with adding condition to php live search

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.

Categories