Membuat Function CRUD Plus Paging di PHP

  • 2016-10-28 20:32:52
  • 1079

Silahkan host, username, password, database, tabel disesuaikan dengan pengaturan database anda.
Limit digunakan untuk menampilkan jumlah data per halaman.

<?php ob_start();
error_reporting(0);
$host = "localhost"; 
$username = "root";
$password = "";
$database = "";
$tabel = "";
 
mysql_connect($host, $username, $password)or die("cannot connect");
mysql_select_db($database)or die("cannot select DB");
 
crud($tabel);
 
function crud($tabel='')
{
if ((!isset($_GET['delete'])) && (!isset($_GET['edit'])))
{
echo "<table style='border: solid 1px;width:100%;text-align:center;'>";
$result = mysql_query("select * from $tabel");
mysql_free_result($result);
$limit = 100; 
$result = mysql_query("select count(*) from $tabel");
$row = mysql_fetch_array($result);
$totalrecord = $row[0]; 
$totalpage = $totalrecord/$limit; 
if((int)$totalpage < $totalpage) $totalpage = (int)$totalpage+1; 
if(!isset($_GET['page'])){ 
$page = 1;
$start = 0;
}else{
if($_GET['page'] > $totalpage){ 
$page = $totalpage;
}else{
$page = $_GET['page'];
}
$start = $limit * ($page-1);
}
$queryindex=mysql_query("select * from $tabel");
$index = mysql_field_name($queryindex,0);
$jml_field = mysql_num_fields($queryindex);
echo "<tr>";
for ($ab = 0; $ab <= $jml_field ; $ab++)
{
if ($ab == $jml_field)
{
echo "<td style='border: solid 1px;background-color: #C0C0C0;'>OPTION</td>";
}else{
echo "<td style='border: solid 1px;background-color: #C0C0C0;'>".strtoupper(mysql_field_name($queryindex,$ab))."</td>";
}
}
echo "</tr>";
echo "<form action='' method='POST'><tr>";
for ($ac = 0; $ac <= $jml_field ; $ac++)
{
if ($ac == $jml_field)
{
echo "<td style='border: solid 1px;background-color: #C0C0C0;'><input type='submit' name='tambah' value='SAVE'/></td>";
}else{
echo "<td style='border: solid 1px;background-color: #C0C0C0;'><input type='text' style='width:100%;' name='".mysql_field_name($queryindex,$ac)."'/></td>";
}
}
echo "</tr></form>";
$querycrud=mysql_query("select * from $tabel ORDER BY $index DESC limit {$start}, {$limit}");
while ($reportcrud  = mysql_fetch_array($querycrud))
{
echo "<tr>";
for ($a = 0; $a <= $jml_field ; $a++)
{
if ($a == $jml_field)
{
echo "<td style='border: solid 1px;'><a href='?delete={$reportcrud[0]}' onclick=\"return confirm('Apakah Anda Yakin Untuk Menghapus {$reportcrud[0]} ?')\">Delete</a> | <a href='?edit={$reportcrud[0]}' onclick=\"return confirm('Apakah Anda Yakin Untuk Mengubah {$reportcrud[0]} ?')\">Edit</a></td>";
}else{
if ($reportcrud[$a] == "")
{
echo "<td style='border: solid 1px;'>-</td>";
}else{
echo "<td style='border: solid 1px;'>{$reportcrud[$a]}</td>";
}
}
}
echo "</tr>";
}
echo "</table>";
 
echo("<center>");
$bilangan = 1;
while ($bilangan <= $totalpage)
{
if ($bilangan == $page)
{
echo("<a href='{$_SERVER['PHP_SELF']}?page=$bilangan'><font color='blue'>$bilangan</font></a> ");
}else{
echo("<a href='{$_SERVER['PHP_SELF']}?page=$bilangan'><font color='red'>$bilangan</font></a> ");
}
$bilangan++;
}
echo("<br/>");
    if($page<=1){
        echo("&lt;&lt;Prev | ");
    }else{
        $prev = $page-1;
        echo("<a href='{$_SERVER['PHP_SELF']}?page={$prev}'><font color='red'>&lt;&lt;Prev</font></a> | ");
    }
     
     
    if($page>=$totalpage){
        echo("Next&gt;&gt;");
    }else{
        $next = $page+1;
        echo("<a href='{$_SERVER['PHP_SELF']}?page={$next}'><font color='red'>Next&gt;&gt;</font></a>");
    }
    echo("</center>");
    }
if ((isset($_GET['delete'])) && (!isset($_GET['edit'])))
{
$id = addslashes($_GET['delete']);
$querycrud=mysql_query("select * from $tabel ");
$nama_id = mysql_field_name($querycrud,0);
$quer="DELETE FROM $tabel WHERE $nama_id = '$id'";
$hasil=mysql_query($quer) or die("kesalahan query:".mysql_error() );
header("location:{$_SERVER['PHP_SELF']}");
}
if ((isset($_GET['edit'])) && (!isset($_GET['delete'])))
{
$id = addslashes($_GET['edit']);
$querycrud=mysql_query("select * from $tabel ");
$jml_field = mysql_num_fields($querycrud);
$nama_id = mysql_field_name($querycrud,0);
echo "<form action='' method='POST'><table style='border: solid 1px;width:100%;text-align:center;'>";
echo "<tr>";
for ($ab = 0; $ab <= $jml_field ; $ab++)
{
if ($ab == $jml_field)
{
echo "<td style='border: solid 1px;background-color: #C0C0C0;'>OPTION</td>";
}else{
echo "<td style='border: solid 1px;background-color: #C0C0C0;'>".strtoupper(mysql_field_name($querycrud,$ab))."</td>";
}
}
echo "</tr>";
$queryedit=mysql_query("select * from $tabel WHERE $nama_id = '$id'");
$hasiledit = mysql_fetch_array($queryedit);
echo"<tr>";
for ($ac = 0; $ac <= $jml_field ; $ac++)
{
if ($ac == $jml_field)
{
echo "<td style='border: solid 1px;background-color: #C0C0C0;'><input type='submit' name='edit' value='EDIT'/></td>";
}else{
echo "<td style='border: solid 1px;background-color: #C0C0C0;'><input type='text' style='width:100%;' name='".mysql_field_name($querycrud,$ac)."' value='{$hasiledit[$ac]}'/></td>";
}
}
echo "</tr></table></form>";
if (isset($_POST['edit']))
{
$id = addslashes($_GET['edit']);
for ($ad = 0; $ad < $jml_field ; $ad++)
{
$namafield[$ad] = mysql_field_name($querycrud,$ad);
}
for ($ae = 0; $ae < $jml_field ; $ae++)
{
$namavalue[$ae] = addslashes($_POST[$namafield[$ae]]);
}
$quer1 = "";
for ($af = 0; $af < $jml_field ; $af++)
{
if ($af == ($jml_field-1))
{
$quer1 = $quer1."".$namafield[$af]."='".$namavalue[$af]."' ";
}else{
$quer1 = $quer1."".$namafield[$af]."='".$namavalue[$af]."', ";
}
}
$querupdate = "UPDATE $tabel SET ".$quer1."WHERE $namafield[0]='$id'";
$hasilupdate=mysql_query($querupdate) or die("kesalahan query:".mysql_error() );
header("location:{$_SERVER['PHP_SELF']}");
}
}
if (isset($_POST['tambah']))
{
$querycrud=mysql_query("select * from $tabel ");
$jml_field = mysql_num_fields($querycrud);
for ($ad = 0; $ad < $jml_field ; $ad++)
{
$namafield[$ad] = mysql_field_name($querycrud,$ad);
}
for ($ae = 0; $ae < $jml_field ; $ae++)
{
$namavalue[$ae] = addslashes($_POST[$namafield[$ae]]);
}
$quer2 = "";
for ($af = 0; $af < $jml_field ; $af++)
{
if ($af == ($jml_field-1))
{
$quer2 = $quer2."".$namafield[$af];
}else{
$quer2 = $quer2."".$namafield[$af].",";
}
}
$quer4 = "";
for ($ag = 0; $ag < $jml_field ; $ag++)
{
if ($ag == ($jml_field-1))
{
$quer4 = $quer4."'".$namavalue[$ag]."'";
}else{
$quer4 = $quer4."'".$namavalue[$ag]."',";
}
}
$querinsert = "INSERT INTO $tabel (".$quer2.") VALUES (".$quer4.")";
$hasilinsert=mysql_query($querinsert) or die("kesalahan query:".mysql_error() );
header("location:{$_SERVER['PHP_SELF']}");
}
}
?>

SHARE:

Comments

Recent Posts

Cara Membuat Inputan Tanggal Dengan Javascript

Cara Membuat Inputan Tanggal Dengan Javascript

Buatlah file dan tuliskan kode sebagai berikut :<!-- awal kode --><link rel="stylesheet" type="text/css" href="style.css"><script type="text/javascript"

Pengurutan Atau Perangkingan Data Di PHP

Pengurutan Atau Perangkingan Data Di PHP

Berikut adalah cara untuk mengurutkan / perangkingan data di PHPCara 1 :<?php$nomer = array (99,28,7,6,54,6,8,9, 34,3,2,41,6,7,52);$data

Function CRUD Paging dan Search di PHP

Function CRUD Paging dan Search di PHP

Function untuk membuat Cread,Read,Update dan Delete Beserta Paging dan juga menu Pencarian.<?php ob_start();error_reporting(0);$host = "localhost";