Advertisement
cdsatrian

contoh fungsi

Jun 21st, 2012
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.99 KB | None | 0 0
  1. //file fungsi.inc.php
  2. <?php
  3.  
  4. function koneksi_db(){
  5.     $dbhost="localhost";
  6.     $dbuser="root";
  7.     $dbpasw="";
  8.     $dbname="latihan";
  9.     $link=mysql_connect($dbhost,$dbuser,$dbpasw);
  10.     mysql_select_db($dbname,$link);
  11. }
  12.  
  13. function delete_row($table,$field,$value){
  14.     $sql="DELETE FROM $table WHERE $field='$value'";
  15.     $ret=mysql_query($sql);
  16.     return $ret;
  17. }
  18.  
  19. function get_data($table,$fields){
  20.   $sql="SELECT ".fields." FROM ".$table;
  21.   return mysql_query($sql);
  22. }
  23.  
  24. koneksi_db();
  25. ?>
  26.  
  27. //file prodi.php
  28. <?php
  29. include("fungsi.inc.php");
  30. if(!empty($_GET['id'])){
  31.     delete_row('prodi','kd_prodi',$_GET['id']);
  32. }
  33. ?>
  34. <!DOCTYPE html>
  35. <html>
  36. <head>
  37. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  38. <title>Latihan Fungsi Delete Baris</title>
  39. </head>
  40. <body>
  41.   <table width="200" border="1">
  42.     <tr>
  43.       <td>Prodi</td>
  44.       <td>Nama</td>
  45.       <td>Delete</td>
  46.     </tr>
  47. <?php  
  48.   $q=get_data('prodi','kd_prodi,prodi');
  49.     while($k=mysql_fetch_array($q)){
  50.         echo "<tr>\n";
  51.         echo "<td>".$k['kd_prodi']."</td>\n";
  52.         echo "<td>".$k['prodi']."</td>\n";
  53.         echo "<td><a href=\"".$SERVER['PHP_SELF']."?id=".$k['kd_prodi']."\">hapus</a></td>\n";
  54.         echo "</tr>\n";
  55.     };
  56. ?>
  57.   </table>
  58. </body>
  59. </html>
  60.  
  61. //file mata_kuliah.php
  62. <?php
  63. include("fungsi.inc.php");
  64. if(!empty($_GET['id'])){
  65.     delete_row('mata_kuliah','kd_mk',$_GET['id']);
  66. }
  67. ?>
  68. <!DOCTYPE html>
  69. <html>
  70. <head>
  71. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  72. <title>Latihan Fungsi Delete Baris Mata Kuliah</title>
  73. </head>
  74. <body>
  75.   <table width="200" border="1">
  76.     <tr>
  77.       <td>Kode Mata Kuliah</td>
  78.       <td>Mata Kuliah</td>
  79.       <td>Delete</td>
  80.     </tr>
  81. <?php
  82.   $q=get_data('mata_kuliah','kd_mk,mata_kuliah');
  83.     while($k=mysql_fetch_array($q)){
  84.         echo "<tr>\n";
  85.         echo "<td>".$k['kd_mk']."</td>\n";
  86.         echo "<td>".$k['mata_kuliah']."</td>\n";
  87.         echo "<td><a href=\"".$SERVER['PHP_SELF']."?id=".$k['kd_mk']."\">hapus</a></td>\n";
  88.         echo "</tr>\n";
  89.     };
  90. ?>
  91.   </table>
  92. </body>
  93. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement