Jump to content

[SOLVED] Problem with Pagination Script


Fluoresce

Recommended Posts

I found an excellent script for pagination on PHPFreaks.com, written by some guy called Crayon Violent. I set it up on my site but have encountered a problem . . .

 

It displays navigation links like this:

 

1 2 3 > >>

 

Even if no results are returned from the database, the pagination links are still there:

 

1 2 3 > >>

 

If 2 or 3 are clicked, they go to blank pages.

 

I've played with $range (see below) but the problem remains. When I change $range to 0, for example, the pagination links display like the following, even if no results are returned:

 

1 > >>

 

If ">" or ">>" are clicked, they lead to blank pages.

 

Anyone know how I can fix this? If no results are returned, I don't want links to blank pages.

 

Below's the full code. However, I think only the end bit is relevant.

 

Thank you for any help in advance.

 

<?php

// database connection info
$conn = mysql_connect('localhost','root') or trigger_error("SQL", E_USER_ERROR);
$db = mysql_select_db('ctyi', $conn) or trigger_error("SQL", E_USER_ERROR);

// find out how many rows are in the table
$sql = "SELECT COUNT(*) FROM tcoupon";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
$r = mysql_fetch_row($result);
$numrows = $r[0];

// number of rows to show per page
$rowsperpage = 5;
// find out total pages
$totalpages = ceil($numrows / $rowsperpage);

// get the current page or set a default
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
// cast var as int
$currentpage = (int) $_GET['currentpage'];
} else {
// default page num
$currentpage = 1;
} // end if

// if current page is greater than total pages...
if ($currentpage > $totalpages) {
// set current page to last page
$currentpage = $totalpages;
} // end if
// if current page is less than first page...
if ($currentpage < 1) {
// set current page to first page
$currentpage = 1;
} // end if

// the offset of the list, based on current page
$offset = ($currentpage - 1) * $rowsperpage;

// get the info from the db
$sql = "SELECT * FROM wherever LIMIT $offset, $rowsperpage";

$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);

if(mysql_num_rows($result) < 1)

{
echo "<p>No results.</p>";
}

else

{

while($row = mysql_fetch_assoc($result))
{

// echo data
echo "Results";

} // end while
} // end else


/****** build the pagination links ******/

// if not on page 1, show back links
if ($currentpage > 1) {
// show << link to go back to page 1
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> ";
// get previous page num
$prevpage = $currentpage - 1;
// show < link to go back to 1 page
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> ";
} // end if

// range of num links to show
$range = 3;

// loop to show links to range of pages around current page
for ($x = (($currentpage - $range) - 1); $x < (($currentpage + $range) + 1); $x++) {
// if it's a valid page number...
if (($x > 0) && ($x <= $totalpages)) {
// if we're on current page...
if ($x == $currentpage) {
// 'highlight' it but don't make a link
echo " [$x] ";
// if not current page...
} else {
// make it a link
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> ";
} // end else
} // end if
} // end for

// if not on last page, show forward and last page links
if ($currentpage != $totalpages) {
// get next page
$nextpage = $currentpage + 1;
// echo forward link for next page
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>></a> ";
// echo forward link for lastpage
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>>></a> ";
} // end if

/****** end build pagination links ******/

?> 

Link to comment
Share on other sites

On the page do yo get "No results" with the linkable carrots (< <<  >> >)

 

Hi, Peranha!

 

Yes, it says "No results!" on the page, and the pagination links are there, too, even though there's no results!

 

All of them (2, 3, ">" and ">>") link to blank pages.

 

Any ideas?

Link to comment
Share on other sites

Then change these lines

 

{

echo "<p>No results.</p>";

}

 

to

 

{

echo "<p>No results.</p>";

die();

}

 

Peranha, it kind of worked! Thank you very, very much!

 

I say it kind of worked because, you know the "Build the pagination links" section in my code? I had copied and pasted that above the bit that echos the results, so that the pagination links would appear both above and below the results. After applying your suggestion, if no results are returned for a query, just the bottom pagination links don't appear. The ones at the top do appear.

 

Even though you've been very helpful already - which I very much appreciate - do you know why this might be? How can I get both sets of pagination links not to appear if there are no results?

Link to comment
Share on other sites

Then change these lines

 

{

echo "<p>No results.</p>";

}

 

to

 

{

echo "<p>No results.</p>";

die();

}

 

Actually, I've just noticed a problem . . .

 

When I try what you said, Peranha, nothing at the bottom of my page appears, including my footer! Half my site's missing!

 

Any other ideas, Sir?

Link to comment
Share on other sites

The logic of the page will have to be redone.  A die statement stops the script from running after that point.

 

Apparently, my problem is that my script doesn't check if the proceeding pages contain any records.

 

for ($x = (($currentpage - $range) - 1); $x < (($currentpage + $range) + 1); $x++) {

 

It loops through all the ranges, but doesn't do anything additional to make sure that the data for page $x exists. That's why the links always appear whether or not there is data for the page.

 

Do you how I might be able to do this?

Link to comment
Share on other sites

You can try this and see if it works the way you want it to.  I rearrange the logic a little, and I didnt test it, but should work.

 

<?php

// database connection info
$conn = mysql_connect('localhost','root', 'pass') or trigger_error("SQL", E_USER_ERROR);
$db = mysql_select_db('database', $conn) or trigger_error("SQL", E_USER_ERROR);

// find out how many rows are in the table
$sql = "SELECT COUNT(*) FROM table";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
$r = mysql_fetch_row($result);
$numrows = $r[0];

// number of rows to show per page
$rowsperpage = 5;
// find out total pages
$totalpages = ceil($numrows / $rowsperpage);

// get the current page or set a default
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
// cast var as int
$currentpage = (int) $_GET['currentpage'];
} else {
// default page num
$currentpage = 1;
} // end if

// if current page is greater than total pages...
if ($currentpage > $totalpages) {
// set current page to last page
$currentpage = $totalpages;
} // end if
// if current page is less than first page...
if ($currentpage < 1) {
// set current page to first page
$currentpage = 1;
} // end if

// the offset of the list, based on current page
$offset = ($currentpage - 1) * $rowsperpage;

// get the info from the db
$sql = "SELECT * FROM table LIMIT $offset, $rowsperpage";

$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);

if(mysql_num_rows($result) > 1){

while($row = mysql_fetch_assoc($result))
{

// echo results from search.
echo "Results";
} // end while

/****** build the pagination links ******/

// if not on page 1, show back links
if ($currentpage > 1) {
// show << link to go back to page 1
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> ";
// get previous page num
$prevpage = $currentpage - 1;
// show < link to go back to 1 page
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> ";
} // end if

// range of num links to show
$range = 3;

// loop to show links to range of pages around current page
for ($x = (($currentpage - $range) - 1); $x < (($currentpage + $range) + 1); $x++) {
// if it's a valid page number...
if (($x > 0) && ($x <= $totalpages)) {
// if we're on current page...
if ($x == $currentpage) {
// 'highlight' it but don't make a link
echo " [$x] ";
// if not current page...
} else {
// make it a link
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> ";
} // end else
} // end if
} // end for

// if not on last page, show forward and last page links
if ($currentpage != $totalpages) {
// get next page
$nextpage = $currentpage + 1;
// echo forward link for next page
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>></a> ";
// echo forward link for lastpage
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>>></a> ";
} // end if

/****** end build pagination links ******/

} // end else

else

{
echo "<p>No results.</p>";
}

?> 

Link to comment
Share on other sites

Thank you very much for your help, Peranha!

 

Your code partly works.

 

If there are no results, the pagination links don't show - just like we wanted!

 

However, if there's, say, just a few results, the pagination links for all pages (1 2 3 > >>) appear again! Once again I've got links to blank pages!

 

This problem's driving me CRAZY!

Link to comment
Share on other sites

This is what I use for my pagination, I have png files with arrows on them, and it shows them, but it doesnt make the link active if there is only 1 page, or no pages at all.  If you want, just put your < > in the place of the images and it will show them instead.  It should work.

 

				if ($pageno == 1) {
					echo "<img src='pics/first.png' alt='' /> <img src='pics/previous.png' alt=''/> ";
				} else {
					echo " <a href='{$_SERVER['PHP_SELF']}?pageno=1'><img src='pics/first.png' alt=''/></a> ";
					$prevpage = $pageno-1;
					echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$prevpage'><img src='pics/previous.png' alt=''/></a> ";
				} // if

				echo " ( Page $pageno of $lastpage ) ";

				if ($pageno == $lastpage) {
					echo " <img src='pics/next.png' alt=''/> <img src='pics/last.png' alt=''/>";
				} else {
					$nextpage = $pageno+1;
					echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$nextpage'><img src='pics/next.png' alt=''/></a>";
					echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$lastpage'><img src='pics/last.png' alt=''/></a>";
				} // if

Link to comment
Share on other sites

Peranha, thank you very much! I am very grateful. However, I need to make an apology, because I have just understood something very important . . .

 

There's nothing wrong with my script! Nor is there anything wrong with Sabo86's script, I suspect.

 

The number of pagination links that are displayed on any one page following a query depend on how many rows I have in my database and how many rows I want to be displayed on each page. For example, if there were 10 rows in my database and I wanted to show 5 per page, links to pages 1 and 2 would appear on my page, thus:

 

1 2 > >>

 

And if there were 15 rows in my database and I wanted to display 5 per page, links 1, 2 and 3 would appear on my page, thus:

 

1 2 3 > >>

 

No more than links 1, 2 and 3 would ever appear because I have set $range to 3.

 

Now, if I had 15 rows in my database, even if my SELECT query returned no rows, I would still have links 1, 2 and 3 on my page because the number of links present on my page will always depend on how many rows I have in my database and how many rows I want to display on my page. Therefore, the only way to control the number of links that will actually appear on my page per each query is to make sure that the SELECT COUNT(*) query only counts the rows that will be selected during the main query and not all of my rows. In other words, the exact same rows must be taken into account by both of the following queries:

 

$sql = "SELECT COUNT(*) FROM table WHERE . . ."; 

 

$sql = "SELECT * FROM table WHERE . . . LIMIT . . .";

 

As such, I realise that I have made a mistake: in fact, there really isn't a problem with my script! I therefore want to extend a sincere apology to you, Peranha, for wasting your time.

 

Sorry, dude, and thank you very much for your help.

 

Regards,

 

Fluoresce, London

Link to comment
Share on other sites

You can try this and see if it works the way you want it to.  I rearrange the logic a little, and I didnt test it, but should work.

 

<?php

// database connection info
$conn = mysql_connect('localhost','root', 'pass') or trigger_error("SQL", E_USER_ERROR);
$db = mysql_select_db('database', $conn) or trigger_error("SQL", E_USER_ERROR);

// find out how many rows are in the table
$sql = "SELECT COUNT(*) FROM table";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
$r = mysql_fetch_row($result);
$numrows = $r[0];

// number of rows to show per page
$rowsperpage = 5;
// find out total pages
$totalpages = ceil($numrows / $rowsperpage);

// get the current page or set a default
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
// cast var as int
$currentpage = (int) $_GET['currentpage'];
} else {
// default page num
$currentpage = 1;
} // end if

// if current page is greater than total pages...
if ($currentpage > $totalpages) {
// set current page to last page
$currentpage = $totalpages;
} // end if
// if current page is less than first page...
if ($currentpage < 1) {
// set current page to first page
$currentpage = 1;
} // end if

// the offset of the list, based on current page
$offset = ($currentpage - 1) * $rowsperpage;

// get the info from the db
$sql = "SELECT * FROM table LIMIT $offset, $rowsperpage";

$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);

if(mysql_num_rows($result) > 1){

while($row = mysql_fetch_assoc($result))
{

// echo results from search.
echo "Results";
} // end while

/****** build the pagination links ******/

// if not on page 1, show back links
if ($currentpage > 1) {
// show << link to go back to page 1
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> ";
// get previous page num
$prevpage = $currentpage - 1;
// show < link to go back to 1 page
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> ";
} // end if

// range of num links to show
$range = 3;

// loop to show links to range of pages around current page
for ($x = (($currentpage - $range) - 1); $x < (($currentpage + $range) + 1); $x++) {
// if it's a valid page number...
if (($x > 0) && ($x <= $totalpages)) {
// if we're on current page...
if ($x == $currentpage) {
// 'highlight' it but don't make a link
echo " [$x] ";
// if not current page...
} else {
// make it a link
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> ";
} // end else
} // end if
} // end for

// if not on last page, show forward and last page links
if ($currentpage != $totalpages) {
// get next page
$nextpage = $currentpage + 1;
// echo forward link for next page
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>></a> ";
// echo forward link for lastpage
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>>></a> ";
} // end if

/****** end build pagination links ******/

} // end else

else

{
echo "<p>No results.</p>";
}

?> 

 

 

 

One quick thing, Peranha, before I mark the thread as "Solved" . . .

 

Can you tell me, please, what you did to stop my pagination links from showing when no results are returned? I can't figure it out.  ???

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.