Kamis, 04 Oktober 2012

PHP Paging Web Cara IV

Ini cara buat PAGING WEB cara IV
*BUAT DATA BASE NAMANYA "PAGING"
*BUAT TABEL DENGAN NAMA "BUKUTAMU"
INI CODING BUAT TABELNYA :
--
-- Table structure for table `bukutamu`
--

CREATE TABLE IF NOT EXISTS `bukutamu` (
`id` int(6) NOT NULL,
`nama` varchar(30) NOT NULL,
`email` varchar(25) default NULL,
`komentar` varchar(220) default NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

--
-- Dumping data for table `bukutamu`
--

INSERT INTO `bukutamu` (`id`, `nama`, `email`, `komentar`) VALUES
(1, 'joko', 'joko@mail.com', 'mantap'),
(2, 'santoso', 'santoso', 'ajeb'),
(3, 'dian', 'dian@mail.com', 'siip'),
(4, 'madun', 'madun@yamail', 'oke bro'),
(5, 'marto', 'marto@mail.com', 'cucok'),
(6, 'nita', 'nita@mail.com', 'keren om'),
(7, 'sunaryo', 'sunaryo@mail.com', 'yoi mas dicoba dulu')
SELANJUTNYA KITA BUAT CODING PHPNYA
INI CODINGNYA
BUAT FILE PERTAMA DENGAN NAMA FILE KONEKSI.INC.PHP

INI CODINGNYA
<?php
/* koneksi database */

$host="localhost";
$user="FALAH";
$pass="MASTER";
$db="paging";

mysql_connect($host,$user,$pas s);
mysql_select_db ($db);

?
BUAT FILE KEDUA DENGAN NAMA FILE PAGING.PHP

INI CODINGNYA

<?php
require_once("koneksi.inc.php");
//Example of use:
$sql = "SELECT * FROM bukutamu";
$pager = new pager($sql,'page',3);
while($row = mysql_fetch_array($pager->result))
{
echo $row['id']." ".$row['nama']."<br>";
}
echo $pager->show();

class pager
{
var $sql;
var $getvar;
var $rows;
var $start;
var $getvar_val;
var $pager;
var $result;
function __construct($sql,$getvar,$length)
{
$this->result=null;
$this->sql = $sql;
$this->getvar = $getvar;
$this->rows = 0;
$this->start = 0;
$this->getvar_val = 1;
$this->pager = null;
$this->SetLength($length);
$this->GetStart();
$this->doQuery();
}
//Sets $length
function SetLength($length)
{
$this->length = (int)$length;
if($this->length<0)
{
$this->length = 0;
}
}
function doQuery()
{
$sql = trim($this->sql);
$sql = substr($sql,6);
$sql = 'SELECT SQL_CALC_FOUND_ROWS '.$sql.' limit '.$this->start.', '.$this->length;
$this->result = mysql_query($sql);
$sql = "SELECT FOUND_ROWS()";
$result = mysql_query($sql);
$this->rows = mysql_result($result,0);
}
//getvar_val() gets the
//$getvar_val is a positive integer – > 0
function Set_Getvar_val()
{
$this->getvar_val = (int)$_GET[$this->getvar];
if($this->getvar_val<1)
{
$this->getvar_val = 1;
}
}
//Gets the start of the data
function GetStart()
{
$this->Set_Getvar_val();
$this->start = (($this->getvar_val-1)*$this->length);
}
function show($next="Next",$last="Last",$separator="")
{
$array = $this->pager();
$str = array();
foreach($array as $k => $v)
{
if($k!='next'&&$k!='last')
{
if($k==$this->getvar_val)
{
$str[] = $k;
}
else
{
$str[] = '<a href="'.$v.'">'.$k.'</a>';
}
}
}
$str = implode($separator, $str);
$rt = $array['last']===null?"":'<a href="'.$array['last'].'">'.$last.'</a>'.$separator;
$rt .= $str.$separator;
$rt .= $array['next']===null?"":'<a href="'.$array['next'].'">'.$next.'</a>';
return $rt;
}
//Returns an array of the links arround the given point
//['next'] => next page
//['last'] => last page
function pager($margin=10)
{
$path = $_GET;
$newpath = $PHP_SELF."?";
foreach($path as $key => $value)
{
if($key!=$this->getvar)
{
$newpath .= $key."=".$value;
$newpath .="&amp;";
}
}
$newpath .= $this->getvar;
$linkpaths = array();
$current = $this->start / $this->length + 1;
$pages = ceil(($this->rows/$this->length));
$pagerstart = $current-$margin;
$pagerstart = ($pagerstart<1)?1:$pagerstart;
$pagerend = $current+$margin;
$pagerend = ( $pagerend > $pages ) ? ( ceil( ( $this->rows / $this->length ) ) ): $pagerend;
for($i=$pagerstart;$i < ($pagerend+1);$i++)
{
$linkpaths[$i] = $newpath."=".($i);
}
if($linkpaths[($current+1)]!=null)
{
$linkpaths['next']=$linkpaths[($current+1)];
}
if($linkpaths[($current-1)]!=null)
{
$linkpaths['last']=$linkpaths[($current-1)];
}
return $linkpaths;
}
}
?>
oke ini hasilnya jika di jalankan

Tidak ada komentar:

Posting Komentar