Load more results like on facebook/twitter - php

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.

Related

Multiple result sites (pagenumber buttons)

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.

Pagination with XML and PHP?

before anyone asks; I've googled my 'question', I've also looks at the 'Questions that may already have your answer' and none of them work.
What I'm wanting to do is 'Pagination'. However, I don't want to use Databases as I've never had to and I'd rather not give up and go to them now as XML does everything I want it for.
The code I have is the following:
$files = glob('include/articles/*.xml');
foreach($files as $file){
$xml = new SimpleXMLElement($file, 0, true);
}
I've tried these ones already: XML pagination with PHP, PHP XML pagination and Pagination Filtered XML file and have achieved nothing. I have also tried a lot of Javascript 'pagination' scripts and still nothing.
So to sum it up: I have four articles (More to be added) and I want to show 2articles per a page. The following information will be 'pulled' from the xml file: ID, TITLE, CONTENT, PICTURE, AUTHOR, DATE by doing $xml->id and so on for the rest of them. Does anyone know of any way of doing this? as I've spent the past four hours (Its 4:04AM GMT) and have found nothing that works yet. (If I find anything that does work I'll make sure to update the question with the working code encase there is anyone else out there that needs help with this too.)
For a start define the order in which you want your articles to appear. I.e. which article goes on page 1, which one on page 2, etc. This is important, because that order will be the base for your pagination algorithm. Please note that glob() is not guaranteed to return results in any specific order, which means the order can change from one invocation of your script to another (notably when you add new articles) -- almost certainly not what you want.
Then the second step is to introduce another variable which is part of your URL that denotes the actual page (number) you're on. The URL query string would be a natural choice for putting this information, so your URL's look like: article.php?page=1. On the PHP side you can use the $_GET superglobal to retrieve the query string parameters.
Thirdly, use the new style URL's whenever you link to your article.php script. Additionally, validate the input --especially when you also want to display the current page based on this parameter (or you will end up with an injection vulnerability). This also means you want to have a default value (in case the value is invalid/wrong/ or not supplied at all for some reason).
Finally, filter your articles based on the two key pieces of information: the order of the articles w.r.t. the page number and the page number: i.e compute the actual articles that should appear on the current page.

How can I speed up Bootstrap Tooltips

I have a table which dynamically reloads continually every 60 seconds. This keeps the data up to date.
I am using the script from here - http://www.michaelfretz.com/2010/04/21/using-ajax-to-load-data-from-php-into-your-website/
On the pages that have about 15 records, the Twitter Bootstrap tooltips work fine, they are speedy and look great.
On another page however I have over 400 records. Each record has a hover tooltip which shows the information from the database about that record. The information has already been outputted to the title tag but when hovering it takes more than a second before it appears which makes the whole page seem sluggish.
I'm thinking the reason for this is due to using the 'Rel' tag and twitter javascript which is live(Continually updating) , and therefore slows it down. But I'm not sure.
Is there any way to fix this..... or am I better to try and make a paginated table which loads the next page each time I click Next?
400 records is a lot to expect someone to traverse within a 60 second period. Without actually seeing any actual html, it's a bit hard to make suggestions but here are three:
Use the title attribute instead - see about the Title attribute. This will mean that you are using inbuilt browser code rather than Bootstrap rendering for tooltips.
Show a subject/content snippet for each row rather than just subject i.e. place the initial part of the content in available space after the actual subject. Most people have large monitors these days and with a responsive design you can show a lot of content after the subject.
As you say, use pagination. Bootstrap provides one but it requires you to do the wiring.
400 Records! Too much!
This is something seriously to do with the browser and system's performance. Displaying 400 records with live() is kinda crazy. The browser will crash for sure. Instead you can do one thing. Use pagination and display only a small sub set. Also, the users will find it difficult to navigate and search.
One another way is to use datatables. Load the full content in table and don't worry about anything. Datatables will take care of the rest. Pagination and Search are good features in this.
Screenshot of Datatables:
(source: webresourcesdepot.com)
If you see this, everything from Searching, Sorting, giving tooltips are done in the client side, with minimal set of data. So the payload on the browser will be less and the users see the part, which they just require.

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.

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