Best solution for pagination results ajax codeigniter - php

I made a scroll pagination on a system of posts/comments with jquery / Ajax and codeigniter everything work fine, but i exaplain the my problem with a example:
THERE IS USER A AND USER B
I'm user A and i wrote on the profile of the user B,
In the same time the user B scroll down for see the informations on his own profile but 1 record has been inserted on the database and the pagination doesn't work fine because load 1 result identical.
I thought that the solution can be a system of cache but i would like know the logic for use it with the pagination.
Thank you so much.

The most common solution to this problem is to have a pagination pass not the page, but the ID from the database of the last item shown. So instead of calling for page X, ajax calls for items after item XX.
So the database query will look like this:
SELECT * FROM table WHERE id > 10 LIMIT 10
instead of
SELECT * FROM table LIMIT 10 OFFSET 10

Related

How to go back to same MySql record without using pagination

I have a MySql table with over 1000 products that I scroll through, I understand the SQL query below says "return only 10 records, start on record 16 and display the next 10 records"
sql =”SELECT * FROM items LIMIT 10 OFFSET 15”;
I could keep track of LIMIT and OFFSET within variables and use a pagination function to scroll through my table.
BUT I don’t want to use pagination. I just want to scroll up or down through all my records up or down, even if there where more than 1000 records, I don't care. So here is the problem, lets say product record 567 is displayed on page(A) and I have a link to another page(B) that displays more product information. Then I want to come back to page(A) to same spot, record 567 and be able to scroll up or down through my records. Even the records less than 567. This is why OFFSET is not necessarily what I need. Any Ideas would be great.
If you just use the back button of the browser it's up to the browser to memorize the position and hopefully restore it. If you go back via link an page B use anchor links in page A and have the link in page B point at that. You have to pass the right link from page A to page B of course.
E.g.:
Page A:
...
Product 4711
View details
...
Page B:
...
<h1>Product 4711</h1>
(Description of product 4711)
Go back to the list of products
...

PHP mysql_query order by views trouble

I have mysql_query problem in php. Of course I have visitors in my website and they read football news there. The problem is that I try to sort news by views DESCending, but it doesn't work for new uploaded news. Somehow it appears at the end of the page. I've figured out one more thing that when views of the news is 9 it appears at the beginning of the page, but if it is more than 9 it appears at the end of the page. I'd appreciate any kind of help. Here is mysql_query.
mysql_query("SELECT * FROM news ORDER BY views DESC");
Oh and by the way, my website is http://www.bbcsport-football.com/football
there you can find Sorting options, New and Most Viewed. Click most viewed and you'll see it.
You need to convert the views column to a number
mysql_query("SELECT * FROM news ORDER BY cast(views as INTEGER) DESC")
Or even better change the table definition so views is a INTEGER
You're sorting the news by the count of "views", if I'm right, views is the count of users that already have read the page right? Try sorting it by the publication date instead, like:
"SELECT * FROM news ORDER BY pub_date DESC"
your code seems to be o.k to me... just check this query in your phpmyadmin database and check results what you get manually.
i see your URL and it looks o.k as per order by view. but one thing i notice is your pagination.
when i click pagination (page 2) from most viewed. it will automatically redirected to the first page of new. hope this little testing will help you in your website.. :)

Navigate Throught MySQL Result

I have searched everywhere but could not get anything. The scenario is that when I run a select statement from MySQL/PHP, I want to use Next & Previous buttons to navigate backwards and forward through the results.
Does anyone know how I can accomplish this?
Here's how I run the query: select everything from members where hair = black;
This returns 8 results, and I have a page like so details.php?id=3 which takes id and display the details.
I want to be able to keep clicking the next button and move to another id from the result.
How do I accomplish this?
Thank you for your assistance.
If you mean to display 1 product details per page with Next buttons, then
1) Get the total rows from table
2) Find total number of pages like
$totalPages = $totalRows; //since 1 record per page
3) Loop thru $totalPages value to generate next links
4) Get the id of product from page url $_GET
5) Query sql using te product id obtained frm GET
Well thats the basics, hope your get it
Assuming you have all ids in the $allIds var, try something like this:
<?php
$id = (int) $_GET['id'];
$key = array_search($id, $allIds);
$nextId = $allIds[$key+1];
?>
Well, you have 2 different scopes here to address, first, when you query you are in PHP/Server side mode. PHP gets some data, processes it, shows some of it to the knowledge i have of your problem and then sends it to the browser.
Next, you have the client scope, where the client has the HTML rendered to his screen and has the possibility to click NEXT and PREVIOUS. When he does that, he issues a request to the server to get the NEXT or PREVIOUS record based on the current one.
You have 2 scenarios to work this problem out:
1) Load the whole data into memory and scan it to find your current position, see if there is a NEXT and PREVIOUS element and respond to the request of the user. If the user asked for the NEXT, easy, just move one more record. If the user asked for PREVIOUS item, then when scanning using a WHILE or FOR/FOREACH, always keep in memory the "lastitem" you saw, you can use this "lastitem" as your PREVIOUS item.
2) In the event you have many (i mean like more than 1000) items, you need to use a little subquery magic and work this out using SQL.
select everything from members where hair = black AND id = XYZ ORDER BY id LIMIT 2;
This will return you the CURRENT AND NEXT elements. Then call the same query again but this time, order it DESC so that your 2 items are the CURRENT AND PREVIOUS.
select everything from members where hair = black AND id = XYZ ORDER BY id DESC LIMIT 2;
Note that you can order this the way you want, the goal is to get a static result, something that always gets out the same way so that you can reverse it and get the items around the selected one.
I hope this helps you

Pagination with ajax

I've build an application (with PHP, Codeigniter and jQuery) that uses ajax for pagination, similar to how Twitter made it back in the day (clicking a button to load in more data).
It's all quite well, but there are a few issues however.
When there are no more posts left to load in, I want the "load-more"-button to be removed. However, right now I can only check if there are any posts left when I click the button, and the script returns null.
This is how it is now: Let's say that there are 14 posts in the database. 5 is loaded per default.
1 - click load-more, 5 more posts are loaded. 4 remain.
2 - click load-more, the remaining 4 posts are loaded.
3 - click load-more, no more remaining posts, button disappears
But I want to get rid of step 3, the application should "be aware" of that there are no more posts left to render at step 2.
I am sure there are a simple way that I haven't thought if yet...
simple. select 6 posts instead of 5 and display only 5.If no of posts less than 6 dont display more button.
if ($num_rows < 6){
//remove more button
}
You could set a global Javascript variable holding the amount of total posts something like this:
var total = <?php echo $total ?>;
And after the call compare the amount to the total:
if($('.post').length == total) // .post being database results
$('#loadmore').hide();
Return a JSON object with one property that is an array (the entries) and one property that is a bool "EOF" or such to indicate when there is no more entries to load.
if(response.EOF)
//Hide button..
Ohh and add the id of the last post from current pagination in the AjaxRequest.
That way the server gives you say 25 posts from the last one it gave you.
Then you dont get offsets if a new post is added during pagination.
you could try something like this
if ($num_rows < 5){
/*what ever you want removed */
}
at the end of you loop
Please, please, please - do not do pagination in Ajax!
It is pain in the a$$ to navigate such pages natural way, unable to use back button or reload button or set a bokmark or anything!
not to mention clients that doesn't support js at all

php and mysql site design question

I am trying to build a website with mysql and php. This is the first site I have attempted so I want to write a little plan and get some feedback.
The site allows users to add some text in a text field as a “comment”. Once the comment has been entered into the site it is added to the database where it can be voted for by other users.
When a new comment has been added to the database it needs to create a new page, e.g. www.xxxxx.com/commentname or www.xxxxxx.com/?id=99981.
There will be a list of "Comments" in the database along with the number of votes for each comment.
The home page will have two functions.
1) Allow users to add a "comment"
2) Display two tables, each with 20 rows containing most "popular comments" and "recent comments"
Each comment will generate its one page where the comment will be displayed. Here users can read the comment and Vote for the comment if they wish.
Please help me out by explaining how to do the following.
-Generate a new page whenever a comment is added to the database
-Add a vote to the vote count in the comment database.
-Display the top 20 most popular comments as per number of votes.
-Generate a new page whenever a comment is added to the database
You only need a comment.php file with a MySQL query getting the given comment out of the database. I would recommend to use the comments primary key to get the comment. Using rewrites you can have a URL like this: www.xxxx.com/comment/1. If you need the redirect for a specific link structure ask again.
-Add a vote to the vote count in the comment database.
Just add a column to your table holding the votes. If you have logged in users and you want then to check their votes, create a new table for the votes and another table for the many to many realtion.
-Display the top 20 most popular comments as per number of votes.
This is simply done by sorting in the MySQL queries and selecting only 20 results:
// For the recent 20 comments
SELECT * FROM comments ORDER BY id DESC LIMIT 0,20
// For the 20 most popular comments
SELECT * FROM comments ORDER BY votes DESC LIMIT 0,20
Any further questions?
This is a pretty broad question, i don't think we'll be able to help you fully here at stack without making a full blown php blog tutorial!
I'll try and point you in the right direction however. Firstly i'd say take a look at wordpress, even though i presume you want to make your own custom one, wordpress would be a good starting point for code inspiration? (Just a thought)
The way i'd generate a new page, would be to make a php page, say comments.php, which using the $_GET variable, gets the related record in the database and displays it.
Adding a vote up or down is as simple as adding form to the page with two submit buttons, one with a value of 1 one with a value of -1, upon submit it sends its value to the database, and takes the existing vote value say 25 and adds its value so, if u up voted 25 + 1 = 26 if you downvoted 25 + -1 = 24.
Displaying the 20 most popular comments is just a case of using some SQL sorting, something like this would work
SELECT * FROM comments ORDER BY votes DESC LIMIT 0, 20
That statement selects all the columns from the comments table, sorts it by the votes column decending, so highest value first, and then limits the number of records it fetches by 20, from there its a case of looping through each record and displaying it how you wish.
I hope this atleast gets you started on the right path :)

Categories