Jump to content

[SOLVED] PHP Balance ?


nathan1

Recommended Posts

Hi everyone,

I have two tables orders and payments, i want to know the balance, i have done the following;

 

<?php
$c = $ro['user_id'];
//total payments
$p_result = mysql_query("SELECT * FROM payment WHERE user_id=$id") or die(mysql_error());

$ptotal = 0;

while ($p_ro = mysql_fetch_assoc($p_result)) {
$ptotal += $p_ro['payment'];
}

//total debit


$debit_result = mysql_query("SELECT * FROM hdebit WHERE user_id='$c' ") or die(mysql_error());

$total = 0;

//going to use num row to use while loop

$num_rows = mysql_num_rows($debit_result);


while ($num_rows = mysql_fetch_array($debit_result)){

//got to get event details
$event_id = $num_rows['event_desc'];
$trans_fee = $num_rows['price_two'];

if ($trans_fee == 'yes'){

$tq = mysql_query("SELECT * FROM events WHERE event_id=$event_id") or die("Error querying customer database1");
	$dotq = mysql_fetch_array($tq);

$qqq = $dotq['event_price'];


}
//lets get all the event details!

$qq = mysql_query("SELECT * FROM events WHERE event_id=$event_id") or die("Error querying customer database1");
	$doq = mysql_fetch_array($qq);

$ep = $doq['event_price'];


$total += $ep + $qqq;
}

$balance = $ptotal - $total;
?>

 

I am wondering if there is a better way of doing this? It seams to work but when i have multiple entries sometimes i get different answers for 0 - for instance 0.0832783728347e etc

 

Thanks Heaps!

Link to comment
Share on other sites

OK, I had to go through your code and figure out what you needed. This is how I understand it:

The total of payment.payment is the total payments made

hdebt.user_id = payment.user_id

hdebt.event_desc = events.event_id

The total of events.event_price is the total debt

if hdebt.price_two equals "yes", double the debt

 

If that is correct, try this:

<?php
$query = "SELECT SUM(a.payments) AS payments, SUM(c.event_price) AS debt, (SUM(a.payments)-SUM(c.event_price)) AS bal1, (SUM(a.payments)-(SUM(c.event_price)*2)) AS bal2, b.price_two FROM payments AS a, hdebt AS b, events AS c WHERE a.user_id = $id AND a.user_id = b.user_id AND b.event_desc = c.event_id GROUP BY b.price_two";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
?>

 

Now $row has all you need. Just add an IF statement that checks $row['prive_two']. If it's "yes," use $row['bal2'] as your balance. Otherwise use $row['bal1'].

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.