wesley_santos2002 Posted June 10, 2012 Share Posted June 10, 2012 Hello everyone, im having problems with this function to add numbers from a form, in the form i have $max1 to $max12 and $mand1 to $mand12, so what the function does is simply make the sum of 12 vars, "max" and "mand", so i use the same function for both results, it works perfectly for "mand" but for "max" it's not adding whatever comes after ".", like: 1.5 + 1.7 is resulting in only 3, when it should be 3.2, whats confusing me it's that it's working just fine with the values from $mand1 to $mand12, i hope that you guys can help me out, btw, i'm just starting at PHP and already love it, thank's to everyone in advance. Wesley Santos Quote Link to comment https://forums.phpfreaks.com/topic/263967-getting-wrong-results-on-php-function/ Share on other sites More sharing options...
smoseley Posted June 10, 2012 Share Posted June 10, 2012 Your code has a bug. If you post it, someone might be able to tell you more. Quote Link to comment https://forums.phpfreaks.com/topic/263967-getting-wrong-results-on-php-function/#findComment-1352774 Share on other sites More sharing options...
wesley_santos2002 Posted June 11, 2012 Author Share Posted June 11, 2012 Here's the function: function soma_T($valor1, $valor2, $valor3, $valor4, $valor5, $valor6, $valor7, $valor8, $valor9, $valor10, $valor11, $valor12) { $total = $valor1 + $valor2 + $valor3 + $valor4 + $valor5 + $valor6 + $valor7 + $valor8 + $valor9 + $valor10 + $valor11 + $valor12; return $total; } and here's the code in the CGI: $max1 = $_POST['max1']; $max2 = $_POST['max2']; $max3 = $_POST['max3']; $max4 = $_POST['max4']; $max5 = $_POST['max5']; $max6 = $_POST['max6']; $max7 = $_POST['max7']; $max8 = $_POST['max8']; $max9 = $_POST['max9']; $max10 = $_POST['max10']; $max11 = $_POST['max11']; $max12 = $_POST['max12']; $mand1 = $_POST['mand1']; $mand2 = $_POST['mand2']; $mand3 = $_POST['mand3']; $mand4 = $_POST['mand4']; $mand5 = $_POST['mand5']; $mand6 = $_POST['mand6']; $mand7 = $_POST['mand7']; $mand8 = $_POST['mand8']; $mand9 = $_POST['mand9']; $mand10 = $_POST['mand10']; $mand11 = $_POST['mand11']; $mand12 = $_POST['mand12']; $superiores_total = soma_T($max1, $max2, $max3, $max4, $max5, $max6, $max7, $max8, $max9, $max10, $max11, $max12); $inferiores_total = soma_T($mand1, $mand2, $mand3, $mand4, $mand5, $mand6, $mand7, $mand8, $mand9, $mand10, $mand11, $mand12); <tr bgcolor="#9cc1e1"> <td> <br /> <strong>DISCREPÂNCIA ENTRE OS ARCOS</strong><br /> <strong>12 Mand =</strong> <?php print $inferiores_total; ?><br /> <strong>12 Max =</strong> <?php print $superiores_total; ?> </td> I hope this will make it clear, you see, the same function works fine for 12 Mand, but not for 12 Max, it just ignores whatever comes after "." Quote Link to comment https://forums.phpfreaks.com/topic/263967-getting-wrong-results-on-php-function/#findComment-1352787 Share on other sites More sharing options...
smoseley Posted June 11, 2012 Share Posted June 11, 2012 That code would not generate the result you posted. Show us the form. Quote Link to comment https://forums.phpfreaks.com/topic/263967-getting-wrong-results-on-php-function/#findComment-1352802 Share on other sites More sharing options...
wesley_santos2002 Posted June 11, 2012 Author Share Posted June 11, 2012 It's kinda long, but here it is. <tr bgcolor="#9cc1e1"> <td align="left"> <br /> <strong>Primeiro Molar Direito</strong><br /> <strong>Segundo Pré-Molar Direito</strong><br /> <strong>Primeiro Pré-Molar Direito</strong><br /> <br /> </td> <td align="center"> <br /> <input type ="Text" size="4" name="max1"><br /> <input type ="Text" size="4" name="max2"><br /> <input type ="Text" size="4" name="max3"><br /> <br /> </td> <td align="center"> <br /> <input type ="Text" size="4" name="mand1"><br /> <input type ="Text" size="4" name="mand2"><br /> <input type ="Text" size="4" name="mand3"><br /> <br /> </td> <tr bgcolor="#9cc1e1"> <td align="left"> <br /> <strong>Canino Direito</strong><br /> <strong>Incisivo Lateral Direito</strong><br /> <strong>Incisivo Central Direito</strong><br /> <br /> </td> <td align="center"> <br /> <input type ="Text" size="4" name="max4"><br /> <input type ="Text" size="4" name="max5"><br /> <input type ="Text" size="4" name="max6"><br /> <br /> </td> <td align="center"> <br /> <input type ="Text" size="4" name="mand4"><br /> <input type ="Text" size="4" name="mand5"><br /> <input type ="Text" size="4" name="mand6"><br /> <br /> </td> <tr bgcolor="#9cc1e1"> <td align="left"> <br /> <strong>Incisivo Central Esquerdo</strong><br /> <strong>Incisivo Lateral Esquerdo</strong><br /> <strong>Canino Esquerdo</strong><br /> <br /> </td> <td align="center"> <br /> <input type ="Text" size="4" name="max7"><br /> <input type ="Text" size="4" name="max8"><br /> <input type ="Text" size="4" name="max9"><br /> <br /> </td> <td align="center"> <br /> <input type ="Text" size="4" name="mand7"><br /> <input type ="Text" size="4" name="mand8"><br /> <input type ="Text" size="4" name="mand9"><br /> <br /> </td> <tr bgcolor="#9cc1e1"> <td align="left"> <br /> <strong>Primeiro Pré-Molar Esquerdo</strong><br /> <strong>Segundo Pré-Molar Esquerdo</strong><br /> <strong>Primeiro Molar Esquerdo</strong><br /> <br /> </td> <td align="center"> <br /> <input type ="Text" size="4" name="max10"><br /> <input type ="Text" size="4" name="max11"><br /> <input type ="Text" size="4" name="max12"><br /> <br /> </td> <td align="center"> <br /> <input type ="Text" size="4" name="mand10"><br /> <input type ="Text" size="4" name="mand11"><br /> <input type ="Text" size="4" name="mand12"><br /> <br /> </td> Quote Link to comment https://forums.phpfreaks.com/topic/263967-getting-wrong-results-on-php-function/#findComment-1352808 Share on other sites More sharing options...
smoseley Posted June 11, 2012 Share Posted June 11, 2012 I don't see any errors in the code. It should work just fine. Quote Link to comment https://forums.phpfreaks.com/topic/263967-getting-wrong-results-on-php-function/#findComment-1352815 Share on other sites More sharing options...
smoseley Posted June 11, 2012 Share Posted June 11, 2012 The only suggestion for improvement that I can make is: $superiores = array(); $inferiores = array(); for ($i = 1; $i <= 12; $i++) { $superiores[$i] = (double) urldecode($_POST["max{$i}"]); $inferiores[$i] = (double) urldecode($_POST["mand{$i}"]); } $superiores_total = array_sum($superiores); $inferiores_total = array_sum($inferiores); Quote Link to comment https://forums.phpfreaks.com/topic/263967-getting-wrong-results-on-php-function/#findComment-1352816 Share on other sites More sharing options...
wesley_santos2002 Posted June 11, 2012 Author Share Posted June 11, 2012 That's why i'm puzzled, even being a noob on this i don't see anything wrong with the code, 1.5 + 1.5 + 1.5 = 4.5, it gives me 3, seems like the problem happens not in the function... well, I'll try to make the way you sugested, thank you for your attention. =D Wesley Santos Quote Link to comment https://forums.phpfreaks.com/topic/263967-getting-wrong-results-on-php-function/#findComment-1352826 Share on other sites More sharing options...
smoseley Posted June 11, 2012 Share Posted June 11, 2012 Your previous post said 1.5 + 1.7 = 3. Different result than 1.5+1.5+1.5 = 3. The latter post makes me believe it's a urldecoding issue. Use my code and see how it works. Quote Link to comment https://forums.phpfreaks.com/topic/263967-getting-wrong-results-on-php-function/#findComment-1352827 Share on other sites More sharing options...
wesley_santos2002 Posted June 11, 2012 Author Share Posted June 11, 2012 Problem solved, your code worked just fine, than you for you help on this man Quote Link to comment https://forums.phpfreaks.com/topic/263967-getting-wrong-results-on-php-function/#findComment-1352831 Share on other sites More sharing options...
smoseley Posted June 11, 2012 Share Posted June 11, 2012 FYI, I think the problem is that the "." was probably being urlencoded (to %3A) and not implicitly decoding on the server. So when you performed addition, it was casting the number to an (int), thus truncating everything after % symbol. I'm glad the fix worked. Quote Link to comment https://forums.phpfreaks.com/topic/263967-getting-wrong-results-on-php-function/#findComment-1352834 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.