disarikan dari : http://chandrajatnika.com/2008/07/sistem-transaksi-barang-sederhana/#more-27, dengan sedikit perubahan, karena script aslinya muncul error jika dijalanin.
berikut untuk script buat tabel barang.
CREATE TABLE `barang` (
`id_barang` CHAR(5) NOT NULL,
`nama_barang` VARCHAR(40) NOT NULL,
`jumlah` INT(5) NOT NULL,
PRIMARY KEY (`id_barang`)
);
berikut kelas barangnya dan beri nama barang.class.php
<?
class Barang{
var $table = 'barang';
function Barang(){
mysql_connect('localhost','root','');
mysql_select_db('test');
}
function insert($data){
$fields = array();
$values = array();
foreach($data as $key=>$value){
$fields[] = $key;
$values[] = "'".$value."'";
}
$sql = "INSERT INTO ".$this->table."(".implode(',',$fields).") VALUES(".implode(',',$values).")";
$result = mysql_query($sql) or die(mysql_error());
return $result;
}
function get(){
$sql = mysql_query('SELECT * FROM '.$this->table) or die(mysql_error());
$data = array();
if(!$sql || @mysql_num_rows($sql) == 0){
return NULL;
}else{
while($row = mysql_fetch_object($sql)){
$data[] = $row;
}
return $data;
}
}
}
?>
berikut sessionnya dan beri nama session.class.php
<?
@session_start(); // klo session mesti diawali dengan fungsi di ini
class StoreSession{
var $name = 'barang';
var $error = '';
function StoreSession(){
if(!isset($_SESSION[$this->name])){
$_SESSION[$this->name] = array();
}
}
function add($id,$nama,$jumlah){
if(!array_key_exists($id,$_SESSION[$this->name])){
$_SESSION[$this->name][$id] = array('nama'=>$nama,'jumlah'=>$jumlah);
}else $_SESSION[$this->name][$id]['jumlah'] += $jumlah;
}
function get(){
return $_SESSION[$this->name];
}
function delete($id=false){
if($id) unset($_SESSION[$this->name][$id]);
else unset($_SESSION[$this->name]);
}
function isNull(){
if(!isset($_SESSION[$this->name])) return true;
else return empty($_SESSION[$this->name]);
}
}
?>
berikut beri nama index.php
<?
require('session.class.php');
$sess = new StoreSession();
if(isset($_GET['delete'])){
$sess->delete($_GET['delete']);
header('Location: ./');
}
if(isset($_GET['save'])){
require('barang.class.php');
$barang = new Barang();
$barang->truncate();
foreach($sess->get() as $key=>$value){
$post = array(
'id_barang' => $key,
'nama_barang' => $value['nama'],
'jumlah' => $value['jumlah']
);
$barang->insert($post);
}
header('Location: result.php');
}
if(isset($_POST['submit'])){
$sess->add($_POST['id_barang'],$_POST['nama_barang'],$_POST['jumlah']);
}
?>
<html>
<head>
<title>Data Transaction</title>
</head>
<body>
<form id="form1" name="form1" method="post" action="">
Kode Barang :
<input name="id_barang" type="text" id="id_barang" size="5" maxlength="5" />
<br />
Nama Barang :
<input name="nama_barang" type="text" id="nama_barang" size="30" />
<br />
Jumlah :
<input name="jumlah" type="text" id="jumlah" size="4" maxlength="5" />
<br />
<input name="submit" type="submit" id="submit" value="Submit" />
<input type="reset" name="Submit2" value="Reset" />
</form>
<br />
<table border="1" cellspacing="0" cellpadding="0" width="83%">
<tr>
<th width="13%" scope="col">ID Barang </th>
<th width="54%" scope="col">Nama Barang </th>
<th width="12%" scope="col">Jumlah</th>
<th width="21%" scope="col">Hapus</th>
</tr>
<? foreach($sess->get() as $key=>$value): ?>
<tr>
<td align="center"><?=$key?></td>
<td><?=$value['nama']?></td>
<td align="center"><?=$value['jumlah']?></td>
<td align="center"><a href="?delete=<?=$key?>">Hapus</a></td>
</tr>
<? endforeach; ?>
</table>
<? if(!$sess->isNull()): ?>
<center><input type="button" value="Save" onClick="document.location='?save=1'" /></center>
<? endif; ?>
</body>
</html>
dan berinama berikut result.php
<html>
<head>
<title>Data Result</title>
</head>
<body>
<?
require('barang.class.php');
$barang = new Barang();
?>
<table border="1" cellspacing="0" cellpadding="0" width="83%">
<tr>
<th width="18%" scope="col">ID Barang </th>
<th width="63%" scope="col">Nama Barang </th>
<th width="19%" scope="col">Jumlah</th>
</tr>
<? foreach($barang->get() as $row): ?>
<tr>
<td align="center"><?=$row->id_barang?></td>
<td><?=$row->nama_barang?></td>
<td align="center"><?=$row->jumlah?></td>
</tr>
<? endforeach; ?>
</table>
</body>
</html>
Sunday, January 11, 2009
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment