jhollin1138 Posted February 19, 2007 Share Posted February 19, 2007 I am working on script and I am having the addition problems with PHP. Here is a sample piece of code that I am having a problem with. ><?php $OverallTotal = 0; for ($i=0; $i<count($arr_Multiplier); $i++) { $arr_EstimateTotal[$i]['ItemType'] = $arr_Multiplier[$i]['ItemType']; $arr_EstimateTotal[$i]['Multiplier'] = 1 + $arr_Multiplier[$i]['Multiplier']; for ($j=0; $j<count($arr_Estimate); $j++) { if ($arr_Multiplier[$i]['ItemTypeID'] == $arr_Estimate[$j]['ItemTypeID']) { $arr_EstimateTotal[$i]['ItemTotal'] = $arr_EstimateTotal[$i]['ItemTotal'] + $arr_Estimate[$j]['Total']; $arr_EstimateTotal[$i]['MultTotal'] = ceil($arr_EstimateTotal[$i]['ItemTotal'] * $arr_EstimateTotal[$i]['Multiplier']); $OverallTotal = $OverallTotal + $arr_EstimateTotal[$i]['MultTotal']; } } } for ($i=0; $i<count($arr_LaborMultiplier); $i++) { $arr_LaborTotal[$i]['LaborType'] = $arr_LaborMultiplier[$i]['LaborType']; $arr_LaborTotal[$i]['LaborTotal'] = ceil($int_LaborTotal * $arr_LaborMultiplier[$i]['LaborMultiplier']); $OverallTotal = $OverallTotal + $arr_LaborTotal[$i]['LaborTotal']; } // Building Table echo '<table class="estimate">'."\n"; for ($i=0; $i<count($arr_EstimateTotal); $i++) { echo ' <tr>'."\n"; echo ' <td align="right" colspan="8">'; echo str_replace($cfg['lang']['tag_total'],$arr_EstimateTotal[$i]['ItemType'],money_format($cfg['lang']['est_Material'], $arr_EstimateTotal[$i]['ItemTotal'])); echo ':</td>'."\n"; echo estimate_box('ETotal'.'_'.$arr_EstimateTotal[$i]['ItemTypeID'], $arr_EstimateTotal[$i]['MultTotal'], 50, 'Text', 7, 'right', 0, 0); echo ' </tr>'."\n"; } for ($i=0; $i<count($arr_LaborMultiplier); $i++) { echo ' <tr>'."\n"; echo ' <td align="right" colspan="8">Total '.$arr_LaborTotal[$i]['LaborType'].':</td>'."\n"; echo estimate_box('LTotal'.'_'.$arr_LaborTotal[$i]['LaborTypeID'], $arr_LaborTotal[$i]['LaborTotal'], 50, 'Text', 7, 'right', 0, 0); echo ' </tr>'."\n"; } echo ' <tr>'."\n"; echo ' <td align="right" colspan="8">Total:</td>'."\n"; echo estimate_box('OTotal'.'_'.'T', $OverallTotal, 50, 'Text', 7, 'right', 0, 0); echo ' </tr>'."\n"; echo '</form>'."\n"; ?> I am pulling data into the arrays "$arr_Multiplier", "$arr_Estimate" & "$arr_LaborMultiplier" and variable "$int_LaborTotal" from a MySQL database. The data is all correct. The problem is the value for "$OverallTotal" is incorrect. PHP is saying 4193, but when I add the values I get 4075 (791+269+810+585+1620=4075). See this link to the stripped down source code of the output (I have a screen-shot of the output but I couldn't get it to attach it). Some items don't have the problem, while others do. Does anyone have any ideas? Quote Link to comment Share on other sites More sharing options...
jhollin1138 Posted February 19, 2007 Author Share Posted February 19, 2007 :oops:I found my problem. Replacing the similar section of code in my first post with the code below, fixes the it. >for ($i=0; $i<count($arr_Multiplier); $i++) { $arr_EstimateTotal[$i]['ItemType'] = $arr_Multiplier[$i]['ItemType']; $arr_EstimateTotal[$i]['Multiplier'] = 1 + $arr_Multiplier[$i]['Multiplier']; for ($j=0; $j<count($arr_Estimate); $j++) { if ($arr_Multiplier[$i]['ItemTypeID'] == $arr_Estimate[$j]['ItemTypeID']) { $arr_EstimateTotal[$i]['ItemTotal'] = $arr_EstimateTotal[$i]['ItemTotal'] + $arr_Estimate[$j]['Total']; $arr_EstimateTotal[$i]['MultTotal'] = ceil($arr_EstimateTotal[$i]['ItemTotal'] * $arr_EstimateTotal[$i]['Multiplier']); } } $OverallTotal = $OverallTotal + $arr_EstimateTotal[$i]['MultTotal']; } Quote Link to comment Share on other sites More sharing options...
TCH-Bruce Posted February 19, 2007 Share Posted February 19, 2007 Glad you sorted it out. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.