3 Replies - 450 Views - Last Post: 29 January 2018 - 04:51 AM Rate Topic: -----

#1 Yhym   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 08-December 13

SUM from columns in a table (JSON data)

Posted 28 January 2018 - 10:18 AM

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. Thanks in advance!

Is This A Good Question/Topic? 0
  • +

Replies To: SUM from columns in a table (JSON data)

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14239
  • View blog
  • Posts: 57,130
  • Joined: 12-June 08

Re: SUM from columns in a table (JSON data)

Posted 28 January 2018 - 11:17 AM

I think putting the data into a collection or an array would be a good first step.
Was This Post Helpful? 1
  • +
  • -

#3 Yhym   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 08-December 13

Re: SUM from columns in a table (JSON data)

Posted 28 January 2018 - 11:50 AM

View Postmodi123_1, on 28 January 2018 - 11:17 AM, said:

I think putting the data into a collection or an array would be a good first step.

Ok, thanks. So this is what I have now and while it's definitely a step in the right direction, it doesn't work fully because for now, it just displays the results from the last row. So essentially, it doesn't add up all the values in that column. What would be the best way to do that?
<?php
	$arr = array();
		for($x=0;$x<$y;$x++) 
		{?>
			<tr>
<td class="input"><?php 
					if($pri[$x] > $lastprice[$x])
					{
						$pnl= ($lastprice[$x]-$pri[$x]) * $vol[$x];
						echo $pnl;
					}
					else if($pri[$x] < $lastprice[$x]) 
					{
						 $pnl= ($lastprice[$x]-$pri[$x]) * $vol[$x];
						 echo $pnl;
					}
					else
						echo '0'; 
					?></td> 

etc
 $arr[$x] = array('pnl' => $pnl, 'sum' => $sum);

Here's the var_dump if that helps:
array(1) { [4]=> array(2) { ["pnl"]=> float(665) ["sum"]=> float(73) } }
Was This Post Helpful? 0
  • +
  • -

#4 Yhym   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 08-December 13

Re: SUM from columns in a table (JSON data)

Posted 29 January 2018 - 04:51 AM

Problem fixed. Just had to put things into arrays and then properly loop through them.
Thanks for your help!
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1