Menghitung Grand Total di php

Teguh · May 25, 2011
Mas mau tanya gimana caranya menghitung GrandTotal/TotalBayar pada transaksi pembelian di php?
kondisinya seperti ini:
1. jumlah baris input harga bisa disesuaikan (tidak selalu tetap) dengan ketentuan:
- bila di klik link tambah jumlah baris harga bertambah 1
- bila di klik link hapus jumlah baris harga berkurang 1
2. nilai GrandTotal/TotalBayar langsung otomatis muncul(terhitung) saat input harga dimasukan
Silahkan login untuk menjawab!
5
Loading...
Ellyx Christian · May 25, 2011 · 5 Suka · 0 Tidak Suka
pada contoh ini data form disimpan di session.
dan bentuk sessionnya seperti berikut:
$_SESSION['cart'] = array(
	'PRD001' => array(
		'code' => 'PRD001',
		'price' => 1000,
		'qty' => 2
	)
);
contoh kode:
<?php
session_start();
//add
if(isset($_POST['add'])){
	$code = $_POST['code'];
	$price = $_POST['price'];
	$qty = isset($_SESSION['cart'][$code]) ? $_POST['qty'] + $_SESSION['cart'][$code] : $_POST['qty'];
	$_SESSION['cart'][$code]['code'] = $code;
	$_SESSION['cart'][$code]['price'] = $price;
	$_SESSION['cart'][$code]['qty'] = $qty;
}
//update
if(isset($_POST['update'])){
foreach($_POST['code'] as $code){
	if(isset($_POST['delete'][$code]) && $_POST['delete'][$code]){
		unset($_SESSION['cart'][$code]);
	}elseif(isset($_SESSION['cart'][$code])){
		$_SESSION['cart'][$code]['price'] = $_POST['price'][$code];
		$_SESSION['cart'][$code]['qty'] = $_POST['qty'][$code];
	}
}
}
<form name="form-add" action="" method="post">
Code: <input type="text" name="code" />
Price: <input type="text" name="price"/>
Qty: <input type="text" name="qty"/>
<input type="submit" name="add" value="Add"/>
</form>
<form name="form-cart" action="" method="post">
<table>
<tr>
	<td>Code</td>
	<td>Price</td>
	<td>Qty</td>
	<td>Sub Total</td>
	<td>Delete</td>
</tr>
<?php 
$grandTotal = 0;
if(isset($_SESSION['cart'])){
foreach($_SESSION['cart'] as $cart){?>
<tr>
	<td>
	<?php echo $cart['code'];?>
	<input type="hidden" name="code[]" value="<?php echo $cart['code'];?>"/>
	</td>
	<td><input type="text" name="price[<?=$cart["code"];?>]" value="<?php echo $cart['price'];?>"/></td>
	<td><input type="text" name="qty[<?=$cart['code'];?>]" value="<?php echo $cart['qty'];?>"/></td>
	<?php
	//proses kalkulasi
	$total = $cart['qty'] * $cart['price'];
	$grandTotal += $total;
	?>
	<td><?php echo $total;?></td>
	<td><input type="checkbox" name="delete[<?=$cart['code'];?>]" value="1"/></td>
</tr>
<?php }
}?>
<tr>
	<td></td>
	<td></td>
	<td>Grand Total:</td>
	<td><?php echo $grandTotal;?></td>
	<td></td>
</tr>
<tr>
	<td></td>
	<td></td>
	<td></td>
	<td><input type="submit" name="update" value="Update Cart"/></td>
	<td></td>
</tr>
</table>
</form>
1
Loading...
Fr7Frans · Sep 5, 2011 · 1 Suka · 0 Tidak Suka
solusi.JPG


saya sudah mencobanya bang,,
tapi terjadi error disini,,,,,
tolong bantuannya bang Ellyx,,,,,,,,,,,,
3
Loading...
Ellyx Christian · Sep 8, 2011 · 3 Suka · 0 Tidak Suka
coba yang ini:
<?php
session_start();
//add
if(isset($_POST['add'])){
	$code = $_POST['code'];
	$price = $_POST['price'];
	$qty = isset($_SESSION['cart'][$code]) ? $_POST['qty'] + $_SESSION['cart'][$code] : $_POST['qty'];
	$_SESSION['cart'][$code]['code'] = $code;
	$_SESSION['cart'][$code]['price'] = $price;
	$_SESSION['cart'][$code]['qty'] = $qty;
}
//update
if(isset($_POST['update'])){
foreach($_POST['code'] as $code){
	if(isset($_POST['delete'][$code]) && $_POST['delete'][$code]){
		unset($_SESSION['cart'][$code]);
	}elseif(isset($_SESSION['cart'][$code])){
		$_SESSION['cart'][$code]['price'] = $_POST['price'][$code];
		$_SESSION['cart'][$code]['qty'] = $_POST['qty'][$code];
	}
}
}
?>
<form name="form-add" action="" method="post">
Code: <input type="text" name="code" />
Price: <input type="text" name="price"/>
Qty: <input type="text" name="qty"/>
<input type="submit" name="add" value="Add"/>
</form>
<form name="form-cart" action="" method="post">
<table>
<tr>
	<td>Code</td>
	<td>Price</td>
	<td>Qty</td>
	<td>Sub Total</td>
	<td>Delete</td>
</tr>
<?php 
$grandTotal = 0;
if(isset($_SESSION['cart'])){
foreach($_SESSION['cart'] as $cart){?>
<tr>
	<td>
	<?php echo $cart['code'];?>
	<input type="hidden" name="code[]" value="<?php echo $cart['code'];?>"/>
	</td>
	<td><input type="text" name="price[<?=$cart["code"];?>]" value="<?php echo $cart['price'];?>"/></td>
	<td><input type="text" name="qty[<?=$cart['code'];?>]" value="<?php echo $cart['qty'];?>"/></td>
	<?php
	//proses kalkulasi
	$total = $cart['qty'] * $cart['price'];
	$grandTotal += $total;
	?>
	<td><?php echo $total;?></td>
	<td><input type="checkbox" name="delete[<?=$cart['code'];?>]" value="1"/></td>
</tr>
<?php }
}?>
<tr>
	<td></td>
	<td></td>
	<td>Grand Total:</td>
	<td><?php echo $grandTotal;?></td>
	<td></td>
</tr>
<tr>
	<td></td>
	<td></td>
	<td></td>
	<td><input type="submit" name="update" value="Update Cart"/></td>
	<td></td>
</tr>
</table>
</form>
0
Loading...
Fr7Frans · Sep 10, 2011 · 1 Suka · 1 Tidak Suka
bang,,, masih tetap dalam pernyataan "Foreach ()" yang salah..

Warning: Invalid argument supplied for foreach() in c:\apache\htdocs\aku.php on line 44

dia tidak bisa melakukan penginputan berulang, jika satu kali masih bisa tapi sudah 2 kali tidak bisa lagi bang Ellyx.

mohon penjelasannya bang sdikit lagi .... ??
0
Loading...
Ellyx Christian · Sep 10, 2011 · 0 Suka · 0 Tidak Suka
saya sudah coba dan berhasil. Coba kamu copy paste langsung kode saya, dan lihat apakah mau jalan atau tidak. Jika tetap tidak jalan, kamu pake PHP versi berapa?
1
Loading...
Fr7Frans · Sep 12, 2011 · 1 Suka · 0 Tidak Suka
saya pake MACROMEDIA DREAMWEAVER Versi 8.0

yang salah di tunjukkan pada pernyataan Foreach ()

saya ingin tahu bang,, pernyataan Foreach itu digunakan pada apa saja ??

mohon di jelaskan dlu bang Ellyx.......??
0
Loading...
Ellyx Christian · Sep 12, 2011 · 1 Suka · 1 Tidak Suka
http://php.net/foreach
saya tanya kamu pake php versi berapa? kamu jawab dengan dreamweaver!
Coba kamu copy paste langsung kode saya, dan lihat apakah mau jalan atau tidak. Jika tetap tidak jalan, kamu pake PHP versi berapa?
1
Loading...
Fr7Frans · Sep 14, 2011 · 1 Suka · 0 Tidak Suka
saya pake PHP Versi 4.1.1 bang.
0
Loading...
Ellyx Christian · Sep 14, 2011 · 0 Suka · 0 Tidak Suka
coba kasi tunjuk koding kamu!
1
Loading...
Fr7Frans · Sep 16, 2011 · 1 Suka · 0 Tidak Suka
<?php
	ob_start();
    session_start();
	ob_end_clean();
    //add
    if(isset($_POST['add'])){
    $code = $_POST['code'];
    $price = $_POST['price'];
    $qty = isset($_SESSION['cart'][$code]) ? $_POST['qty'] + $_SESSION['cart'][$code] : $_POST['qty'];
    $_SESSION['cart'][$code]['code'] = $code;
    $_SESSION['cart'][$code]['price'] = $price;
    $_SESSION['cart'][$code]['qty'] = $qty;
    }
    //update
    if(isset($_POST['update'])){
    foreach($_POST['code'] as $code){
    if(isset($_POST['delete'][$code]) && $_POST['delete'][$code]){
    unset($_SESSION['cart'][$code]);
    }elseif(isset($_SESSION['cart'][$code])){
    $_SESSION['cart'][$code]['price'] = $_POST['price'][$code];
    $_SESSION['cart'][$code]['qty'] = $_POST['qty'][$code];
    }
    }
    }
    ?>
    <form name="form-add" action="" method="post">
    Code: <input type="text" name="code" />
    Price: <input type="text" name="price"/>
    Qty: <input type="text" name="qty"/>
    <input type="submit" name="add" value="Add"/>
    </form>
    <form name="form-cart" action="" method="post">
    <table>
    <tr>
    <td>Code</td>
    <td>Price</td>
    <td>Qty</td>
    <td>Sub Total</td>
    <td>Delete</td>
    </tr>
    <?php
    $grandTotal = 0;
    if(isset($_SESSION['cart'])){
    foreach($_SESSION['cart'] as $cart){?>
    <tr>
    <td>
    <?php echo $cart['code'];?>
    <input type="hidden" name="code[]" value="<?php echo $cart['code'];?>"/>
    </td>
    <td><input type="text" name="price[<?=$cart["code"];?>]" value="<?php echo $cart['price'];?>"/></td>
    <td><input type="text" name="qty[<?=$cart['code'];?>]" value="<?php echo $cart['qty'];?>"/></td>
    <?php
    //proses kalkulasi
    $total = $cart['qty'] * $cart['price'];
    $grandTotal += $total;
    ?>
    <td><?php echo $total;?></td>
    <td><input type="checkbox" name="delete[<?=$cart['code'];?>]" value="1"/></td>
    </tr>
    <?php }
	}?>
    <tr>
    <td></td>
    <td></td>
    <td>Grand Total:</td>
    <td><?php echo $grandTotal;?></td>
    <td></td>
    </tr>
    <tr>
    <td></td>
    <td></td>
    <td></td>
    <td><input type="submit" name="update" value="Update Cart"/></td>
    <td></td>
    </tr>
    </table>
    </form>
?>