我需要幫助的顯示效果JSON文件的值的總和即是這樣的JSON文件的總和值的結果:按鈕顯示
http://www.base.gov.pt/base2/rest/contratos?&sort(-publicationDate)
我有這樣的代碼在PHP這樣做:
(name of this file: estat.php)
<?php
//include "sum.php";
$x="active";
$y='';
if(isset($_GET['tabsoft'])){
$y="active";
$x="";
}
?>
<html>
<?php include 'cab.php'; ?>
<body>
<?php include 'menu.php'; ?>
<div class="container">
<div calss="row">
<ul id="tabs" class="nav nav-tabs" data-tabs="tabs">
<li class="<?php echo $x ?>"><a href="#val" data-toggle="tab">Valor Total Gasto</a></li>
<li class="<?php echo $y ?>"><a href="#emp" data-toggle="tab">Número de Empresas Envolvidas</a></li>
</ul>
<div id="my-tab-content" class="tab-content">
<div class="tab-pane <?php echo $x ?> " id="val">
<p>
<form>
<h2>Dinheiro gasto nos contratos</h2>
<input type="submit" name="soma" value="Somar Valores" class="btn btn-primary" formaction="action_sum.php"/>
<!-- <p><?php echo $total; ?></p> -->
</form>
</p>
</div>
<div class="tab-pane <?php echo $y ?> " id="emp">
</div>
</div>
</div>
</div>
<?php include 'rodape.php';?>
</body>
</html>
,我需要從JSON文件顯示「initialContractualPrice」的和值,爲此,我寫這篇文章的代碼在其他文件中,我sum.php名稱:
<?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
global $total;
// 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;
?>
我的問題是如何對網頁做做的款項,但不得重定向到sum.php,但做到這一點,在estat.php打印結果
我可以改變sum.php對JS的類型或jQuery如果更容易做到這一點。
非常感謝。
你可以在「estat.php」頁面中包含你的頁面,或者用ajax調用它; – MamaWalter
但如果我與包括estat.php顯示值,當我打開頁面,我只需要點擊一個按鈕時,或者我做錯了方式? @MamaWalter – user3332475
如果你的json文件永遠不會改變,你可以計算你的總和,當你加載頁面,只是隱藏的價值。而'javascript'只顯示點擊按鈕時的值。 – MamaWalter