Jump to content
  • 0

Php Math


simplynice

Question


  • Group:  Members
  • Topic Count:  25
  • Topics Per Day:  0.01
  • Content Count:  128
  • Reputation:   4
  • Joined:  11/14/11
  • Last Seen:  

Good day guys, i just want to ask about PHP math.

I don't have any idea what to do with this stuff.

So for example i'm selling 3 items [already stored this in mySQL/Database] each item has a value of $100 and the total money of the client is $500 i want to do the calculation using PHP script. So it will subtract the $300 into the client's money which is $500. So the total exchange is $200..

So now, i can do this in PHP in manual way but how about let the PHP do the job.

Like i'll buy 6 items or so, and enter the total money i got. It will subtract automatically and tells me how much do i owe and how much do i get back.

It's more like buying stuffs in a supermarket, paying it, giving you receipt.

Total amount of what did you bought:

Total exchange:

Total money:

Let me know if the explanation is so much complicated and thanks in advance.

Link to comment
Share on other sites

5 answers to this question

Recommended Posts


  • Group:  Members
  • Topic Count:  47
  • Topics Per Day:  0.01
  • Content Count:  633
  • Reputation:   78
  • Joined:  11/14/11
  • Last Seen:  

Try this but i havent tested it yet


<?php 

$connect = mysql_connect("localhost,root,root") or die(mysql_error());
$select_db = mysql_select_db("item_db",$connect);

$query_items = mysql_query("SELECT * FROM tbl_items") or die(mysql_error());
/*
Assuming that this the table format:
item_id
item_name
item_price
*/
?>

<html>
<head>
<title>PHP Do the calculation</title>
</head>
<body>
<form action="index.php" method="GET">
<?php while($fetch_items = mysql_fetch_array($query_items)){ ?>
        <input type="checbox" name="item-<?php echo $fetch_items['item_id']; ?>" id="item-<?php echo $fetch_items['item_id']; ?>" value="name="<?php echo $fetch_items['item_id']; ?>"/><?php echo $fetch_items['item_name']; ?><input type="text" id="amount-<?php echo $fetch_items['item_id']; ?>" name="amount-<?php echo $fetch_items['item_id']; ?>" value="0"/>
<br/>
<?php } ?>
Budget/Client Money: <input type="text" name="clientbudget" id="clientbudget" />
<input type="submit" value="Submit" />
<input type="reset" value="Reset" />

</form>
<?php 

if(!empty($_GET))
{
if(!empty($_GET['clientbudget']))
{
$get_total_expenses = 0;
$change;
$budget = $_GET['clientbudget'];
while($fetch_items = mysql_fetch_array($query_items)){
if(isset($_GET['item-'.$fetch_items['item_id']]))
{
$get_total_expenses += ($fetch_items['item_price']*$fetch_items['amount-'.$fetch_items['item_id']]);
}
}
if($get_total_expenses > $budget)
{
echo "Insufficient funds.";
}
else
{
$change = $budget - $get_total_expenses; 
echo "Total amount of what did you bought: ".$get_total_expenses;
echo "Total Change: ".$change;
echo "Total Money: ".$budget;
}

}
else
echo "You have no budget.";
}
?>

</body>

</html>

Link to comment
Share on other sites


  • Group:  Members
  • Topic Count:  25
  • Topics Per Day:  0.01
  • Content Count:  128
  • Reputation:   4
  • Joined:  11/14/11
  • Last Seen:  

I'll give it a try jaypee, thanks!

Link to comment
Share on other sites


  • Group:  Members
  • Topic Count:  25
  • Topics Per Day:  0.01
  • Content Count:  128
  • Reputation:   4
  • Joined:  11/14/11
  • Last Seen:  

Uhmm..it seems that it's just getting the value of customer's budget but no exchange and total expenses so i made this:

<?php include('dbconnect.php'); ?>
<?php
if(isset($_POST['total']))
{
$id = $_POST['item_id'];
$query_prices = mysql_query("SELECT * FROM add_parts WHERE item_id = '$id'") or die(mysql_error());
while($row = mysql_fetch_array($query_prices))
{
$itemprice = $row['item_price'];
$itemamount = $_POST['item_amount'];
$customer  = $_POST['customers_money'];
$amount_to_pay = $itemprice * $itemamount;
if($amount_to_pay > $customer) { echo "You dont have enough money."; die(); }
$cash =  $customer - $amount_to_pay;
echo "Price per item: ".$itemprice;
echo "<br/>Amount Due: ".$amount_to_pay;
echo "<br/>Exchange: ".$cash;
echo "<br/>Customer cash: ".$customer;
}
}
?>

<?php 
$query = mysql_query("SELECT * FROM add_parts") or die(mysql_error());
while($row = mysql_fetch_array($query))
{ 
$itemid = $row['item_id'];
$itemname = $row['item_name'];
$itemprice = $row['item_price'];
?>

<form method="POST">
<input type="checkbox" name="item_id" id="item_id" value="<?php echo $itemid; ?>" /> <label><?php echo $itemname; ?> 
- Price [ Php <?php echo $itemprice; ?> ]</label><br/>
<!-- <input type="hidden" name="hidden_price" id="" value="<?php echo $itemprice; ?>" /> -->
<?php
}
?>
<input type="text" name="item_amount" id="" placeholder="Enter how many item" /><br/>
<input type="text" name="customers_money" id="" placeholder="Customer's total money" /><br/><br/>
<input type="submit" name="total" value="Total"/>
</form>

It works very fine although i can't select as much as i want. So if there's someone or anyone can help me out of this do as you please. :)

Link to comment
Share on other sites


  • Group:  Members
  • Topic Count:  47
  • Topics Per Day:  0.01
  • Content Count:  633
  • Reputation:   78
  • Joined:  11/14/11
  • Last Seen:  

try to put your form tag outside the while loop.

the loop will generate multiple opening form. And i guess the last loop will be the only valid form tag

<form method="POST">
<form method="POST">
<form method="POST">
<form method="POST">
.
.
.
</form>

Edited by JayPeeMateo
Link to comment
Share on other sites


  • Group:  Members
  • Topic Count:  25
  • Topics Per Day:  0.01
  • Content Count:  128
  • Reputation:   4
  • Joined:  11/14/11
  • Last Seen:  

Used the SQL query sum. :)

Link to comment
Share on other sites

×
×
  • Create New...