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.

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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:

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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']);

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.