Generating Serial Page Numbers in PHP [Pager]
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:
$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";
?>












Comments
Post new comment