Jump to content

[SOLVED] pagination


almightyegg

Recommended Posts

I know this comes up a lot, but I've never been able to understand i and the php freaks tutorials don't help either...

 

need a pagination script for my message board, I got a script of a website and it just shows:

<< < [1] > >> but, it is not making a difference on showing how ever many posts I tell it to, it changes the number of pages...

I think it was because I didn't know where to put my loops for them to paginate  :-\

 

pagination script:

$display = 20;
$pg = (isset($_REQUEST['pg']) && ctype_digit($_REQUEST['pg'])) ?
  $_REQUEST['pg'] : 1;
$start = $display * $pg - $display;
$fluff = mysql_query("SELECT count(*) FROM topics WHERE rid = '0' AND fid = '$f'");
$total = mysql_result($fluff, 0);
if (isset($_SERVER['QUERY_STRING']) && trim($_SERVER['QUERY_STRING'])
  != '') {
  if(stristr($_SERVER['QUERY_STRING'], 'pg='))
    $query_str = '?'.preg_replace('/pg=\d+/', 'pg=', 
      $_SERVER['QUERY_STRING']);
  else
    $query_str = '?'.$_SERVER['QUERY_STRING'].'&pg=';
} else
  $query_str = '?pg=';
$pages = ($total <= $display) ? 1 : ceil($total / $display);
$first = '<a href="'.$_SERVER['PHP_SELF'].$query_str.'1">&#171;
</a>';
$prev = '<a href="'.$_SERVER['PHP_SELF'].$query_str.($pg - 1).'">
&#139;</a>';
$next = '<a href="'.$_SERVER['PHP_SELF'].$query_str.($pg + 1).'">
&#155;</a>';
$last = '<a href="'.$_SERVER['PHP_SELF'].$query_str.$pages.'">
&#187;</a>';
echo '<div><p align="center">';
echo ($pg > 1) ? "$first : $prev :" : '&#171; : &#139; :';
$begin = $pg - 4;
while($begin < 1)
  $begin++;
$end = $pg + 4;
while($end > $pages)
  $end--;

for($i=$begin; $i<=$end; $i++)
  echo ($i == $pg) ? ' ['.$i.'] ' : ' <a href="'.
    $_SERVER['PHP_SELF'].$query_str.$i.'">'.$i.'</a> ';
echo ($pg < $pages) ? ": $next : $last" : ': &#155; : &#187;';
echo '</p></div>';

 

My message Board (main page) it is a big to read:

$f = $_GET['f'];

$result = mysql_query("select title from forums where id = '$f'");

$result2 = mysql_query("SELECT * FROM topics WHERE fid = '$f' AND pinned = 'No' AND rid = '0' order by timestamp desc");

$r = mysql_fetch_array($result);

$result3 = mysql_query("SELECT * FROM topics WHERE fid = '$f' AND pinned = 'Yes' AND rid = '0' order by timestamp desc");
$forum_title = $r['title'];
?>

<b>Viewing Forum: <? echo $forum_title; ?></b><br /><br />

<a href="addthread.php?f=<? echo $f; ?>">Add Thread</a><br />

<table cellpadding="5" cellspacing="1" border="0" style="border:1px solid #000;">
  <tr style="background-color:#222;">
    <td>Thread</td>
    <td>Replies</td>
    <td>Views</td>
<?
if($mem[mod] == Yes){
echo "<td>Mod Actions</td>"; }else{} ?>
  </tr>
<?

while($r3 = mysql_fetch_array($result3))
{

   $thread_id3 = $r3['id'];
   $thread_title3 = $r3['title'];
   $thread_username3 = $r3['username'];
   $thread_last_post_username3 = $r3['last_post_username'];
   $thread_replies3 = mysql_num_rows(mysql_query("SELECT * FROM topics WHERE rid = '$thread_id3'"));
   $thread_views3 = $r3['views'];

$view = "SELECT * FROM users WHERE username = '$thread_username3' LIMIT 1";
$view2 = mysql_query($view);
$userid = mysql_fetch_assoc($view2);
$view3 = "SELECT * FROM users WHERE username = '$thread_last_post_username3' LIMIT 1";
$view4 = mysql_query($view3);
$userid2 = mysql_fetch_assoc($view4);
?>
  <tr>
    <td style="width:250px;background-color:#101010;">
    PINNED: <a href="viewthread.php?t=<? echo $thread_id3; ?>"><? echo $thread_title3; ?></a>
    <br />Started by: <b><a href=http://www.lordoftheabyss.com/player/view.php?id=<? echo "$userid[id]>$thread_username3"; ?></a></b>
    </td>
    <td style="width:75px;background-color:#101010;"><? echo $thread_replies3; ?></td>
    <td style="width:75px;background-color:#101010;"><? echo $thread_views3; ?></td>
<?
if($mem[mod] == Yes){
echo "<td width=150 bgcolor=#101010>
<a href=/delete.php?id=$r3[id]>Delete</a> | <a href=/unpin.php?id=$r3[id]>Unpin</a> | <a href=/edit.php?id=$r3[id]>Edit</a></td>"; }else{} ?>
  </tr>
<?
}
// for each row in our $result2 (the threads query), we display  the info. below.
while($r2 = mysql_fetch_array($result2))
{
   // redefine our thread row values
   $thread_id = $r2['id'];
   $thread_title = $r2['title'];
   $thread_username = $r2['username'];
   $thread_last_post_username = $r2['last_post_username'];
   $tr = mysql_query("SELECT * FROM topics WHERE rid = '$thread_id'");
   $thread_replies = mysql_num_rows($tr);
   $thread_views = $r2['views'];

$view5 = "SELECT * FROM users WHERE username = '$thread_username' LIMIT 1";
$view6 = mysql_query($view5);
$userid3 = mysql_fetch_assoc($view6);
$view7 = "SELECT * FROM users WHERE username = '$thread_last_post_username' LIMIT 1";
$view8 = mysql_query($view7);
$userid4 = mysql_fetch_assoc($view8);
?>
  <tr>
    <td style="width:250px;background-color:#101010;">
    <a href="viewthread.php?t=<? echo $thread_id; ?>"><? echo $thread_title; ?></a>
    <br />Started by: <b><a href=http://www.lordoftheabyss.com/player/view.php?id=<? echo "$userid3[id]>$thread_username"; ?></b>
    </td>
    <td style="width:75px;background-color:#101010;"><? echo $thread_replies; ?></td>
    <td style="width:75px;background-color:#101010;"><? echo $thread_views; ?></td>
<?
if($mem[mod] == Yes){
echo "<td width=150 bgcolor=#101010>
<a href=/forum/delete.php?id=$r2[id]>Delete</a> | <a href=/forum/pin.php?id=$r2[id]>Pin</a> | <a href=/forum/edit.php?id=$r2[id]>Edit</a></td>"; }else{} ?>
  </tr>
<?
}
?>
  <tr>
    <td style="background-color:#222;" colspan="5"> </td>
  </tr>
</table>
</td></table>

Link to comment
https://forums.phpfreaks.com/topic/45443-solved-pagination/
Share on other sites

I cannot take any credit for this at all, I found it previously here on phpfreaks in the tutorials I believe but the last time I looked it was not there ( i had misplaced my original download of this...lol)...I want to say Crayon Violent ut this together if I remember correctly...

 

It works great, just follow the comments along...

 

<?php 
/****** start of getting some variables figured out and querying db ********/ 
// connect to database 
$conn = mysql_connect('localhost','username','password') or die(mysql_error()); 
$db = mysql_select_db('dbname',$conn) or die(mysql_error()); 

// get the page number or set default 
$page = (is_numeric($_GET['page']) && $_GET['page'] > 0) ? $_GET['page'] : 1; 

// Define the number of results per page 
$max_results = 10; 

// Figure out the limit for the query based 
// on the current page number. 
$from = (($page * $max_results) - $max_results); 

// going to make some links to click in order for 
// user to sort by whichever column 
$allowed = array('list_id','list_order','list_name'); 
$sortby = (in_array($_GET['sortby'],$allowed)) ? $_GET['sortby'] : 'list_id'; 

// build and run the sql query   
$sql = "select * from test_list order by $sortby limit $from, $max_results"; 
$getlist = mysql_query($sql, $conn) or die(mysql_error()); 




// Figure out the total number of rows in table 
$sql = "select count(*) as num from test_list"; 
$getcount = mysql_query($sql, $conn) or die(mysql_error()); 
$total_results = mysql_result($getcount, 0) or die(mysql_error()); 

// Figure out the total number of pages we will have 
$total_pages = ceil($total_results / $max_results); 

// Figure out the current page result numbers 
$fr = $from + 1; 
$to = $from + mysql_num_rows($getlist); 
/******* end of get vars *******/ 

/***** generic crappy way to format the results.. pardon the messy  ***/ 
// display our "showing blah to blah of blah results" message 
echo "showing $fr to $to of $total_results results"; 

// output the table of info 
echo "<table border = '1'>"; 
echo "<tr>"; 
// echo the links for user to sort results by. This is setup so  
// that if you are on say page 5 and it is sorted by ID and you  
// click on 'ORDER' it will sort by order and but still show the 
// 5th page.  If you want it to start over at page 1 upon resorting 
// simply remove the page=$page in the link tags 
echo "<td><a href = '{$_SERVER['PHP_SELF']}?page=$page&sortby=list_id'>ID</a></td>"; 
echo "<td><a href = '{$_SERVER['PHP_SELF']}?page=$page&sortby=list_order'>ORDER</a></td>"; 
echo "<td><a href = '{$_SERVER['PHP_SELF']}?page=$page&sortby=list_name'>NAME</a></td>"; 
echo "</tr>"; 
// echo out the query results 
while ($list = mysql_fetch_array($getlist)) { 
   echo "<tr>"; 
   echo "<td>{$list['list_id']}</td>"; 
     echo "<td>{$list['list_order']}</td>"; 
     echo "<td>{$list['list_name']}</td>"; 
     echo "</tr>"; 
} // end while 
echo "</table>"; 
/***** end of result dump/format *****/  

/***** start of pagination *******/ 
// Build First and Prev links. If on page 1, we won't make links 
if($page > 1){ 
    $prev = ($page - 1); 
    echo "<a href='{$_SERVER['PHP_SELF']}?page=1&sortby=$sortby'>First</a> "; 
    echo "<a href='{$_SERVER['PHP_SELF']}?page=$prev&sortby=$sortby'>Prev</a> "; 
} // end if  

// build the links to the 2 previous and 2 next pages 
for($i = ($page - 2); $i <= ($page + 2); $i++){ 
   // only make a link if the prev/next is a valid page number  
   if (($i >= 1) && ($i <= $total_pages)) { 
     echo ($page == $i) ? "[$i] " : "<a href='{$_SERVER['PHP_SELF']}?page=$i&sortby=$sortby'>$i</a> ";  
   } // end if  
} // end for 
     
// Build Next and Last links. If on last page, we won't make a links 
if($page < $total_pages){ 
    $next = ($page + 1); 
    echo "<a href='{$_SERVER['PHP_SELF']}?page=$next&sortby=$sortby'>Next</a> "; 
    echo "<a href='{$_SERVER['PHP_SELF']}?page=$total_pages&sortby=$sortby'>Last</a>"; 
} // end if 
/***** end of pagination ******/ 

Link to comment
https://forums.phpfreaks.com/topic/45443-solved-pagination/#findComment-220676
Share on other sites

Alright maybe it isn't working  :-X

 

It is limiting the first page and is counting the pages right etc...but when I click the next page I get nohing but a table with no posts  ???

 

<?
session_start(); // Session Start
include ("db.php");
$email = $_COOKIE['email']; 
$password = $_COOKIE['password']; 
if((!$email) || (!$password)){ 
echo "Please enter ALL of the information! <br />"; 
include 'index.php'; 
exit(); 
} 

$sql = mysql_query("SELECT * FROM users WHERE email='$email'") or die(mysql_error());
$mem = mysql_fetch_array($sql);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" href="default.css"> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Viewing Forum</title>
</head>

<body bgcolor=black>
<?
$link = mysql_query("SELECT * FROM links") or die(mysql_error());
$qlink = mysql_fetch_array($link);
$sql = mysql_query("SELECT * FROM users WHERE email='$email'") or die(mysql_error());
$mem = mysql_fetch_array($sql);
?>
<center>
<?
include 'links.php';
?>
</center>
<table border=0 cellpadding=15 width=100%>
<td width=150 valign=top>
<?
include 'panel.php';
?>
</td>
<td valign=top>
<?

$f = $_GET['f'];

$result = mysql_query("select title from forums where id = '$f'");


$r = mysql_fetch_array($result);

$result3 = mysql_query("SELECT * FROM topics WHERE fid = '$f' AND pinned = 'Yes' AND rid = '0' order by timestamp desc");
$forum_title = $r['title'];
?>

<b>Viewing Forum: <? echo $forum_title; ?></b><br /><br />

<a href="addthread.php?f=<? echo $f; ?>">Add Thread</a><br />

<table cellpadding="5" cellspacing="1" border="0" style="border:1px solid #000;">
  <tr style="background-color:#222;">
    <td>Thread</td>
    <td>Replies</td>
    <td>Views</td>
<?
if($mem[mod] == Yes){
echo "<td>Mod Actions</td>"; }else{} ?>
  </tr>
<?

while($r3 = mysql_fetch_array($result3))
{

   $thread_id3 = $r3['id'];
   $thread_title3 = $r3['title'];
   $thread_username3 = $r3['username'];
   $thread_last_post_username3 = $r3['last_post_username'];
   $thread_replies3 = mysql_num_rows(mysql_query("SELECT * FROM topics WHERE rid = '$thread_id3'"));
   $thread_views3 = $r3['views'];

$view = "SELECT * FROM users WHERE username = '$thread_username3' LIMIT 1";
$view2 = mysql_query($view);
$userid = mysql_fetch_assoc($view2);
$view3 = "SELECT * FROM users WHERE username = '$thread_last_post_username3' LIMIT 1";
$view4 = mysql_query($view3);
$userid2 = mysql_fetch_assoc($view4);
?>
  <tr>
    <td style="width:250px;background-color:#101010;">
    PINNED: <a href="viewthread.php?t=<? echo $thread_id3; ?>"><? echo $thread_title3; ?></a>
    <br />Started by: <b><a href=http://www.lordoftheabyss.com/player/view.php?id=<? echo "$userid[id]>$thread_username3"; ?></a></b>
    </td>
    <td style="width:75px;background-color:#101010;"><? echo $thread_replies3; ?></td>
    <td style="width:75px;background-color:#101010;"><? echo $thread_views3; ?></td>
<?
if($mem[mod] == Yes){
echo "<td width=150 bgcolor=#101010>
<a href=/delete.php?id=$r3[id]>Delete</a> | <a href=/unpin.php?id=$r3[id]>Unpin</a> | <a href=/edit.php?id=$r3[id]>Edit</a></td>"; }else{} ?>
  </tr>
<?
}



$page = (is_numeric($_GET['page']) && $_GET['page'] > 0) ? $_GET['page'] : 1; 

// Define the number of results per page 
$max_results = 1; 

// Figure out the limit for the query based 
// on the current page number. 
$from = (($page * $max_results) - $max_results);  

// build and run the sql query   
$blah = "select * from topics where rid = '0' and fid = '$f' and pinned = 'No' limit $from, $max_results"; 
$getlist = mysql_query($blah, $connect) or die(mysql_error()); 




// Figure out the total number of rows in table 
$blah = "select count(*) as num from topics where rid = '0' and fid = '$f' and pinned = 'No'"; 
$getcount = mysql_query($blah, $connect) or die(mysql_error()); 
$total_results = mysql_result($getcount, 0) or die(mysql_error()); 

// Figure out the total number of pages we will have 
$total_pages = ceil($total_results / $max_results); 

// Figure out the current page result numbers 
$fr = $from + 1; 
$to = $from + mysql_num_rows($getlist); 
/******* end of get vars *******/ 

/***** generic crappy way to format the results.. pardon the messy  ***/ 
// display our "showing blah to blah of blah results" message 
echo "showing $fr to $to of $total_results results"; 

$result2 = mysql_query("SELECT * FROM topics WHERE fid = '$f' AND pinned = 'No' AND rid = '0' order by timestamp desc LIMIT $from, $max_results");


// for each row in our $result2 (the threads query), we display  the info. below.
while($r2 = mysql_fetch_array($result2))
{
   // redefine our thread row values
   $thread_id = $r2['id'];
   $thread_title = $r2['title'];
   $thread_username = $r2['username'];
   $thread_last_post_username = $r2['last_post_username'];
   $tr = mysql_query("SELECT * FROM topics WHERE rid = '$thread_id'");
   $thread_replies = mysql_num_rows($tr);
   $thread_views = $r2['views'];

$view5 = "SELECT * FROM users WHERE username = '$thread_username' LIMIT 1";
$view6 = mysql_query($view5);
$userid3 = mysql_fetch_assoc($view6);
$view7 = "SELECT * FROM users WHERE username = '$thread_last_post_username' LIMIT 1";
$view8 = mysql_query($view7);
$userid4 = mysql_fetch_assoc($view8);
?>
  <tr>
    <td style="width:250px;background-color:#101010;">
    <a href="viewthread.php?t=<? echo $thread_id; ?>"><? echo $thread_title; ?></a>
    <br />Started by: <b><a href=http://www.lordoftheabyss.com/player/view.php?id=<? echo "$userid3[id]>$thread_username"; ?></b>
    </td>
    <td style="width:75px;background-color:#101010;"><? echo $thread_replies; ?></td>
    <td style="width:75px;background-color:#101010;"><? echo $thread_views; ?></td>
<?
if($mem[mod] == Yes){
echo "<td width=150 bgcolor=#101010>
<a href=/forum/delete.php?id=$r2[id]>Delete</a> | <a href=/forum/pin.php?id=$r2[id]>Pin</a> | <a href=/forum/edit.php?id=$r2[id]>Edit</a></td>"; }else{} ?>
  </tr>
<?
}
?>
  <tr>
    <td style="background-color:#222;" colspan="5"> </td>
  </tr>
</table><?








/***** start of pagination *******/ 
// Build First and Prev links. If on page 1, we won't make links 
if($page > 1){ 
    $prev = ($page - 1); 
    echo "<a href='{$_SERVER['PHP_SELF']}?page=1'>First</a> "; 
    echo "<a href='{$_SERVER['PHP_SELF']}?page=$prev'>Prev</a> "; 
} // end if  

// build the links to the 2 previous and 2 next pages 
for($i = ($page - 2); $i <= ($page + 2); $i++){ 
   // only make a link if the prev/next is a valid page number  
   if (($i >= 1) && ($i <= $total_pages)) { 
     echo ($page == $i) ? "[$i] " : "<a href='{$_SERVER['PHP_SELF']}?page=$i'>$i</a> ";  
   } // end if  
} // end for 
     
// Build Next and Last links. If on last page, we won't make a links 
if($page < $total_pages){ 
    $next = ($page + 1); 
    echo "<a href='{$_SERVER['PHP_SELF']}?page=$next'>Next</a> "; 
    echo "<a href='{$_SERVER['PHP_SELF']}?page=$total_pages'>Last</a>"; 
} // end if 
/***** end of pagination ******/ 





?>
</td></table>
<center>
<?
include 'links.php';
?>
</center>

Link to comment
https://forums.phpfreaks.com/topic/45443-solved-pagination/#findComment-220696
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.