Jump to content

Getting wrong results on php function


Recommended Posts

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

Link to comment
Share on other sites

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 "."

Link to comment
Share on other sites

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>

 

 

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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.

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.