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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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