The list of all PHP and Mysql posts

How to show page number for a forum

Page numbers are one of the most interesting php powers , in the following image you can see an example for page numbering that allows the user to see sufficient number of results on each page.


Add the following function to the file that you store your functions on it (for example funcs.php) and add the file by an include to any page that you want to show page numbering on it.

the function codes:

function get_pages_html()
  {
    $p = $_GET["p"];
    $page_num = $GLOBALS['page_num'] +1; // if you have downloaded "my forum codes" , there's an error in it on this line , kindly correct it from here.

    if ($p == "")
      $p = 1;
$begin_page = 1;
if($p == 1 or $p == 2 or $p == 3){
 $begin_page = 1;
}else{
 $begin_page = $p-3;
}
if($p >= $page_num-3){
 $end_page = $page_num;

}else{

 $end_page = $begin_page+5;
}

    $html = "page $p from $page_num   ";

    if ($page_num > 1)

      {

      for ($i=$begin_page; $i<= $end_page; $i++)
        {
        if ($i == $p)
          $html .= "$p  ";
        else
          {
          $link = $_GET["link"];
          $url ="showforum.php?link=$link&p=$i";

          $html .= "<a href= ".$url." >".$i."</a> ";

          }
        }
      } 
    return $html;

  }

error_reporting( 0);
?>

The codes of this function are easy to understand so I explain just some points. This function produces an html code that shows the image above.

First in the codes:

$html = "page&nbsp;$p&nbsp;from&nbsp;$page_num&nbsp;&nbsp;&nbsp;";


Here , we produce a format like "page 2 from 6" , the number of pages "$page_num" is a parameter that is sent from the place that the function is summoned , the current page number "$p" can be sent to the function or in its default value is obviosly equal to one.

Second in the codes :

    if ($page_num > 1)

      {

      for ($i=$begin_page; $i<= $end_page; $i++)
        {
        if ($i == $p)
          $html .= "$p  ";
        else
          {
          $link = $_GET["link"];
          $url ="showforum.php?link=$link&p=$i";

          $html .= "<a href= ".$url." >".$i."</a> ";

          }
        }
      } 


We show the page numbers in the shape of hyperlink that direct the user to the other pages results. Link variable "$link" is an other thing that is sent from the place that the function is summoned.
The produced hyperlink for each page number directs the user to the same page (here , it is "showforum.php" , but it also can be replaced by "search.php" or any other page that wants to summon the function) , that the function (get_pages_html) is summoned in it.

Now the function "get_pages_html" is ready to summoned on the page "showforum.php" . These are the codes in the page "showforum.php" related to page numbering action. (I describe "showforum.php" codes totally in an other post ).
The codes related to page numbering on "showforum.php" :

codes:

<?php
include "auth.inc"; // test the login

include "funcs.php"; // include the file contain the function "get_pages_html"

$forumlink = $_GET["link"]; // the field of the forum that is sent from the index page

connect();

$sql = "SELECT * FROM topics WHERE field='$forumlink' "; // extract all of the posts in the forum field

$SearchResult = mysql_query($sql);

$TotalResults = mysql_num_rows($SearchResult);

$num_show_post = 4; // select an even number for the number of posts are showed in a page

if($TotalResults%2 == 0){

// based on the number of results we calculate the number of pages that is needed to show the results

$page_num = intval($TotalResults/$num_show_post); // "intval" returns the integer value of its variable

}else{
$page_num = intval($TotalResults/$num_show_post)+1;
}
$p = $_GET["p"]; // the current page number

$page_num = $GLOBALS['page_num']; // this line is not needed , you can omit it , it changes "$page_num" into a global variable.

if ($p == "")
$p = 1;
$counter = $TotalResults - (($p-1)*$num_show_post); // based on the page number and the number of posts to show on a page , we calculate this counter

for($i = $counter - 1; $i != $counter - $num_show_post -1; $i-- )
/
/ by the calculated counter , show the appropriate number of posts on a page

{
$TextTitle = mysql_result($SearchResult, $i, 3);
$Textauthor = mysql_result($SearchResult, $i, 0);
$Textlink = mysql_result($SearchResult, $i, 5);

echo "<a href=\"showtopic.php?Textlink=" .$Textlink. " \">" . $TextTitle . "</a>"; // make a hyper link to the post that is clicked

echo "<br> <br>";
}
?>
 <?php
 echo get_pages_html(); // the page number function "get_pages_html" is summoned here.
 ?>

Enough comments has been added to the codes so there's no more need to explain.
If you don't want to go over all of my weblog you may need to read the following posts to understand the current post :

http://webprogram4beginners.blogspot.com/2011/02/download-php-codes-of-complete-forum.html

http://webprogram4beginners.blogspot.com/2011/02/how-to-add-variable-to-url-adress.html

http://webprogram4beginners.blogspot.com/2011/02/php-orders-to-work-with-database-and.html

1 comment:

  1. Here is the super extra 20x fast server and cheap best and cheapest web hosting service to grow your big and small business on all search engine & get more sales or traffic, 70% blast discount, 24 hour live chat support.

    ReplyDelete