0

I need help to display result a sum of values of a json file that's like this:

http://www.base.gov.pt/base2/rest/contratos?&sort(-publicationDate)

I have this code in php to do that:

                         (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>

And i need to display the sum value of 'initialContractualPrice' from the json file, to do this i write this code in other file that i name 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;

?>

My question is how can i do for the page do the sum without redirect to the sum.php but do it and print the result in estat.php

I can change the type of sum.php to js or jquery if are more easy to do that.

Thank you very much.

user3332475
  • 55
  • 1
  • 2
  • 10
  • you could just include your page in the "estat.php" page, or doing it with an ajax call; – MamaWalter Mar 02 '14 at 18:00
  • but if i do with include estat.php show the value when i open the page and i need only when click on a button, or i do the wrong way? @MamaWalter – user3332475 Mar 02 '14 at 18:05
  • if your json file will never change, you could calculate your sum when you load the page and just hide the value. And with `javascript` just show the value when the button is clicked. – MamaWalter Mar 02 '14 at 18:09
  • yes that it, how can i do it in code? @MamaWalter – user3332475 Mar 02 '14 at 18:10

1 Answers1

0

as you did, include your sum.php at the beggining of your file estat.php. Hide the sum value with 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;
}

and with jQuery (as you tag it) put a click handler on your button:

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

FIDDLE DEMO

Alternative, if you dont want to calculate the sum when the page is loaded, you can call your sum.php with an AJAX call when the button is clicked.

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

and in sum.php, echo $total; at the end.

MamaWalter
  • 2,073
  • 1
  • 18
  • 27
  • what did you try ? do you have an error ? in the console ? – MamaWalter Mar 02 '14 at 19:06
  • Uncaught ReferenceError: $ is not defined – user3332475 Mar 02 '14 at 19:14
  • you tag your post with Jquery so i assume you use it. with my example you have to import [Jquery](http://jquery.com/download/). – MamaWalter Mar 02 '14 at 19:21
  • and show this error in console event.returnValue is deprecated. Please use the standard event.preventDefault() instead. – user3332475 Mar 02 '14 at 19:27
  • your last comment is not a error but a warning of chrome i think, see this post for more details: http://stackoverflow.com/questions/20045162/event-returnvalue-is-deprecated-please-use-the-standard-event-preventdefault – MamaWalter Mar 03 '14 at 00:08