Web Hosting Talk







View Full Version : Paging results


HakonHoy
05-23-2007, 04:45 AM
I have this code:

<?php
$db="dbname";
$connect = mysql_connect('localhost', 'user', 'pass');
if (! $connect)
die("Couldn't connect to MySQL");
mysql_select_db($db , $connect) or die("Select Error: ".mysql_error());
$query="SELECT id, headline, DATE_FORMAT(abdate, '%d.%m.%y %H:%i') AS date FROM table ORDER BY date DESC LIMIT 0, 70";
echo "<tr><td width='100%' valign='top'><br><font size='2'><b>News</b></font><br></td></tr></table><table width=100% cellpadding=2 cellspacing=2>";
$results = mysql_query($query)
or die(mysql_error());
while ($row = mysql_fetch_array($results)) {
extract($row);
echo "<tr><td><font size='1'>$date:&nbsp;</font><a href='news.php?news=$id'>$headline</a><br></td></tr>";
}
mysql_close($connect);
?>

It displays the newest 70 results, what I want is to be able to show more results using paging and 70 results per page.

<<- 1 2 3 4 5 6 7 8 9 10 ->>

The page I'm on will not be clickable. etc... Just like other websites paging. And when you are on e.g. page 7.. these will show.. eg...

<<- 2 3 4 5 6 7 8 9 10 11 ->>

I for sure hope someone can help me out! would be delighted! :)
I've tried myself but never got it to work. But I've been told its easy, but I'm not to good at PHP :/

sasha
05-23-2007, 07:36 AM
$resultsperpage = 70;

$pg = max( 1, intval ($_GET['pg']));

$query = "SELECT SQL_CALC_FOUND_ROWS id, headline, DATE_FORMAT(abdate, '%d.%m.%y %H:%i') AS date
FROM table
ORDER BY date DESC
LIMIT " . ( ($pg -1) * $resultsperpage ) ." , $resultsperpage";
$results = mysql_query($query);
$found_count = mysql_result (mysql_query ("SELECT FOUND_ROWS() ") , 0 ) ;

$pages = '';
for ($i=1 ; $i <= ceil($found_count / $resultsperpage) ; $i++ ) {
$pages .= <a href="news.php?pg=$i">$i</a>;
}