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.
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.
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.
I'm new to programming and have a question about navigation.
What are the various methods that can achieve a "First", "Previous", "Next" and "Last" navigation, as seen in article or comic based sites? (Admittedly the "First" link isn't confusing as it can stay static, but what about the others?)
For example, on the 50th page, the links would appropriately lead to the 49th and 51st (if it exists, if not it would not function but would automatically become active when such a page exists.
In most examples of this I see urls ending with something like ".php?id=50" but am not certain how it's achieved, with a database?
Any help will be very much appreciated, thanks.
The bit at the end of the URL is called a GET variable. When a user goes to a page with one at the end, for example:
example.org/comics/myawesomecomic.php?page=50
The server goes to that page and (usually) a server-side script will do some work before outputting the page to the user. With PHP, the GET variable is a Global Variable (along with others, the big ones being POST and COOKIE), that the php script can retrieve by getting the value passed from the server to script, in this case in $_GET['page'].
So assuming you have a script that handles all requests for the comic storyline "My Awesome Comic" and it's broken up into 60 pages, all a user has to do to get to any page is change the GET variable to the page number. Your script might do something really simple, like get the contents of a directory called myawesomecomic/ and grab a file called 50.html and then output it to the user. But chances are, you want something a tad more sophisticated since you went to this trouble, so instead it goes and grabs page 50 from a database and updates all of the navigation and maybe even throws a stylish 50 - 60 in the top corner.
Now, here's the easy part, aka the answer to your question. Now that you know how GET variables work and what the server-script does with them, all you have to do to make navigation like you describe is:
$current_page = $_GET['page'];
$next_page = $current_page + 1;
$prev_page = $current_page - 1;
$first_page = 1;
$last_page =
And that last part is going to be up to you to know already. Either you can always have the same number of pages, or you can query the DB at the same time you pull up the current page, etc. Let's say you did query the DB for the total page count while you were getting the current page. And it returned 60 (and we'll assume, just to avoid headaches that 60 is the page count starting from 1, not 0), so back to the navigation...
$last_page = 60;
Now you have your first, last, prev, and next. Assuming you know your way around HTML, all that's left (besides making this look pretty) is:
echo "
<a href='myawesomecomic.php?page=$first_page'>First Page</a>
<a href='myawesomecomic.php?page=$prev_page'>Previous Page</a>
<a href='myawesomecomic.php?page=$next_page'>Next Page</a>
<a href='myawesomecomic.php?page=$last_page'>Last Page</a>
";
And there's your navigation. Obviously you'll want to arrange them to fit your site, not have them run together like that, use cool arrows, etc. But the links will work even if you don't do anything fancy.
BUT!
I just gave you the really nice, "my first time having fun with PHP" version. Things I left out (that you should ask more about or do some serious research on):
Since GET Variables are including on the URL, they aren't hard to figure out. I could easily change ?page=50 to ?page=1=1 and, depending on how you are getting the content, I may have just asked for all contents of your database. Never grab user-input (even the URL) straight into your variables. If you know it's page number, then you know it's a number. Check the input to make sure it's a number before running it through your script and your database.
You need to have a system for if the user puts in a number and it's out of range. Something simple like "Sorry, page not found" or even "Page not found, starting at the beginning" and sending them back to page 1, whatever. If you just output nothing, it looks bad and you reveal parts of your script's logic that could be manipulated.
When you get comfortable with GET variables, I suggest ditching them for mod_rewrite (assuming you are on an apache server). Don't jump in now, but when you get the hang of it, you can start making URLs like: example.org/comics/myawesomecomic/page50 or example.org/comics/myawesomecomic/page/50 or what have you. What really happens is all pages in that directory get redirected to the same script which then checks the URL's ending to figure out what to pull from the DB. But to the end-user, it looks like there's just a webpage. This looks cleaner (I think) and does even more to cover your script's logic. But I'm saying to wait on it because it also opens you up for other risks that can be harder to anticipate until you're used to sanitizing your user-input and handling standard page errors like "page not found."
Anyways, figuring out stuff like adding a week to a current date and adding a page number to the current page is totally how I got into PHP and web app development, so I hope this was helpful and straightforward.
This kind of navigation is called pagination. There are three variables that affect pagination on a given page:
The total number of items
The number of items per page
Current page
You usually can get the total number of items from a database easily, using a query like this:
$query = "SELECT COUNT(*) FROM tbl";
$count = mysql_result($query, 0);
And as you know the number of items per page and the current page, you can figure out how many pages are in total:
$perpage = 10; // This you can define yourself
$pages_count = ceil($count / $perpage);
After that, it's easy to do some simple if clauses to see what kind of navigation should be displayed:
// Get the current page or set default if not given
$page = isset($_GET['page']) ? $_GET['page'] : 1;
$pages_count = ceil($count / $perpage);
$is_first = $page == 1;
$is_last = $page == $pages_count;
// Prev cannot be less than one
$prev = max(1, $page - 1);
// Next cannot be larger than $pages_count
$next = min($pages_count , $page + 1);
// If we are on page 2 or higher
if(!$is_first) {
echo 'First';
echo 'Previous';
}
echo '<span>Page '.$page.' / '.$pages_count.'</span>';
// If we are not at the last page
if(!$is_last) {
echo 'Next';
echo 'Last';
}
To select the correct results from a database for the given page, you can use a query like this:
$query = "SELECT * FROM tbl LIMIT ".(int)($page - 1)." ".(int)$perpage;
That's really all there's into it. Hope this helps.
Perhaps this tutorial or this one will help you.
Edit: meh, the first one was a highly annoying site. Found another. :)
I am assuming you are not talking about pagination (i.e. splitting a big list of records into multiple pages) but about having a detail view of a record, and preoviding "next" / "previous" links to the next and previous record.
If you have no fixed way of finding out the previous and last neighbours (so you can't just say "current ID -1" and "+1") you will have to do a database query ordered by your criteria, and find out the previous and next members.
Maybe it's to deeply into the issue, and not what you need to start out, but I asked a question recently that concerns optimization issues and differnt approaches to do this "neghbor lookup" in a fast and efficient way. Caching the results of a “next” / “previous element” query
Theres quite a few methods to go about doing this, ranging from complex to simple.
(Seach for php pagination on google).
The ?id=50 in some urls is to determine the item to start at.
For example in sql, you determine a range of items to select using SELECT * FROM tbl LIMIT 0,10 , This will select records 0-10. then SELECT * FROM tbl LIMIT 11,10 would select 11 to 20.