Forum Moderators: coopster

Message Too Old, No Replies

creating multiple pages from mysql.

         

supermanjnk

9:55 pm on Mar 15, 2005 (gmt 0)

10+ Year Member



I'm working on a small message board, and I want to make it so that if a topic has over 20 posts the 21st will begin on a new page, I'm trying to figure out the best way to do this. I figure i'll have to put a
LIMIT 20
on the mysql query. but as for how to pull the first twenty, then the next twenty, then the next twenty I don't know where to start.

jatar_k

10:26 pm on Mar 15, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



if you look at LIMIT it also has an offset

[LIMIT {[offset,] row_count ¦ row_count OFFSET offset}]

from
[dev.mysql.com...]

henry0

11:58 pm on Mar 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try adapting the following to your need
this is a basic pagination system

// ###### pagination ######

if(!isset($_GET['page'])){
$page = 1;
} else {
$page = $_GET['page'];
}

$max_results = 1;

$from = (($page * $max_results) - $max_results);

@$outcome=mysql_query("SELECT * from XXXX where user_id=$user_id ASC LIMIT $from,$max_results", $db);

$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM XXXX where user_id='$user_id' "),0);
$total_pages = ceil($total_results / $max_results);
// Build Page Number Hyperlinks
echo "<center>Select a Page: ";

// Build Previous Link
if($page > 1){
$prev = ($page - 1);
echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$prev\"><<Previous</a>&nbsp;";
}

for($i = 1; $i <= $total_pages; $i++){
if(($page) == $i){
echo "$i&nbsp;";
} else {
echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$i\">$i</a>&nbsp;";
}
}

// Build Next Link
if($page < $total_pages){
$next = ($page + 1);
echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$next\">Next>></a>";
}
echo "</center>";


if ($user_id) @$outcome=mysql_query("SELECT XXXX, aaa, sss, ddd, fff, ggg, hhh
from XXXXX where user_id=$user_id ORDER BY XXX_id ASC LIMIT $from, $max_results", $db);