Mirzam Muhammad (M0508051) Laporan PPL

1. File MySQLConnection.php <?php /* Class : MySQLConnection * Filename : MySQLConnection.php * Kelas koneksi ke MySQL dengan PHP Object * @author : mirzam muhammad * @NIM: M0508051 */ class MySQLConnection { //properties private $connection; private $database; private static $instances = 0; //methods public function __construct($hostname,$username,$password){ if(MySQLConnection::$instances==0){ $this->connection = mysql_connect($hostname,$username,$password) or die (mysql_error()."No Kesalahan".mysql_errno()); MySQLConnection::$instances=1; } else{ $msg="Tutup instansiasi yang lain dari". "kelas MySQLConnection."; die($msg); }} //destructor public function __destruct(){ $this->close(); } //header fungsi untuk membuat query public function buatQuery($sql,$databasename){ $this->database=$databasename; $dat=mysql_select_db($this->database,$this->connection); if(!$dat) { echo "Tidak Ada Database <br />"; } else { echo "Database OK <br />"; } if (mysql_query($sql,$this->connection)){ $this->mysql_res = mysql_query($sql); echo "Table berhasil dibuat <br />"; } else { echo "Table Gagal dibuat <br />"; }} //fungsi untuk menutup koneksi public function close(){ MySQLConnection::$instances=0; if(isset($this->connection)){ mysql_close($this->connection); unset($this->connection); }}}?> File test.php <html> <head> <meta http-equiv="content-type" content="text/html; charset=iso-88591" /> <meta name="author" content="Mirzam" /> <title>Test Koneksi</title> </head> <body> <?php // Filename : test.php require 'MySQLConnection.php'; $con = new MySQLConnection("localhost", "root", ""); if($con){ echo "Koneksi berhasil.<br />"; } if ( (isset($_POST['username'])) and (isset($_POST['password'])) and (isset($_POST['gender'])) and (isset($_POST['job'])) ) { $username = $_POST['username']; $password = $_POST['password']; $gender = $_POST['gender']; $job = $_POST['job']; $query = "INSERT INTO data (username, password, gender, job) VALUES ( '$username', '$password', '$gender', '$job' )"; } else { $query = ""; } $con->buatQuery($query,"ppl"); ?> </body> </html>

1

2.

File form.inc <?php /** * Class : Form * Filename : form.inc * Sebuah kelas untuk membuat HTML * @author mirzam muhammad * @NIM: M0508051 */ class Form{ //properties private $fields=array(); # nama field dan label private $processor; #nama file pemroses private $submit="Submit Form"; private $NFields=0; #banyaknya field yang ditambahkan //methods public function __construct($filepemroses,$lblsubmit){ $this->processor=$filepemroses; $this->submit=$lblsubmit; } public function addField($nama,$lbl){ $this->fields[$this->NFields]['name']=$nama; $this->fields[$this->NFields]['label']=$lbl; $this->NFields = $this->NFields+1; } public function addFieldType($nama,$lbl,$type){ $this->fields[$this->NFields]['name']=$nama; $this->fields[$this->NFields]['label']=$lbl; $this->fields[$this->NFields]['type']=$type; $this->NFields = $this->NFields+1; } public function addFieldValue($nama,$lbl,$tipe,$val) { $this->fields[$this->NFields]['name']=$nama; $this->fields[$this->NFields]['label']=$lbl; $this->fields[$this->NFields]['type']=$tipe; $this->fields[$this->NFields]['value']=$val; $this->NFields = $this->NFields+1; } public function addFieldValue1($nama,$lbl,$tipe,$val,$valName) { $this->fields[$this->NFields]['name']=$nama; $this->fields[$this->NFields]['label']=$lbl; $this->fields[$this->NFields]['type']=$tipe; $this->fields[$this->NFields]['value']=$val; $this->fields[$this->NFields]['valname']=$valName; $this->NFields = $this->NFields+1; } public function displayForm(){ echo "<form action='$this->processor' method='post'>"; echo "<table>"; for($j=1;$j<=sizeof($this->fields);$j++){ echo "<tr><td align='right'> {$this->fields[$j-1]['label']}: </td>\n";

Mirza Muha L po n L

ad M0508051

File f rm <html> <head> <meta http-e uiv="content-type" content="te t/html; charset=iso-88591" /> <meta name="author" content="Mirzam" /> <title>Form Mahasis a</title></head><body> < php /* File name form.php */ error_reporting(0 ; re uire_once("form.inc"); $phone_form = ne Form("test.php","Submit"); $phone_form->addFieldType("username","Username","te t"); $phone_form->addFieldType("pass ord","Pass ord","pass ord"); $phone_form->addFieldValue1("gender","Gender","radio","1","Male"); $phone_form->addFieldValue1("gender","","radio","0","Female"); $phone_form->addFieldType("job","Job","te t"); echo "<h3>Please fill out the following form:</h3>"; $phone_form->displayForm(); ?> </body></html>

tn

3.

File te t wTbl <html> <head> <meta http-e uiv="content-type" content="te t/html; charset=iso-88591" /> <meta name="author" content="Mirzam" /> <title>Test Menampilkan Tabel</title> </head> <body> <?php error_reporting(0); // Filename : testShowTbl.php re uire 'MyS onnection.php'; $con = new MyS onnection("localhost", "root", ""); if($con){ echo "Koneksi berhasil.<br />"; } T username, password, gender, job F M data"; $ uery = "S $con->buatQuery("$ uery","ppl"); $result = $con->mys l_res; while ($data = mys l_fetch_array( $result )) { $dump .= '<tr><td>'.$data['username'].'</td>'; $dump .= '<td>'.$data['password'].'</td>'; if ($data['gender'] == 0) { $dump .= '<td>Female</td>'; } else { $dump .= '<td>Male</td>'; } $dump .= '<td>'.$data['job'].'</td></tr>'; }

9

//ec "< >< ty e= te t' name='{$this-> ie s[$j1]['name']}'></td></tr>"; echo "<td><input type='{$this-> ie ds[$j-1]['type']}' name='{$this> ie ds[$j-1]['name']}'></td></tr>"; } echo "<tr><td colspan=2 align='center'> <input type='submit' value='{$this->submit}'></td></tr> n"; echo "</table>"; }} >

&

% 

CB

©  & % 7

¢
echo ' <p> <br><h1> ist of Table</h1> </p> <br> <table width="50%" cellspacing="10"> <tr> <td><b>Username</b></td> <td><b>Password</b></td> <td><b>Gender</b></td> <td><b>Job</b></td> </tr> '.$dump.' </table> '; ?> </body> </html>

%

& 

¡    & 

  ¨ © ¨ §¦ ¥¥ £¤ £   6 6 6 52 54 32 1 0 @98 @98 & ) @A9A $ 6 "# "! ( $ 6

2 

 6 '

Sign up to vote on this title
UsefulNot useful