PHP Simplicity Forum

Please login or register.

Login with username, password and session length
Advanced search  

News:

User registration is disabled. Thanks to the enhanced spam bots but you can still post as a guest.

Author Topic: About Pagination  (Read 1927 times)

0 Members and 1 Guest are viewing this topic.

julian

  • Newbie
  • *
  • Offline Offline
  • Posts: 1
    • View Profile
About Pagination
« on: August 24, 2009, 06:02:47 AM »

hello all, welcome to me.. :)

wow, it's been quite a while since the last post in this forum..
and I was hoping that someone will answer my query about pagination..

I was browsing the web and wondering about limit pagination in php n mysql, then landed in http://www.phpsimplicity.com/tips.php?id=1
First off, I would like to say thank you to Saleh Jamal in regards to his post from that link about "limit the number of rows returned by MySQL"..

I've done everything off that link correctly, creating the "Next" and "Previous" links to works fine..
But I got a question, what if it comes to the last/first record?

eg.:
I'm viewing the first row and listing all 20 data, and when I click on the Previous link using the following code:

Code: [Select]
<?PHP
//now this is the link..
echo '<a href="'.$_SERVER['PHP_SELF'].'?startrow='.($startrow-10).'">Previous</a>';
?>

it returns me this error message:
Code: [Select]
"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 '-10, 20' at line 1"
perhaps there must be an "if" clauses somewhere to be used, but I don't know how to do that..

I'm hoping someone could answer my query on this problem.. thank you :)
Logged

Saleh

  • Administrator
  • Full Member
  • *****
  • Offline Offline
  • Posts: 222
    • View Profile
    • PHP Simplicity
Re: About Pagination
« Reply #1 on: August 25, 2009, 12:59:04 PM »

hello all, welcome to me.. :)
Yeah, Welcome.  It's been too long, eh! :)
The forum was down and I didn't feel like fixing it but then I had some time so I did.

it returns me this error message:
Code: [Select]
"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 '-10, 20' at line 1"
perhaps there must be an "if" clauses somewhere to be used, but I don't know how to do that..

I'm hoping someone could answer my query on this problem.. thank you :)
You are right. It does need an IF statement. That script is only a starter point since it doesn't detect the total number of rows and when it should stop showing "Next" links.

Anyway, a quick fix is doing the following:
Replace:
Code: [Select]
echo '<a href="'.$_SERVER['PHP_SELF'].'?startrow='.($startrow-10).'">Previous</a>';
with:
Code: [Select]
$prev = $startrow - 10;

if ($prev >= 0)
    echo '<a href="'.$_SERVER['PHP_SELF'].'?startrow='.$prev.'">Previous</a>';
That should do it :)
Hope that helps and glad you liked that little article.
« Last Edit: August 25, 2009, 03:41:11 PM by Saleh »
Logged

We don't need a reason to help people

daniela

  • Guest
Re: About Pagination
« Reply #2 on: November 25, 2009, 12:14:29 PM »

Hello!
I was looking for something like this and it works almost perfectly. I also want to have pages. Can you help me with that?

Regards,
Daniela
Logged

Saleh

  • Administrator
  • Full Member
  • *****
  • Offline Offline
  • Posts: 222
    • View Profile
    • PHP Simplicity
Re: About Pagination
« Reply #3 on: November 27, 2009, 05:25:11 PM »

What do you mean exactly by "pages"?
Logged

We don't need a reason to help people
 

+ Quick Reply

With Quick-Reply you can write a post when viewing a topic without loading a new page. You can still use bulletin board code and smileys as you would in a normal post.
Name: Email:
Verification:
Type the letters shown in the picture
Listen to the letters / Request another image
Type the letters shown in the picture:
What is the answer to one plus one? (in numbers):