0

got another question...

I have the following while loop working:

    $activityreport = "SELECT $fieldname FROM $table WHERE QuoteID=$quoteid";

    $activity = mysql_query($activityreport) or die(mysql_error());

    while($data = mysql_fetch_array($activity)){

    $amount = $data[$fieldname];

    if($amount>0 && $table != 'OptionFees'){ 
        $amountc = abs($amount);
        }
        elseif($amount>0 && $table == 'OptionFees'){
        $amountd = abs($amount);
        }
        elseif($amount<0 && $table == 'OptionFees'){
            $amountc = abs($amount);
        }
        else{
            $amountd = abs($amount);
        }

    $totald = $totald + $amountd;
    $totalc = $totalc + $amountc;

    echo "<tr><td class='description'>$table</td>
            <td class='debit'>".money_format('%(#10n', $amountd)."</td>
            <td class='credit'>".money_format('%(#10n', $amountc)."</td></tr>";

    }

My issue is that $totald and $totalc are ending up with just the results from the last trip through the while loop, not the total of all trips as desired.

If it weren't for the IF statements breaking down $data[$fieldname], I could just use $total += $data[$fieldname], but the breakdown is important. As you can see, I'm using this to create a table which will display GL accounts and their respective debits and credits for a journal entry. The lines for each account display perfectly, but the total just show a repeat of the last line.

Any help would be very, very much appreciated!

2

1 Answer 1

1

Initialize those variables before you start your loop:

$activityreport = "SELECT $fieldname FROM $table WHERE QuoteID=$quoteid";

$activity = mysql_query($activityreport) or die(mysql_error());

$totald = 0;
$totalc = 0;

while($data = mysql_fetch_array($activity)){

$amount = $data[$fieldname];
$amountc = 0;
$amountd = 0;

if($amount>0 && $table != 'OptionFees'){ 
    $amountc = abs($amount);
    }
    elseif($amount>0 && $table == 'OptionFees'){
    $amountd = abs($amount);
    }
    elseif($amount<0 && $table == 'OptionFees'){
        $amountc = abs($amount);
    }
    else{
        $amountd = abs($amount);
    }

$totald = $totald + $amountd;
$totalc = $totalc + $amountc;

echo "<tr><td class='description'>$table</td>
        <td class='debit'>".money_format('%(#10n', $amountd)."</td>
        <td class='credit'>".money_format('%(#10n', $amountc)."</td></tr>";

}
Sign up to request clarification or add additional context in comments.

5 Comments

Hmm interesting article, I'll think about it. Thanks anyways.
Yeah, I was doing that with this, above $activityreport: $totald = 0; $totalc = 0; $amountd = 0; $amountc = 0; $activity = 0; Does it need to be after $activity?
No it doesn't, it should just be before loop. So i guess it doesn't work, maybe try initializing $amountd and $amountc just after while (and before if)?
no change... It's driving me crazy. I've added the $totald = $totald + $amountd; into the specific If statements, just in case, but that didn't change anything either...
figured it out, this while loop was inside a much larger one. I moved the declaration of $totald and $totalc outside of THAT while loop (and all loops) and voila! works perfectly now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.