View Full Version : Pagination Error
Titan85
02-08-2007, 02:38 AM
ok, I have been trying to get a pagination script running for a while now and can't seem to get it. Here is my code:
function getLinkPage() {
// Pagination Script
if($_GET['page_num']) { // Is the page defined?
$page_num = $_GET['page_num']; // Set page defined
} else {
$page_num = 1; // Default page is 1
}
$max = 20; // Max results on a page
$cur = (($page * $max) - $max); // Works out what results to show
$count_total = mysql_query("SELECT * FROM links"); // Get data from database
$count_total = mysql_num_rows($count_total); // Count lines in database
$total_pages = ceil($count_total / $max); // Total results divided by max to display
if($page > 1) { // Is the page # more than one?
$prev = ($page - 1); // If yes, take one away from current page
$prevTxt = '<a href="?page_num='.$prev.'"><<Previous</a>';
}
$nav = ' ';
for($i == 1; $i <= $total_pages; $i++) // For each page number
if($page == $i) { // If page = current page
$nav .= '<b>'.$i.'</b>'; // Echo page in bold
} else {
$nav .= ' <a href="">'.$i.'</a> '; // Echo link to the page
}
if($page < $total_pages) { // Is there another page?
$next = ($page + 1); // If so, add 1 to current page
$nextTxt = '<a href="">Next>></a>'; // Echo next page link
}
Here is the code where I run the sql query:
$max = 20; // Max results on a page
$cur = (($page_num * $max) - $max); // Works out what results to show
// Query to get links from database
$sql = "SELECT * FROM `links` ORDER BY `count` ASC LIMIT $cur,$max";
$result = mysql_query($sql) or die ('Error Getting Links! <br />' .mysql_error());The error I get is this: "Error Getting Links!
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-20,20' at line 1". Obviously it has something to do with my $cur,$max statement to limit the results. I am thinking that maybe it is not setting the page number to 1 and therefore multiplying 20 by 0 and subtracting 20, hence -20. Could the problem be that I also use ?page=whatever to get pages, not just page numbers? Thanks in advance
Titan85
02-08-2007, 10:55 PM
Anyone have an idea of what could be wrong?
boxxertrumps
02-08-2007, 11:03 PM
try using single quotes ' instead of backticks `
should help some.
Titan85
02-08-2007, 11:39 PM
try using single quotes ' instead of backticks `
should help some.I don't see anywhere that I used backticks where I need to use single quotes. I only used backticks in the sql queries
djr33
02-09-2007, 04:57 AM
I'm almost positive that is NOT the problem.
The problem is with your min/max.... you can't start at a negative entry.
You're right. You start with 0, not 1. You could just multiply by $pagenum+1, or you could just figure out a better way to set it in the first place. The default should be 1, so I guess you'd have it set correctly if you have 2, 3, ..., etc. So... I think the solution is just making it default to 1 if it isn't set.
$page_num = $_GET['page_num'] ? is_numeric($_GET['page_num']) : 1;
(This is a compact if/else statement: $x = this IF condition ELSE this)
You already have something like this, but might not be working. Just using if ($x) isn't a very good way to check.
Titan85
02-09-2007, 12:12 PM
I did what you said and now I get: "
Warning: Division by zero in /home/bntqann/public_html/testing/cms/functions.php on line 61". Here is lines 58-61:
$count_results = mysql_query("SELECT * FROM `links` ORDER BY id DESC"); // Get data from database
$count_total = mysql_num_rows($count_results); // Count lines in database
$total_pages = ceil($count_total / $max); // Total results divided by max to displayI should be getting results from my query because there is a link in the database. Any ideas?
boxxertrumps
02-09-2007, 03:04 PM
sorry for misleading, i thought backticks were invalid in sql...
djr33
02-09-2007, 06:12 PM
Hm.... that would mean $max is 0, clearly. The question is why and how to fix it.
OH! Obviously...
$page_num = [something];
....
($page * $max)
Erm.... $page or $page_num? You gotta pick... heh.
That should fix all of it.
And note that my line of code may not be needed any more, though it's a lot cleaner than the if/else you have and more accurate. If someone chose to input ?page_num=hello, that would give odd results.
Titan85
02-09-2007, 09:03 PM
Ok, I did what you said and it took the error out, but the pages are not being displayed. Here is my updated code:
// Pagination for counter
function getLinkPage() {
$max = 20;
$page_num = ($page_num * $max);
$count_results = mysql_query("SELECT * FROM `links` ORDER BY id DESC"); // Get data from database
$count_total = mysql_num_rows($count_results); // Count lines in database
$total_pages = ceil($count_total / $max); // Total results divided by max to display
if($page_num > 1) { // Is the page # more than one?
$prev = ($pag_nume - 1); // If yes, take one away from current page
$prevTxt = '<a href="?page_num='.$prev.'"><<Previous</a>';
}
$nav = ' ';
for($i == 1; $i <= $total_pages; $i++) // For each page number
if($page_num == $i) { // If page = current page
$nav .= '<b>'.$i.'</b>'; // Echo page in bold
} else {
$nav .= ' <a href="">'.$i.'</a> '; // Echo link to the page
}
if($page_num < $total_pages) { // Is there another page?
$next = ($page_num + 1); // If so, add 1 to current page
$nextTxt = '<a href="">Next>></a>'; // Echo next page link
}
}I echo the pages like this:
echo 'Pages: '.$prevTxt . $nav . $nextTxt.'';Any idea why its not displaying the page? Thanks for the help
djr33
02-09-2007, 10:41 PM
$nextTxt = '<a href="">Next>></a>'; // Echo next page link
...an actual HREF might be helpful ;)
$nav .= ' <a href="">'.$i.'</a> '; // Echo link to the page
...same
From what I can tell... that is NOT how you 'echo the pages'... in fact, that is just echoing links... broken ones at that.
You're just echoing links to the pages... not text of the pages themselves.
Is this for a TOC type script? Why paginate a TOC?
I'm a bit confused.
thetestingsite
02-10-2007, 01:58 AM
$nextTxt = '<a href="">Next>></a>'; // Echo next page link
...an actual HREF might be helpful ;)
$nav .= ' <a href="">'.$i.'</a> '; // Echo link to the page
...same
Also noted the
if ($page_num > 1) {
$prev = $pag_nume ...
}
Shouldn't that be page_num?
Now as far as the page not showing up anything (just blank), or is it showing up the page links at least? Nowhere in your code (the code you posted at least) does it show anything about displaying (spelling?) stuff.
It could be something else in your code (the part[s] you are not posting.
Hope this helps.
Titan85
02-10-2007, 06:31 AM
Well, I am trying to have pagination to limit the amount of links showed on a page in a link manager. There are also some member view scripts that I would like to use this on. I am attempting to use things that I have seen, so I could be totally wrong in what I am doing. If anyone would be so kind, could you show me what a pagination script should look like? Thanks again
thetestingsite
02-10-2007, 03:42 PM
Well, that's all the pagination script does with the exception of a few things. I don't have time to post a code example, because I am getting ready for work right now. As soon as I get in I will post an example for you, unless someone else does before.
Titan85
02-10-2007, 04:00 PM
Well, that's all the pagination script does with the exception of a few things. I don't have time to post a code example, because I am getting ready for work right now. As soon as I get in I will post an example for you, unless someone else does before.Ok, thanks. I myself am getting ready for work at the moment, so I understand the lack of time :)
thetestingsite
02-10-2007, 05:41 PM
<?php
##### Pagination Example (Not Tested, but should work) #####
$page = $_GET['page']; //request and assign the page variable
$max = 20; //how many results per page
$start = ($page * $max);
$count_results = mysql_query("SELECT * FROM `links` ORDER BY id DESC"); // Get data from database
$count_total = mysql_num_rows($count_results); // Count lines in database
$total_pages = ceil($count_total / $max); // Total results divided by max to display
if($page > 1) { // Is the page # more than one?
$prev = ($page - 1); // If yes, take one away from current page
$prevTxt = '<a href="?page='.$prev.'"><<Previous</a>';
}
$nav = ' ';
for($i == 1; $i <= $total_pages; $i++) // For each page number
if($page == $i) { // If page = current page
$nav .= '<b>'.$i.'</b>'; // Echo page in bold
} else {
$nav .= ' <a href="?page='.$i.'">'.$i.'</a> '; // Echo link to the page
}
if($page < $total_pages) { // Is there another page?
$next = ($page + 1); // If so, add 1 to current page
$nextTxt = '<a href="?page='.$next.'">Next>></a>'; // Echo next page link
}
##### End Pagination and Start Displaying the page #####
$info = mysql_query("SELECT * FROM `links` ORDER BY id DESC LIMIT $start, $max"); // Get data from database and limit to the max results
while ($qry = mysql_fetch_array($info)) {
$link = $qry['link'];
echo '<div>'.$link.'</div> <br>';
}
echo 'Pages: '.$prevTxt . $nav . $nextTxt; //display the page links
?>
That will produce anything that is extracted from the sql for that page. You may want to play around with it a little bit, but the script should work with very little modification.
Hope this helps.
Titan85
02-13-2007, 03:33 AM
Well, I wrote out a new script and it works pretty good. There are a few errors though. The first is that it seems to display results in reverse order. The second is that the last page is not displayed. Here is the code:
<?php
// If page is set
if($_GET['page'] && is_numeric($_GET['page'])) {
$page = $_GET['page'];
}
// Set default page
else {
$page = 1;
}
// Max results per page
$max = 2;
// Current page
$cur = (($page * $max) - $max);
// Get data
$qry = mysql_query("SELECT * FROM `test`");
$total = mysql_num_rows($qry); // count results
// Get amount of pages
$total_pages = ($total / $max);
// Run query
$sql = "SELECT * FROM `test` ORDER BY id DESC LIMIT $cur, $max";
$result = mysql_query($sql) or die ('Error Getting Data! <br />' .mysql_error());
// Is page # more than one
if($page > 1) {
$prev = ($page - 1);
$prevLink = '<a href="?page='.$prev.'"><<Previous</a>';
}
// For each page less than total pages
for($i==1;$i<=$total_pages;$i++) {
// If page = current page
if($page == $i) {
$nav .= ' <b>'.$i.'</b> ';
} else {
// Echo link to previous page
$nav .= ' <a href="?page='.$i.'">'.$i.'</a> ';
}
}
// If page is less than total pages, show next link
if($page < $total_pages) {
$next = ($page + 1);
$nextLink = '<a href="?page='.$next.'">Next>></a>';
}
########## End Pagination Script ##########
$info = mysql_query("SELECT * FROM `test` ORDER BY id DESC LIMIT $cur, $max");
while($row = mysql_fetch_array($info)) {
echo ''.$row['title'].'
<br />';
}
echo 'Pages: '.$prevLink . $nav . $nextLink;
?>Thanks for the help
mechatama25
02-14-2007, 03:39 AM
This will work
<?php
$max = 2; // max number of results
// Calculate paging
if ((isset($_GET['page'])) && (is_numeric($_GET['page']))) {
$page = $_GET['page'];
if ($page == 0) {$page = 1;} // just in case someone put in 0 for the page
} else { $page = 1; }
$offset = ($page-1)*$max; // offset of the query
$qlimit = " LIMIT {$offset},{$max}"; // limit part of the query
// Build the query
$order = " ORDER BY id DESC"; // method of order
$basequery = "SELECT * FROM test" . $order;
$query = $basequery . $qlimit;
// Show pages links
$pttl = mysql_num_rows(mysql_query($basequery)) or die ('Error Getting Data! <br />' .mysql_error());
$numpages = ceil($pttl/$max); // count the number of pages
$pagelinks = NULL; // define a variable for the page links
// If theres more than 1 page
if ($numpages > 1) {
$prev = $page-1; $next = $page+1; // set previous and next
// if its not page 1 show previous link
if ($page > 1) {$pagelinks .= " <a href=\"?page={$prev}\"><<Previous</a> ";}
// print the links
for ($p = 1; $p <= $numpages; $p++) {
// if its the current page make it bold
if ($p == $page) {$pagelinks .= " <b>{$p}</b>";}
// else make it a link
else {$pagelinks .= " <a href=\"?page={$p}\">{$p}</a>";}
}
// if its not the last page print the next link
if ($page < $numpages) {$pagelinks .= " <a href=\"?page={$next}\">Next>></a> ";}
// finally build the final page links
$pagelinks = "Pages ({$numpages}):{$pagelinks}";
}
// Run the query
$results = mysql_query($query) or die ('Error Getting Data! <br />' .mysql_error());
while ($row = mysql_fetch_assoc($results)) {
echo $row['title'] . "<br />";
}
echo $pagelinks; // print the pagelinks
?>
Titan85
02-14-2007, 03:00 PM
I tried using your code, mechatama25 and when I try to go to the page, the page will load extremely slow, but the rest of the pages on the server don't. Then sometimes it will not load, but when it does, nothing is there. I could be wrong, but it seems to me like the code is intentionally made to do something to the server to slow it down.
thetestingsite
02-15-2007, 02:20 AM
Titan, the code you posted just after my example does in fact work with very little modification. For a test of it, simply check out this page (http://phphost.smackum.com/test/page.php). If you would like the source code, click here (http://phphost.smackum.com/test/page.phps). The sql data is here (http://phphost.smackum.com/test/data.txt).
Let me know if you need any further help.
Titan85
02-15-2007, 04:29 AM
Titan, the code you posted just after my example does in fact work with very little modification. For a test of it, simply check out [/URL]. If you would like the source code, [URL="http://phphost.smackum.com/test/page.phps"]click here (http://phphost.smackum.com/test/page.php). The sql data is here (http://phphost.smackum.com/test/data.txt).
Let me know if you need any further help.Yeah, it works mostly, but there is an error (or perhaps lack of code) in displaying the page number link (1,2,3,etc). On the first page, it shows 1 in bold, but not 2, and on page 2, it displays the link to page 1, but not the current page in bold. Is this just a part of code missing? thanks again
thetestingsite
02-15-2007, 05:02 AM
Sorry Titan, forgot to post the actually link to the page for the example (fixed now). Can you post a link (or pm me a link) to the problem page as so I can take a look at it?
Titan85
02-15-2007, 12:07 PM
Sorry Titan, forgot to post the actually link to the page for the example (fixed now). Can you post a link (or pm me a link) to the problem page as so I can take a look at it?I sent a pm ;)
thetestingsite
02-16-2007, 02:25 AM
Ok, so I had a further look at the code and came up with the following.
<?php
include('dbconnect.php');
// If page is set
if($_GET['page'] && is_numeric($_GET['page'])) {
$page = $_GET['page'];
}
// Set default page
else {
$page = 1;
}
// Max results per page
$max = 7;
// Current page
$cur = (($page * $max) - $max);
// Get data
$qry = mysql_query("SELECT * FROM `test`");
$total = mysql_num_rows($qry); // count results
// Get amount of pages
$total_pages = ($total / $max);
// Is page # more than one
if($page > 1) {
$prev = ($page - 1);
$prevLink = '<a href="?page='.$prev.'"><<Previous</a>';
}
// For each page less than total pages
for($i=1;$i<$total_pages+1;$i++) {
// If page = current page
if($page == $i) {
$nav .= ' <b>'.$i.'</b> ';
} else {
// Echo link to previous page
$nav .= ' <a href="?page='.$i.'">'.$i.'</a> ';
}
}
// If page is less than total pages, show next link
if($page < $total_pages) {
$next = ($page + 1);
$nextLink = '<a href="?page='.$next.'">Next>></a>';
}
########## End Pagination Script ##########
$info = mysql_query("SELECT * FROM `test` ORDER BY `id` DESC LIMIT $cur, $max");
while($row = mysql_fetch_array($info)) {
echo $row['title'].'
<br />';
}
if ($total_pages > 1) {
echo 'Pages: '.$prevLink . $nav . $nextLink;
}
?>
Notice the part in red that I edited. I have tested it, and it works as it should.
Hope this helps.
PS: I also pm'd you the code.
Titan85
02-16-2007, 02:26 AM
Thanks, its working great now :)
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.