Frenzyy
Frenzyy

Reputation: 41

SUM of columns in a table (JSON data) in PHP

Couldn't find a specific answer to this so thought I'd ask. In short, I have a table that retrieves information from an API, based on data stored in my database and all I want to do is to get a total of certain, not all, columns from that table so that I can use them elsewhere on the site. As an example, let's use the Profit/Loss column and Total Divi. Do I have to somehow store the results as an array so that I can retrieve it elsewhere or is it something different?

<th>Profit/Loss</th>
<?php
        for($x=0;$x<$y;$x++) 
        {?>
            <tr>
                <td class="input"><?php 
                    if($pri[$x] > $lastprice[$x])
                    {
                        echo ($lastprice[$x]-$pri[$x]) * $vol[$x];
                    }
                    else if($pri[$x] < $lastprice[$x]) 
                    {
                         echo ($lastprice[$x]-$pri[$x]) * $vol[$x];
                    }
                    else
                        echo '0'; 
                    ?></td>
                 <td><?php 
                    $div = file_get_contents("https://api.iextrading.com/1.0/stock/market/batch?symbols=$symbol[$x]&types=stats&filter=dividendRate");  
                    $div = json_decode($div,TRUE);  
                    foreach($div as $divi => $value) {
                    echo $value['stats']['dividendRate']; 
                    }
                    ?></td>
                <td><?php 
                    $firstno  = floatval($vol[$x]);
                    $secondno = floatval($value['stats']['dividendRate']);
                    $sum = $firstno * $secondno;
                    print ($sum);
                    ?></td>
        </tr>
    <?php } ?>

So I only left the profit/loss row/column as well as dividend amount (2nd column) and dividend total, just so you can see how I get these numbers in the first place.

Upvotes: 1

Views: 387

Answers (1)

Rajdeep Paul
Rajdeep Paul

Reputation: 16963

Your requirement is, you want to use the profit/loss and total dividend column data of each row at the later stage of your code. What you can do is, create an empty array before the outermost for loop, and in each iteration of the loop, append the pair where key would be $x and value would be an array of profit/loss and total dividend column data.

<th>Profit/Loss</th>
<?php
    $arr = array();
    for($x=0; $x < $y; $x++){
    ?>
        <tr>
            <td class="input">
            <?php 
                $profitOrLoss = ($lastprice[$x]-$pri[$x]) * $vol[$x];
                echo $profitOrLoss;
            ?>
            </td>
            <td>
            <?php 
                $div = file_get_contents("https://api.iextrading.com/1.0/stock/market/batch?symbols=$symbol[$x]&types=stats&filter=dividendRate");  
                $div = json_decode($div,TRUE); 
                $sum = 0;
                foreach($div as $divi => $value) {
                    echo $value['stats']['dividendRate']; 
                    $sum += (floatval($vol[$x]) + floatval($value['stats']['dividendRate']));
                }
            ?>
            </td>
            <td>
            <?php 
                echo $sum;
            ?>
            </td>
        </tr>
    <?php 
        $arr[$x] = array('profitOrLoss' => $profitOrLoss, 'sum' => $sum);
    } 
?>

Update(1):

Based on your comment, all I want is to add up all the rows in Profit/Loss column together as well as rows in Total Divi column together. the solution code would be like this:

<th>Profit/Loss</th>
<?php
    $profitOrLossSum = 0;
    $dividendRateSum = 0;
    for($x=0; $x < $y; $x++){
    ?>
        <tr>
            <td class="input">
            <?php 
                $profitOrLoss = ($lastprice[$x]-$pri[$x]) * $vol[$x];
                $profitOrLossSum += $profitOrLoss;
                echo $profitOrLoss;
            ?>
            </td>
            <td>
            <?php 
                $div = file_get_contents("https://api.iextrading.com/1.0/stock/market/batch?symbols=$symbol[$x]&types=stats&filter=dividendRate");  
                $div = json_decode($div,TRUE); 
                $sum = 0;
                foreach($div as $divi => $value) {
                    echo $value['stats']['dividendRate']; 
                    $sum += (floatval($vol[$x]) + floatval($value['stats']['dividendRate']));
                    $dividendRateSum += floatval($value['stats']['dividendRate']);
                }
            ?>
            </td>
            <td>
            <?php 
                echo $sum;
            ?>
            </td>
        </tr>
    <?php 
    } 
    $arr = array('profitOrLossSum' => $profitOrLossSum, 'dividendRateSum' => $dividendRateSum);
?>

Sidenote: If you want to see the complete array structure, do var_dump($arr);

Upvotes: 1

Related Questions