Jump to content

[SOLVED] Pagination


supanoob

Recommended Posts

ok so i have the following code and it all seems to work the only problem is it will only find one result, now to test i set the limit at 1 and inserted 3 fields manually it will echo one result but wont give me the option to get onto another 2 pages i echoed the total rows and it is only counting 1 any idea why?

 

<?php

if ($_GET['step'] == 'view_board')
{
    $limit      = 1;               
    // Sets how many results shown per page
    $board_id=$_GET['board_id'];
    $query_count    = "SELECT count(*) FROM tord_forums where posted_in='$board_id'";    
    // Sets what we want to pull from the database
    // count(*) is better for large databases (thanks Greg!)
    echo "$query_count<br>";
    $result_count   = mysql_query($query_count);    
    // Pulls what we want from the database
    echo "$result_count<br>";
    $totalrows  = mysql_num_rows($result_count);    
    // This counts the number of users 
    echo "$totalrows<br>";

echo "<p align=\"center\">Forums >> Announcements<br>";


    
echo "</p>
<table border=\"0\" width=\"100%\" id=\"table1\">
<tr>
	<td width=\"50%\" align=\"center\" bgcolor=\"#838383\">Topic Subject</td>
	<td width=\"20%\" align=\"center\" bgcolor=\"#838383\">Started By</td>
	<td width=\"10%\" align=\"center\" bgcolor=\"#838383\">Views</td>
	<td width=\"10%\" align=\"center\" bgcolor=\"#838383\">Replies</td>
	<td width=\"20%\" align=\"center\" bgcolor=\"#838383\">Options</td>
</tr>";


if(empty($page)){    // Checks if the $page variable is empty (not set)
        $page = 1;      // If it is empty, we're on page 1
    }

    $limitvalue = $page * $limit - ($limit); 
    // Ex: (2 * 25) - 25 = 25 <- data starts at 25
    $board_id=$_GET['board_id'];
    $query  = "SELECT post_id, post_topic, post_views, post_replies, posted_by, sticky, deleted, reply_to, post_type FROM tord_forums where posted_in='$board_id' and deleted='no' and post_type='topic' LIMIT $limitvalue, $limit";        
    $result = mysql_query($query) or die("Error: " . mysql_error()); 
    // Selects all the data from table.
    // mysql_error() will print an error if one occurs.
    
    /* Tip: The MySQL LIMIT value syntax is as follows: 
    
    LIMIT $row_to_start_at, $how_many_rows_to_return 
    
    */ 

                      while($row = mysql_fetch_array($result)){
                      $post_id=($row['post_id']);
                      $post_topic=($row['post_topic']);
                      $post_replies=($row['post_replies']);
                      $posted_by=($row['posted_by']);
                      $sticky=($row['sticky']);
                      $deleted=($row['deleted']);
                      $reply_to=($row['reply_to']);
                      $post_type=($row['post_type']);
                      
                      echo "<tr>
	<td bgcolor=\"#A7A7A7\" width=\"50%\">
	$post_topic</td>
	<td width=\"20%\" align=\"center\" bgcolor=\"#A7A7A7\">$posted_by_name</td>
	<td width=\"10%\" align=\"center\" bgcolor=\"#A7A7A7\">$post_views</td>
	<td width=\"10%\" align=\"center\" bgcolor=\"#A7A7A7\">$post_replies</td>
	<td width=\"20%\" align=\"center\" bgcolor=\"#A7A7A7\">[d][s][us]</td>
</tr>";
}


echo"</table>";

if($page != 1){ 
        $pageprev = $page--;
        
        echo("[<a href=\"forum.php?step=view_board&board_id=1&page=$pageprev\">Previous</a>|"); 
    }else{
        echo("[Previous|");
    }

    $numofpages = $totalrows / $limit; 
    
    for($i = 1; $i <= $numofpages; $i++){
        if($i == $page){
            echo("$i|");
        }else{
            echo("<a href=\"forum.php?step=view_board&board_id=1?page=$i\">$i</a>|");
        }
    }


    if(($totalrows % $limit) != 0){
        if($i == $page){
            echo("$i|");
        }else{
            echo("<a href=\"forum.php?step=view_board&board_id=1?page=$i\">$i</a>|");
        }
    }

    if(($totalrows - ($limit * $page)) > 0){
        $pagenext = $page++;
         
        echo("<a href=\"forum.php?step=view_board&board_id=1?page=$pagenext\">Next</a>]"); 
    }else{
        echo("Next]"); 
    }
    
    mysql_free_result($result);

echo "<br><a href=\"forum.php?step=start_topic\">Start Topic</a><br><br>
  Options Key: [d] = Delete [s] = Sticky [us] = un-sticky";

}

?>

Link to comment
https://forums.phpfreaks.com/topic/44373-solved-pagination/
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.