Forum Moderators: open

Message Too Old, No Replies

MYSQL Page Order and Item Limit

Php,mysql

         

Clouddy

12:37 am on Mar 15, 2009 (gmt 0)

10+ Year Member



Hi there,
I'm inserting mysql database pulls onto my website, but I'm not exactly sure how to set up what I want to do. I have the pulls and everything working, but I'm going to have several pages pulling from the same database. Exactly, on my graphics I'm going to have several pages, but I'm not sure how to code to make the database make different pages, or on each page how to pull up different sections of the database so the same previews arn't shown on all the pages. Like have the newer IDs shown on the first page (10,9,8,7,6) then on the second page have it pull up (5,4,3,2,1) and move as accordingly keeping 5 on each page, with the highest number page showing the lowest/oldest.

I hope that makes sense, tried to make it as sensible. This is my database coding

mysql_connect("******", "******", "*******") or die(mysql_error());
mysql_select_db("cr_png") or die(mysql_error());

$result = mysql_query("SELECT * FROM png ORDER BY ID desc")
or die(mysql_error());

while($row = mysql_fetch_array( $result )) {
echo "<h1>" .$row['Series']. "</h1>";
echo "<p class=content-text>";
echo "<img src=" .$row['Preview']. "align=left>";
echo "<b>Series:</b>" .$row['Series'];
echo "<br>";
echo "<b>Date:</b>" .$row['Date'];
echo "<br>";
echo "<b>Extracted By:</b>" .$row['Extractor'];
echo "<br>";
echo "<b>ID:</b>" .$row['ID'];
echo "<br><br><br>";
echo "<a href=" .$row['URL']. ">Download</a>";

rocknbil

4:13 pm on Mar 15, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome aboard Clouddy,

Like have the newer IDs shown on the first page (10,9,8,7,6) then on the second page have it pull up (5,4,3,2,1) and move as accordingly keeping 5 on each page, with the highest number page showing the lowest/oldest.

So you are asking about paginating through a table?

The general logic is something like this.

First, you need a query of total records matching your select so you know how many "links" will be created by the pages. (select count(*) . . . etc.) So that gives us, in your example, $total = 10.

$per_page = 5;
$order_by = "last_modified desc";
$start = (isset($_GET['start']))?$_GET['start']:0;

So your dynamic select will do something like

select * from table [where, if you have one] order by $order_by limit $start,$per_page;

Then you generate the pagination links:

$counter = 0;
for ($i=$start; $i<=$total; $i+=$per_page){
$counter++;
$links .= '<a href="script.php?start='.$i.'">'.$counter.'</a> ¦ ';
}

(Actually this is not perfect, as you'll have some offsets with the first and last record - just to get you thinking)

So it should generate something like

<a href="script.php?start=0">1</a> ¦
<a href="script.php?start=5">2</a> ¦

so the two links should generate

select * from table [where, if you have one] order by last_modified desc limit 0,5;

select * from table [where, if you have one] order by last_modified desc limit 5,5;

The "last page" may not divide evenly by $per_page, so you'll almost always have to do a special case for the last link (if you make the links say "startnum-endnum" instead of the generic "counter.") But this will get you thinking along the right lines, I have working code but it is too large as it deals with limiting the number of links on the page for millions of records.

Clouddy

8:05 pm on Mar 15, 2009 (gmt 0)

10+ Year Member



Thank you very much. Thats the basic gist of it ^^, the numbers I was using was the ID of my items, as thats how I sort them as I like.

One quick question though, will I need to make the seperate pages or will the code generate it's own?

rocknbil

4:17 pm on Mar 16, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No, it should output dynamically based on the "start" parameter.

First visit to the page, start is not present so it's sets to 0, pulls the first $per_page of records.

Click the second link, start=5, so it will pull $per_page results starting from the 5th record (actually 6th . . . but yeah . . . )

Once you figure it out, you move your method of get-limit-string and generate-footlinks to a function/class of their own so it can be used by any search routine throughout your programs.