Jump to content

If statement with Session variable problem


savagenoob

Recommended Posts

OK, I think I have confused this code and it is not calculating correctly.

$ckifdue = mysql_query("SELECT * FROM accounting WHERE ClientID = '$clientid'") or die(mysql_error());
while($ckdue =mysql_fetch_array($ckifdue)){
    if($ckdue['AmountDue'] == "0"){
        $_SESSION['MONEYOWED'] = "0.00";
    }
    else
    {
       $_SESSION['MONEYOWED'] = $_SESSION['MONEYOWED'] + $ckdue['AmountDue'];
    }
    if($ckdue['Amount1RS'] == "Balance Due"){
        $totcol = $ckdue['TotalCollected'];
        echo " total collected:" . $totcol;
        $monpaid = $_SESSION['MONEYPAID'];
        echo "money paid in:" . $monpaid;
        $_SESSION['MONEYPAID'] =  $totcol + $monpaid;
        echo "session money paid:" . $_SESSION['MONEYPAID'];
    }
}
$moneypaid  = $_SESSION['MONEYPAID'];
$moneyowed = $_SESSION['MONEYOWED'];
echo "money paid:" . $moneypaid;
echo " money owed:" . $moneyowed;
$moneyowed = $moneyowed - $moneypaid;
unset($_SESSION['MONEYOWED']);
unset($_SESSION['MONEYPAID']);

The echos look like:

 

total collected:100.00money paid in:200session money paid:300 total collected:100.00money paid in:300session money paid:400money paid:400 money owed:0.00

 

There should be $100 due, $200 paid, so a balance of -100. There is 2 balance due reciepts, this is just not calclating right in the sessions.

 

Try this:

 

//Bring this statement out of while
$_SESSION['MONEYOWED'] = "0.00";
while($ckdue =mysql_fetch_array($ckifdue)){
       //remove if else, even if amount due is 0, it will add 0
       $_SESSION['MONEYOWED'] = $_SESSION['MONEYOWED'] + $ckdue['AmountDue'];

 

Keep the remaining code as it is.

The echos look like:

 

total collected:100.00money paid in:200session money paid:300 total collected:100.00money paid in:300session money paid:400money paid:400 money owed:0.00

 

There should be $100 due, $200 paid, so a balance of -100. There is 2 balance due reciepts, this is just not calclating right in the sessions.

I thought I did  :confused:

It would be like...

 

Amount1RS        TotalCollected      AmountDue

New Business    200(or w/e)        100

Balance Due      100

Balance Due      100

 

I just did 2 balance dues that exceeded the amount due on this particular client for testing purposes but wouldnt happen normally.

I knew it was simple, I had the elseif statement in the first loop which went through its own loop causing it to add to much, just moved it down a bracket...

while($ckdue =mysql_fetch_array($ckifdue))
{       
if($ckdue['Amount1RS'] == "New Business")  {     
$_SESSION['MONEYOWED'] = $_SESSION['MONEYOWED'] + $ckdue['AmountDue'];
}
    
}
if($ckdue['Amount1RS'] == "Balance Due"){
        $totcol = $ckdue['TotalCollected'];
        $sesscol = $_SESSION['MONEYPAID'];
        $_SESSION['MONEYPAID'] = $totcol + $sesscol;
        
    }
$moneypaid  = $_SESSION['MONEYPAID'];
$moneyowed = $_SESSION['MONEYOWED'];
$moneyowed = $moneyowed - $moneypaid;
unset($_SESSION['MONEYOWED']);
unset($_SESSION['MONEYPAID']);

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.