Artikel kali ini akan membahas bagaimana membuat sebuah paging halaman seperti

< previous 1 2 3 next >

Ok langsung aja sebelujmnya buat sebuah tabel berita

CREATE TABLE berita (
id int NOT NULL auto_increment,
judul varchar(50) collate latin1_general_ci NOT NULL default ”,
isi longtext collate latin1_general_ci NOT NULL,
PRIMARY KEY  (id)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=5 ;

Setelah tabel jadi buat script untuk koneksi ke database koneksi.php

<?php
$dbhost = ‘localhost’;
$dbusername = ‘root’;
$dbpasswd = ‘’;
$database_name = ‘nama_database’;

@$connection = mysql_connect(”$dbhost”,”$dbusername”,”$dbpasswd”) or die

(”MySql Tidak dapat koneksi”);
@$db = mysql_select_db(”$database_name”, $connection) or die(”Database tidak ditemukan”);
?>

setelah itu buat script yang mengatur paging pagenya

<?php

include ‘koneksi.php’;

$table = berita;

$hal = $_GET[hal];

if(!isset($_GET[‘hal’])){
$page = 1;
} else {
$page = $_GET[‘hal’];
}

/*  jumlah item per halaman */
$max_results = 5;

$from = (($page * $max_results) – $max_results);

$sql = mysql_query(”SELECT * FROM $table ORDER BY id DESC LIMIT $from, $max_results “);
while($row = mysql_fetch_array($sql)){

?>


<?php
echo $row[2] ?><br>
<?php echo $row[3] ?></a><br />
<?php echo $row[1] ?> | Halaman Ini dibaca <?php echo $row[6] ?> kali<br />
<?php echo $row[4]; ?>

<hr>
<?php
}
$total_results = mysql_result(mysql_query(”SELECT COUNT(*) as Num FROM $table”),0);

$total_pages = ceil($total_results / $max_results);

echo “<center>Select a Page<br />”;

if($hal > 1){
$prev = ($page – 1);
echo “<a href=$_SERVER[PHP_SELF]?hal=$prev> <-Previous </a> “;
}

for($i = 1; $i <= $total_pages; $i++){
if(($hal) == $i){
echo “$i “;
} else {
echo “<a href=$_SERVER[PHP_SELF]?hal=$i>$i</a> “;
}
}

if($hal < $total_pages){
$next = ($page + 1);
echo “<a href=$_SERVER[PHP_SELF]?hal=$next>Next-></a>”;
}
echo “</center>”;
?>