Jump to content

Pagination Help


Unholy Prayer

Recommended Posts

I can't figure out how to get this pagination to work.  I got it from a tutorial.  It says the mysql_fetch_array is not a valid MySQL Resource.  This is my code:

<?php 

$tid = $_GET["id"];

require_once('config.php');

//If the thread is viewed...
if ($_GET['id']){

//update views in database
mysql_query("UPDATE vbb_threads SET tviews = tviews+1 WHERE tid = $tid");

}

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


echo "<div><a href='index.php'>Index</a> -> <a href='viewforum.php?id=$fid'>$fname</a> -> <a href='viewthread.php?id=$tid'>$threadname</a><br><br>

<a href='addreply.php?tid=$tid'>Add Reply</a></div>
        <table align='center' width='65%' cellspacing='1' cellpadding='1' border='0'>
   <tr>
     <td align='center' colspan='2' width='100%' class='ctop'>Viewing $tname</td>
   </tr><tr>";

$bb_replace = array('[b]', '[/b]', '[i]', '[/i]', '[u]', '[/u]');
$bb_replacements = array ('<b>', '</b>', '<i>', '</i>', '<u>', '</u>');

$threads = mysql_query("SELECT * FROM vbb_threads where tid = $tid");

while($t=mysql_fetch_array($threads))

{

   $tcreator=$t["tcreator"];
   $tcontent=$t["tcontent"];
   $tdatetime=$t["tdatetime"];
   $tsubject=$t["tsubject"];

    $tcontent = str_replace($bb_replace, $bb_replacements, $tcontent);

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

}

//Start pagination.
$limit = 10;               
    $query_count    = "SELECT count(*) FROM vbb_replies WHERE tid = $tid";    
    $result_count   = mysql_query($query_count);    
    $totalrows      = mysql_num_rows($result_count); 

    if(empty($page)){
        $page = 1;
    }
        

    $limitvalue = $page * $limit - ($limit); 
    $replies  = "SELECT * FROM vbb_replies WHERE tid = $tid LIMIT $limitvalue, $limit";        
    $result = mysql_query($replies) or die("Error: " . mysql_error()); 

    if(mysql_num_rows($result) == 0){
        echo("Nothing to Display!");
    }

    $bgcolor = "#f9f5b8"; 

while($r=mysql_fetch_array($replies))

{

   $rauthor=$r["rauthor"];
   $rcontent=$r["rcontent"];
   $rdatetime=$r["rdatetime"];

   $rcontent = str_replace($bb_replace, $bb_replacements, $rcontent);

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

}
  
echo "</table>";
    if($page != 1){ 
        $pageprev = $page--;
        
        echo("<a href=\"$PHP_SELF&page=$pageprev\">PREV".$limit."</a> "); 
    }else{
        echo("PREV".$limit." ");
    }

    $numofpages = $totalrows / $limit; 
    
    for($i = 1; $i <= $numofpages; $i++){
        if($i == $page){
            echo($i." ");
        }else{
            echo("<a href=\"$PHP_SELF?page=$i\">$i</a> ");
        }
    }


    if(($totalrows % $limit) != 0){
        if($i == $page){
            echo($i." ");
        }else{
            echo("<a href=\"$PHP_SELF?page=$i\">$i</a> ");
        }
    }

    if(($totalrows - ($limit * $page)) > 0){
        $pagenext = $page++;
         
        echo("<a href=\"$PHP_SELF?page=$pagenext\">NEXT".$limit."</a>"); 
    }else{
        echo("NEXT".$limit); 
    }
    
    mysql_free_result($result);
//End pagination.


?>

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

//your problem is here I believe

$replies  = "SELECT * FROM vbb_replies WHERE tid = $tid LIMIT $limitvalue, $limit";        
    $result = mysql_query($replies) or die("Error: " . mysql_error()); 

    if(mysql_num_rows($result) == 0){
        echo("Nothing to Display!");
    }

    $bgcolor = "#f9f5b8"; 

while($r=mysql_fetch_array($replies)) // thats because this is your query variable and not your result source variable

//change the above line to read
while($r=mysql_fetch_array($result))

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