2014-03-02 139 views
0

我需要幫助的顯示效果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如果更容易做到這一點。

非常感謝。

+0

你可以在「estat.php」頁面中包含你的頁面,或者用ajax調用它; – MamaWalter

+0

但如果我與包括estat.php顯示值,當我打開頁面,我只需要點擊一個按鈕時,或者我做錯了方式? @MamaWalter – user3332475

+0

如果你的json文件永遠不會改變,你可以計算你的總和,當你加載頁面,只是隱藏的價值。而'javascript'只顯示點擊按鈕時的值。 – MamaWalter

回答

0

和你一樣,包括你的sum.php在你的文件estat.php的開始。用CSS隱藏總和值。

<input id="sum-btn" type="submit" name="soma" value="Somar Valores" class="btn btn-primary" formaction="action_sum.php"/> 
<p id="sum"><?php echo $total; ?></p> 

CSS:

#sum { 
    display:none; 
} 

和使用jQuery(當你標記它)把單擊處理程序上的按鈕:

<script> 
$(document).ready(function() { 
    $('#sum-btn').click(function() { 
     $('#sum').show(); 
    }); 
}); 
</script> 

FIDDLE DEMO

選擇,如果你不想要在頁面加載時計算總和,可以使用調用sum.php當按鈕被點擊時。

<script> 
$(document).ready(function() { 

    $('#sum-btn').click(function() { 

     var request = $.ajax({ 
      url: "sum.php", 
      type: "GET" 
     }); 
     request.done(function(value) { 
      $('#sum').text(value); 
      $('#sum').show(); 
     }); 
     request.fail(function(jqXHR, textStatus) { 
      alert("Request failed: " + textStatus); 
     }); 

    }); 
}); 
</script> 

和sum.php,echo $total;末。

+0

我試了一下,但沒有在div上顯示任何結果 – user3332475

+0

你試過了什麼?你有錯誤嗎?在控制檯中? – MamaWalter

+0

很多錯誤 – user3332475