0

I have a file in PHP to sum values of a JSON file (sum.php) and in this file i put:

    <?php

// Get json from url
$json = file_get_contents("file.json");
// Decode json into an array
//$json = json_decode($content, true);
$data =  json_decode($json, TRUE);

// Set default
$total = 0;

// Loop through the array created when decoding json
foreach ($data as $key) 
{
    // remove symbol from the end leaving only digits
    $value = substr(utf8_decode($key['initialContractualPrice']), 0, -1);
    // remove the decimal point
    $value = str_replace('.', '', $value);
    // replace the comma with a decimal point
    $value = str_replace(',', '.', $value);
    // add this number to the total value
    $total += $value;
}

echo $total;

?>

In other file that haves a button with a <input type="submit" name="soma" value="Somar Valores" class="btn btn-primary" formaction="sum.php"/> to sum.php and when i click on this buttom doesn't show result on page.

How i resolve it, can you help me?

user3332475
  • 55
  • 1
  • 2
  • 10

2 Answers2

0

You need to add action in <form> tag not in submit button

<form action="sum.php" >

 <input type="submit" name="soma" value="Somar Valores" class="btn btn-primary" />
</form>
Krish R
  • 22,583
  • 7
  • 50
  • 59
0

instead of this

    $total += $value;

User this

    $total .= $value;

because it + is plussing two numbers but you have string and number. As I understand you want to have this values together. Am I wrong?

Binier
  • 1,047
  • 2
  • 11
  • 25
  • i have a file.json with all like this [link](http://www.base.gov.pt/base2/rest/contratos?&sort(-publicationDate)) and i need to sum all values of initialContractualPrice and show it on a webpage – user3332475 Mar 02 '14 at 16:32