Jump to content

I need a good example of paginating database results.


Mavent

Recommended Posts

Hello everyone;

I have a database with about ten thousand entries.  Obviously, I'd like to paginate it.  I did what I normally do in these situations: I went to Google, and started looking for sample code as a place to start.  The problem is that every single sample I could find was apparently written in 1605 by Tibetan Monks or something, because absolutely none of it works.  By "doesn't work" I mean "constantly throws errors denying that my Server has ever heard of the syntax being used."

 

Now, I know the DB is set up correctly in all other respects, because I'm able to Query it for all my other processes.  I can add to it, display it, edit it, and so forth, so I know it's not a basic connection problem.  The errors center around counting the number of rows in the table.  I'm five examples in, and so far three of them have told me that "$rows = mysql_num_rows($data)" isn't a real thing, and the other two told me that COUNT(*) "doesn't contain any data".

 

Given that I'm clearly new at this, I'd tend to blame myself, except that I'm not writing a single line of the code.  All I'm doing is using the sample code and putting in my connection/database information, using the identical connection info that I'm using to create and display the database in the first place.

 

Soooooo.... my question is this: can anyone steer me towards a current, working sample of pagination code?  I'd be forever in your debt.

Thanks!

 

Oh, here's the code that's currently failing:

 

$sql = "SELECT COUNT(*) FROM mytable";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
$r = mysql_fetch_row($result);
$numrows = $r[0];

 

The error is: Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource

 

Kyle

Link to comment
Share on other sites

try this phpsense onpagination class

http://phpsense.com/2007/php-pagination-script/

 

Thank you very much for the links- I got this one up and running in about 30 seconds.  At the risk of sounding stupid though, shouldn't it be displaying some actual content along with the links?  :) Here's the result:

http://www.secretauctions.com/pagination/usage.php

 

You'll note that it's indeed giving me the pagination... but no content.

 

Here's what the unpaginated page looks like, just to establish that there is, in fact, content in the database:

http://www.secretauctions.com/carlist.php

 

Link to comment
Share on other sites

From your error message, the connection is not established/failed.

 

Yeah, that's what it looked like to me, too, but the connection code is exactly the same as what I'm using on this page:

http://www.secretauctions.com/carlist.php

 

That page is obviously connecting, which makes the failure of the other code just that much more frustrating.  :D

Link to comment
Share on other sites

shouldn't it be displaying some actual content

 

Only if you modified the display code to be specific to your table column(s.)

 

Do you have php's error_reporting set to E_ALL so that all the php detected errors will be reported?

 

Beyond that, you would need to post your usage.php code that you modified to use with your database table.

Link to comment
Share on other sites

shouldn't it be displaying some actual content

 

Only if you modified the display code to be specific to your table column(s.)

 

That was it.  I failed to notice where I needed to do the column names the first time through.  Now everything is nearly perfect:

http://www.secretauctions.com/carlist1.php?page=2&v1=Name&v2=ASC

 

I say "nearly" because the Pagination appears to believe that B comes before A in the Alphabet, but for the most part it's great. :)  I'm going to assume for the moment that the minor problems are due to mistakes that I'll be able to find once it's no longer 2 o'clock in the morning.  :)

 

Thanks to everyone!

Kyle

Link to comment
Share on other sites

The reason you have a little more than 1 page of data that is out of alphabetically order and comes before the rest is because that data has some white-space characters (looks some with 1 and some with 2 space characters in the 'view source') at the start of each value and the ORDER BY places those rows first. You need to trim the data, at least in the name column, in your database table, so that the ORDER BY will produce the correct results.

 

Link to comment
Share on other sites

The reason you have a little more than 1 page of data that is out of alphabetically order and comes before the rest is because that data has some white-space characters (looks some with 1 and some with 2 space characters in the 'view source') at the start of each value and the ORDER BY places those rows first. You need to trim the data, at least in the name column, in your database table, so that the ORDER BY will produce the correct results.

 

You are again correct.  :)  Thanks!  Sorry about all the newbie mistakes- this is my first database project. 

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.