View Full Version : Paginate issues
crazy8
05-28-2008, 11:34 AM
Im working on some pagination stuff and im running into some issues. I can get the numbers and such to display but cant actually get it to work correctly. I think this may be part of my issue. I have links on my trips.php and page.php pages that read as...
<a href="page.php?group=southdakota">South Dakota</a>
<a href="page.php?group=yellowstone">Yellowstone</a>
But with the pagination script I found it wants to do something similar but more like...
$target = this_script.php
<a href="$target?page=$some_number"></a>
Anyway I was wondering if someone could take a look at this and see if there is a way I could get this to work with how I have it set up. I know Pagination should be simple but Im stumped on this one.
Thanks much for the help.
Jim7283
05-28-2008, 03:34 PM
I didn't take a look at your ZIP, but I'd definitely recommend using a pagination class such as PaginateIT:
http://www.bradyvercher.com/php-pagination-class.html
It has made pagination extremely simple across a number of different areas of our site, and should solve all your problems!
crazy8
05-28-2008, 04:55 PM
How do I get it to connect to my SQL? It has to do so to know how many items I have correct?
Jim7283
05-29-2008, 09:27 AM
Well I would assume that you have a 'config.php' or a DB connection being called somewhere in your script, so all you need to do is run a query on the results you want to paginate, and append the $PaginateIT->GetSqlLimit() function on the end of your query to limit the results per page properly. Here is an example of how I have it set up:
// Count Items for pagination class
$sql = mysql_query("SELECT * FROM table WHERE display = '1'");
$count = mysql_num_rows($sql);
$PaginateIt = new PaginateIt();
$PaginateIt->SetItemCount($count);
$PaginateIt->SetItemsPerPage(5);
$PaginateIt->SetLinksHref('yourpage.php');
$PaginateIt->SetLinksFormat( '<< Back', ' | ', 'Next >>' );
echo '<div class="pagination"><span>page :: '.$PaginateIt->GetPageLinks().'</span></div>';
// Pull info from the table & print accordingly
$result = mysql_query("SELECT * FROM table WHERE display = '1' ORDER BY 'id' DESC" . $PaginateIt->GetSqlLimit());
You'll need to set a couple of variables:
1. $PaginateIt->SetItemCount($count) denotes how many total results are in your query. This needs to be set so that the total number of pages can be calculated properly.
2. $PaginateIt->SetItemsPerPage(5) denotes how many results you want listed on each page. For my purposes I wanted 5 results displayed, but it can be set to any number.
3. $PaginateIt->SetLinksHref('yourpage.php') sets the base page for generating the URLs, which is likely going to be the same page you're working on
4. $PaginateIt->SetLinksFormat() tells the class how you want the paginiation links displayed. There are a number of options for this, so I would suggest you refer to the README doc to figure out which style will work best for you
5. Last but not least, be sure you concatenate your query with $PaginateIt->GetSqlLimit() at the end
crazy8
05-30-2008, 10:11 AM
I know this is one of those things that should not be difficult. Anyway here is what I have now...
<?php
include ('db_connect_images.php');
include ('PaginateIt.php');
// Count Items for pagination class
$sql = mysql_query("SELECT * FROM images");
$count = mysql_num_rows($sql);
$PaginateIt = new PaginateIt();
$PaginateIt->SetItemCount($count);
$PaginateIt->SetItemsPerPage(5);
$PaginateIt->SetLinksHref('trips.php');
$PaginateIt->SetLinksFormat( '<< Back', ' | ', 'Next >>' );
echo $PaginateIt->GetPageLinks();
// Pull info from the table & print accordingly
$result = mysql_query("SELECT * FROM images WHERE ORDER BY 'id' DESC" . $PaginateIt->GetSqlLimit());
?>
All I get is...
1 | 2 at the top inside my page and all of the images that I have uploaded this far for that "group" of images. In my southdakota group I have 7 images, notice I have that SetItemsPerPage set to 5. Yet all 7 display onto one page.
What am I missing here?
Jim7283
06-03-2008, 03:34 PM
Hmmm... I'm not sure - can you do the following:
Change this line:
$result = mysql_query("SELECT * FROM images WHERE ORDER BY 'id' DESC" . $PaginateIt->GetSqlLimit());
To this:
$sql = "SELECT * FROM images WHERE ORDER BY 'id' DESC" . $PaginateIt->GetSqlLimit());
$result = mysql_query($sql)
print $sql;
And then can you post the query (returned by the print statement) so we can see what's going on? It looks like you have everything setup properly at first glance, but it sounds like the query isn't limiting the result set properly...
crazy8
07-02-2008, 01:28 PM
Sorry it took forever to get back to this, Ive been pretty busy. Which sucks because I need to get this done before my trip in 2 weeks. Anyway here is what I did...
// Pull info from the table & print accordingly
$result = mysql_query("SELECT * FROM images WHERE ORDER BY 'id' DESC" . $PaginateIt->GetSqlLimit());
print $sql;
and from what I can see this is what i got...
1 | 2Resource id #6
South Dakota Yellowstone
The "2","South Dakota", and "Yellowstone" show as links the rest shows as just text.
Any ideas?
Jim7283
07-09-2008, 12:40 PM
It looks like you have the query syntax setup incorrectly - I don't think MySQL will understand 'WHERE ORDER BY' so try adjusting it as follows:
// Pull info from the table & print accordingly
$result = mysql_query("SELECT * FROM images ORDER BY 'id' DESC " . $PaginateIt->GetSqlLimit());
print $sql;
vBulletin® v3.6.7, Copyright ©2000-2010, Jelsoft Enterprises Ltd.