Generating Serial Page Numbers in PHP [Pager]

Tagged:  

Hello friends,

In this tutorial we explain how to generate page numbers serially and store them in a file called pages.html

You can further use this tutorial to implement this technique in your CMS [if you are building one :D]

In this tutorial we count the no. of articles in the database and divide it by 5 which implies that we display 5 articles per page. (we take intval() of the result).

This code will generate page nos as

Page 1

Page 2

....so on

Enjoy!

The code:

 

<?php

$db = mysqli_connect("localhost","user","password","database");

if ( mysqli_errno($db) ) {

die(mysqli_error($db));

}

$count = mysqli_query($db,"SELECT COUNT(*) FROM content");

if ( mysqli_errno($db) ) {

die(mysqli_error($db));

}

$r = mysqli_fetch_row($count);

$pg_no = intval($r[0]/5);

$htmlfile = fopen("pages.html", "w") or die("Error occurred while opening file");

for ( $i = 0; $i != $pg_no; $i++ ) {

$str = "<a href=home.html?p=".($i+1).">Page ".($i+1)."<br>";

fwrite($htmlfile, $str);

}

fclose($htmlfile);

print "\n";

?>

Trackback URL for this post:

http://www.itech7.com/trackback/132

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <h1><h2><h3><h4><h5><h6><b><i><u><style><div><span><li><ol><ul><img><s><em><strong><a><address><blockquote><table><td><tr><th><caption><p><br><pre><code><fn><footnotes><dl><dt><dd><font>
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Use [fn]...[/fn] (or <fn>...</fn>) to insert automatically numbered footnotes.

More information about formatting options

CAPTCHA
This is to verify that you are human visitor
10 + 6 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.

Syndicate content
All trademarks and copyrights on this page are owned by their respective owners. Comments are owned by the Poster. Rest Copyright © iTech7.com

For License and Copyrights see http://www.itech7.com/Content-Copyrights