Jump to content

Pagination Help


ShoeLace1291

Recommended Posts

I am having problems with my pagination code.  The limit is 15 and there are 16 threads in the selected forum but it's not showing a link to the next page and it's displaying all 16 threads.  This is my code:

<?php
$action = $_GET["action"];

$m_time = explode(" ",microtime()); 
$m_time = $m_time[0] + $m_time[1]; 
$starttime = $m_time;

ob_start();
require_once('config.php');

session_start(); // Use session variable on this page. This function must put on the
include('constants.php'); 

if(!session_is_registered("username")){

      $user = "Guest";

  }

if(session_is_registered("username")){

     $user = $_SESSION['username'];

    }

include('templates/default/header_body.tpl');

$fid = $_GET["id"];

$getfname = mysql_query("SELECT * FROM vbb_forums WHERE fid = $fid");

while($f=mysql_fetch_array($getfname))

{

  $fname=$f["fname"];

}


echo "<div><p align='left'><a href='index.php'>Index</a> -> <a href='viewforum.php?id=$fid'>$fname</a><br><br><a href='addthread.php?fid=$fid'>Create Thread</a></p></div>";

//Start pagination
$q = mysql_query("SELECT * FROM `vbb_threads` WHERE fid = $fid ORDER BY tid DESC LIMIT 15");
  if(!$q) die(mysql_error());  
$total_items = mysql_num_rows($q);


  $limit = 15; 

   if((!$limit)  || (is_numeric($limit) == false) || ($limit < 10) || ($limit > 50)) {
     
    } 

   if((!$page) || (is_numeric($page) == false) || ($page < 0) || ($page > $total_items)) {
      $page = 1; //default
    } 

    $total_pages = ceil($total_items / $limit);
    $set_limit = $page * $limit - ($limit);

    $prev_page = $page - 1;

    if($prev_page >= 1) { 
       echo("<b><</b> <a href='viewforum.php?id=$fid&page=$prev_page><b><--</b></a>"); 
} 

  for($a = 1; $a <= $total_pages; $a++)
  {
   if($a == $page) {
      echo("<b> $a</b> | "); //no link
 } else {
  echo("  <a href=viewforum.php?id=$fid&page=$a> $a </a> | ");
     } 
} 

  $next_page = $page + 1;

  if($next_page <= $total_pages) {
   echo("<a href='viewforum.php?id=$fid&page=$next_page'><b>--></b></a> > >"); 
} 
//End Pagination

echo "<table align='center' cellspacing='1' cellpadding='1' border='0' width='75%'>
    <tr>
       <td align='left' width='100%' colspan='5'></td>
   </tr><tr>
       <td colspan='5' height='25' class='header'>$fname</td>
   </tr><tr>
       <td width='10%' class='title_threads'> </td>
       <td width='35%' class='title_threads''>Subject/Creator</td>
       <td width='10%' class='title_threads''>Views</td>
       <td width='10%' class='title_threads''>Replies</td>
       <td width='35%' class='title_threads''>Last Post</td>
    </tr><tr>";

//Are there any categories in this program?
$get_threads = "SELECT * FROM vbb_threads where fid = $fid";
$threads_result = mysql_query($get_threads);
$num_threads = mysql_num_rows($threads_result);

if($num_threads < 1)

{

    echo "<td colspan='5' class='forumrow'>There are currently no threads in this forum.</td>";

}
   
   while($t=mysql_fetch_array($q))

   {

      $tcreator=$t["tcreator"];
      $tsubject=$t["tsubject"];
      $tid=$t["tid"];
      $tviews=$t["tviews"];
      $tlastpost_auth=$t["tlastpost_auth"];
      $tlastpost_datetime=$t["tlastpost_datetime"];

     $get_replies = "SELECT * FROM vbb_replies where tid = $tid";
     $replies_result = mysql_query($get_replies);
     $replies = mysql_num_rows($replies_result);

     include('templates/default/threadlist_body.tpl');

    }

   echo "</table>";

  include('templates/default/footer_body.tpl');

?>

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

There are a lot of problems in the code above.

 

1. You do a query to get a total_count, but in that query you have a hard coded limit of 15, so the total_count will always be 15. you then use that count to create your limit variable - but you never use it!

 

2. When checking the page number you have this in the comparrison: $page > $total_items. You should be checking to see if page is greater than total_pages - not the number of items.

 

3. HTML error in this line: echo("<b><</b> <a href='viewforum.php?id=$fid&page=$prev_page><b><--</b></a>");

 

This site has a great pagination tutorial. I suggest you use it.

Link to comment
https://forums.phpfreaks.com/topic/46784-pagination-help/#findComment-228055
Share on other sites

Ok, I tried the tutorial from this site, but now the sql records are not showing up.  The links are showing up fine, but it doesn't display the sql records.  This is my code:

<?php
$action = $_GET["action"];

$m_time = explode(" ",microtime()); 
$m_time = $m_time[0] + $m_time[1]; 
$starttime = $m_time;

ob_start();
require_once('config.php');

session_start(); // Use session variable on this page. This function must put on the
include('constants.php'); 

if(!session_is_registered("username")){

      $user = "Guest";

  }

if(session_is_registered("username")){

     $user = $_SESSION['username'];

    }

include('templates/default/header_body.tpl');

$fid = $_GET["id"];

$getfname = mysql_query("SELECT * FROM vbb_forums WHERE fid = $fid");

while($f=mysql_fetch_array($getfname))

{

  $fname=$f["fname"];

}


echo "<div><p align='left'><a href='index.php'>Index</a> -> <a href='viewforum.php?id=$fid'>$fname</a><br><br><a href='addthread.php?fid=$fid'>Create Thread</a></p></div>";

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

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

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

// Perform MySQL query on only the current page number's results

$sql = mysql_query("SELECT * FROM vbb_threads LIMIT $from, $max_results WHERE fid = $fid");

    echo "<table align='center' cellspacing='1' cellpadding='1' border='0' width='75%'>
    <tr>
       <td align='left' width='100%' colspan='5'></td>
   </tr><tr>
       <td colspan='5' height='25' class='header'>$fname</td>
   </tr><tr>
       <td width='10%' class='title_threads'> </td>
       <td width='35%' class='title_threads''>Subject/Creator</td>
       <td width='10%' class='title_threads''>Views</td>
       <td width='10%' class='title_threads''>Replies</td>
       <td width='35%' class='title_threads''>Last Post</td>
    </tr><tr>";

//Are there any threads in this forum?
$getthreads = "SELECT * FROM vbb_threads WHERE fid = $fid";
$threads_result = mysql_query($getthreads);
$num_threads = mysql_num_rows($threads_result);

//If no, display a message.
if($num_threads < 1)

{

    echo "<td colspan='5' class='forumrow'>There are currently no threads in this forum.</td>";

} 



while($t=mysql_fetch_array($sql)){
    // Build your formatted results here.
   
   

      $tcreator=$t["tcreator"];
      $tsubject=$t["tsubject"];
      $tid=$t["tid"];
      $tviews=$t["tviews"];
      $tlastpost_auth=$t["tlastpost_auth"];
      $tlastpost_datetime=$t["tlastpost_datetime"];

     $get_replies = "SELECT * FROM vbb_replies where tid = $tid";
     $replies_result = mysql_query($get_replies);
     $replies = mysql_num_rows($replies_result);

     include('templates/default/threadlist_body.tpl');

    }

  


   echo "</table>";


// Figure out the total number of results in DB:
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM vbb_threads"),0);

// Figure out the total number of pages. Always round up using ceil()
$total_pages = ceil($total_results / $max_results);

// Build Page Number Hyperlinks
echo "<center><b>Page:</b>";

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

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

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


  include('templates/default/footer_body.tpl');

?>

Link to comment
https://forums.phpfreaks.com/topic/46784-pagination-help/#findComment-228856
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.