Forum Moderators: open
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>";
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.
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.