Jump to content

Work out number of Pages?


RaythMistwalker

Recommended Posts


<?php
  $ThreadID = $_GET['threadid'];
  If (isset($_GET['page'])) { $Page = intval($_GET['page']); }
  Elseif (isset($_POST['page'])) { $Page = intval($_POST['page']); }
  Else { $Page = 1; }
  $ThreadQry = "SELECT * FROM ".FORUM_THREADS." WHERE thread_id='{$ThreadID}'";
  $ThreadRes = mysql_query($ThreadQry, $db);
  $ThreadTitle = mysql_result($ThreadRes, 0, 'thread_title');
  $ForumID = mysql_result($ThreadRes, 0, 'forum_id');
  $Locked = mysql_result($ThreadRes, 0, 'locked');
  
  If ($Page == 1) { $Limit = 0; }
  Else { $Limit = $Page * 20; }
  $Limit2 = $Limit + 20;
  $PostsTotal = mysql_num_rows(mysql_query("SELECT * FROM ".FORUM_POSTS." WHERE thread_id='{$ThreadID}'", $db));
  $PostsQry = "SELECT * FROM ".FORUM_POSTS." WHERE thread_id='{$ThreadID}' ORDER BY  `post_id` ASC LIMIT {$Limit}, {$Limit2}";
  $PostsResult = mysql_query($PostsQry, $db);
  $PostsOnPage = mysql_num_rows($PostsResult);
  $PostCounter = 0;
  
  $PermissionQuery = "SELECT * FROM ".PERMISSIONS." WHERE forum_id='".$ForumID."'";
  $PermissionResult = mysql_query($PermissionQuery, $db);
  $PermissionPostReply = mysql_result($PermissionResult, 0, 'min_post_reply');
  $PermissionReply = intval($PermissionPostReply);
?>
<div class='tableborder'>
    <?php if ($MemLevel >= $PermissionReply) { 
        If ($Locked == 'no') {
    ?>
    <div align="right" style="background-color: #979797">
        <a href="./index.php?act=reply&threadid=<?php echo $ThreadID; ?>"><img src="./images/reply.png"></a>
    </div>
    <?php }
    Else {
        ?>
        <div align="right" style="background-color: #979797">
        <?php If ($MemLevel >= 1000) { Echo "<a href='./index.php?act=reply&threadid={$ThreadID}'>"; } ?><img src="./images/locked_reply.png"> <?php If ($MemLevel >= 1000) { Echo "</a>"; } ?><br>
    </div>
    <?php
    }
    }
    ?>
<div class='maintitle' align='left'><img src='./images/nav_m.gif' border='0' alt='' width='8' height='8'> <a href="./index.php?act=viewthread&threadid=<?php echo $ThreadID; ?>"><?php echo $ThreadTitle; ?></a></div>
<table width="100%" border="0" cellspacing="1" cellpadding="4">
    <tr>
        <th align="Center" width="20%" class='titlemedium'>Author</th>
        <th align="Left" width="80%" class='titlemedium'>Post</th>
    </tr>
<?php
  While ($PostCounter < $PostsOnPage) {
?>
    
<?php  
  $PostCounter++;   
  }
?>
</table>
</div>

This is what I have so far for displaying a thread in a forum. This is set to retrieve 20 Posts per page for each thread.

What I want to do is At the bottom under </table> is display a drop down box which will add a selection for each page of posts. How would I work out how many pages this drop down box needs to add?

 

The posts table is stored in a constant called FORUM_POSTS and they are sorted into threads by thread_id in that table.

Link to comment
Share on other sites

Well, your question aside, you have some problems in the current logic. You already run a query to get the total posts - but you are running a query to get all fields from all records!!! Then only using mysql_num_rows. 1) Only query for the fields you need (i.e. don't use '*' for your select. 2) if you only need the number of rows then use COUNT() in the query so the database doesn't have to get all the data - it will calculate the count for you. There are tons of tutorials on how to implement pagination correctly - there is even one on this site (http://www.phpfreaks.com/tutorial/basic-pagination).

 

You are also not sanitizing the user input and are open to SQL Injection attacks.

 

Plus, you should be using the value of all the records to determine the maximum pages and using that to also validate the user selected page. Anyway, once you have the total records you simply divide that number by the records per page to get the number of pages - of course you need to use ceil() on that value to round up though.

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.