1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Display array results from an API inside of a table, based on stored database values.

Discussion in 'HTML & Website Design' started by Frenzyy, Jan 21, 2018.

  1. #1
    Not sure how to exactly ask this question so let me try to explain. I'm trying to display dividend amount data for each stock (stock symbol stored in db) inside of a table. This dividend data is coming from the IEX API and while I can echo out the results normally and just get them in a list, like this: FB : 0 GOOGL : 0 AAPL : 2.52 I don't know how to display them inside of a table row, for each of those stocks. I just get AAPL : 2.52 for all rows. So it essentially seems like at some point it doesn't know what data to get for what stock and just prints AAPL for all three. This is the code (some unrelated hidden to make it shorter):
    
    <?php
    // Initialising variables to store extracted information
    $name =[];
    $symbol =[];
    $open =[];
    $close =[];
    $high =[];
    $low =[];
    $lastprice =[];
    $y =0;
    $z ='';
    $key ="93dcc722279c3a7577f248b09ef6167f";
    
    $memberid = $_SESSION['memberID'];
    $sql ="SELECT * FROM portfolio WHERE memberID = $memberid";
    $result = mysqli_query($conn, $sql);
    
    // Check if databse is emptyif(mysqli_num_rows($result)>0){while($row = mysqli_fetch_assoc($result)){
    $sym[$y]= $row["stocks_symbol"];
    $pri[$y]= $row["price"];
    $vol[$y]= $row["quantity"];
    $id[$y]= $row["memberid"];
    $y +=1;}}// If database emptyelse{?><h1><center><?php
    echo "Portfolio Empty";?></h1></center><?php
    die();}
    mysqli_close($conn);
    
    // Adding all stock names in one variable to enable API callfor($a=0;$a<$y;$a++){
    $z = $z.$sym[$a].',';}
    
    $z = rtrim($z,",");
    
    
    // API call
    $contents = file_get_contents("http://marketdata.websol.barchart.com/getQuote.json?key=$key&symbols=$z&mode=R");
    $contents = json_decode($contents,true);
    
    $div = file_get_contents("https://api.iextrading.com/1.0/stock/market/batch?symbols=$z&types=stats&filter=dividendRate");
    $div = json_decode($div,TRUE);foreach($div as $divi => $value){
    echo '<br/>'. $divi.' : '. $value['stats']['dividendRate'];}
    
    // Check successfull API callif($contents["status"]["code"]==200){foreach($contents['results']as $result){
    array_push($name,$result['name']);
    array_push($symbol,$result['symbol']);
    array_push($open,$result['open']);
    array_push($close,$result['close']);
    array_push($high,$result['high']);
    array_push($low,$result['low']);
    array_push($lastprice,$result['lastPrice']);}
    
    }// If API call unsuccessfulelse{?><h1><center>"Error retreiving data. Please try again later."</center></h1><?phpdie();}?><!-- Generating Output in tabular format --><tableclass='table table-responsive'><trclass='head warning'><td>Name</td><td>Symbol</td><td>Open</td><td>Close</td><td>High</td><td>Low</td><td>Last Price</td><td>Price Bought</td><td>Quantity</td><td>Change Per Stock</td><td>Profit/Loss</td><td>Advanced Data</td><td>Dividend</td></tr><?phpfor($x=0;$x<$y;$x++){?><tr><td><?php echo $name[$x];?></td><td><?php echo $symbol[$x];?></td><td><?php echo $open[$x];?></td><td><?php echo $close[$x];?></td><td><?php echo $high[$x];?></td><td><?php echo $low[$x];?></td><td><?php echo $lastprice[$x];?></td><td><?php echo $pri[$x];?></td><td><?php echo $vol[$x];?></td><td><?php if($pri[$x]> $lastprice[$x]){?><iclass="fa fa-arrow-down"><?php echo $lastprice[$x]-$pri[$x];}elseif($pri[$x]< $lastprice[$x]){?></i><iclass="fa fa-arrow-up"><?php echo $lastprice[$x]-$pri[$x];?></i><?php}else
    echo '0';?></td>
    
    <td><?php if($pri[$x]> $lastprice[$x]){?><iclass="fa fa-arrow-down"><?php echo ($lastprice[$x]-$pri[$x])* $vol[$x];}elseif($pri[$x]< $lastprice[$x]){?></i><iclass="fa fa-arrow-up"><?php echo ($lastprice[$x]-$pri[$x])* $vol[$x];?></i><?php}else
    echo '0';?></td><td><center><buttonid="opener1"class='btn btn-success'>Click to view advanced data</button></center><divid="dialog1"title="Advanced Company Data">Company Description:<br></div><script>
    $("#dialog1").dialog({
    autoOpen:false});
    $("#opener1").click(function(){
    $("#dialog1").dialog("open","modal",true);});</script></td>
    
    <td><?php echo '<br/>'. $divi.' : '. $value['stats']['dividendRate'];?></td>
    
    </tr><?php }?></table>
    
    Code (markup):
    In short, I'm asking about the last 'td' in the table and how to change it so that it displays the dividend amount for the correct symbol in the row, not AAPL for all.
    SEMrush
     
    Frenzyy, Jan 21, 2018 IP
    SEMrush
  2. deathshadow

    deathshadow Acclaimed Member

    Messages:
    8,651
    Likes Received:
    1,561
    Best Answers:
    225
    Trophy Points:
    515
    #2
    You have your foreach commented out... was that a goof in trying to remove that 'extra' code?

    Really what you've shown is a broken mess of how not to use PHP or HTML.

    On the PHP side you're dropping in and out of the language for no good reason, dumping the values as you loop through them into multiple arrays for Christmas only knows what, creating endless variables for nothing, performing a while on what I would think is a single result, checking number of rows for NOTHING instead of doing a result pull, slopping a variable into your query string (you're using mysqli, USE IT!), etc, etc...

    On the HTML side you're using the <center> tag that has ZERO business on ANY website written ANY time of the past TWO DECADES! You are missing spaces between tagnames and their id's resulting in outright gibberish, <button> with no form, type, or identification, IF those were to be inside a proper loop you'd end up with multiple instances of the same ID (id's must remain unique, you can only use them ONCE), slopping out multiple instances of the same scripting (that doesn't even belong in the markup). numbered headings inside <td> which is gibberish semantics (and probably gibberish document structure)...

    That's time to toss that and start over. Could you provide a sample of the data being retrieved and what you are trying to have it output for markup and style?
     
    deathshadow, Jan 21, 2018 IP
  3. Frenzyy

    Frenzyy Peon

    Messages:
    16
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #3
    Thanks for the constructive feedback, long way ahead of me looks like. That said, I'm planning on making the code/structure more efficient after I get all the functionality done right. Not sure if that's the correct way to go about it but that's the plan anyway.
    Let me show you a screenshot of the table, the data it retrieves and what I'm looking to add to it.
    On the screenshot, you can see the type of data I'm retrieving and the list of the correct values that it's retrieving in a list above the table, with the use of echo but then not being able of getting that same result in the actual table, instead getting just the numbers for AAPL, in all rows.
    Screenshot_1.png
    API part:
    
    $div = file_get_contents("https://api.iextrading.com/1.0/stock/market/batch?symbols=$z&types=stats&filter=dividendRate"); 
        $div = json_decode($div,TRUE); 
        foreach($div as $divi => $value) {
        echo '<br/>'. $divi.' : '. $value['stats']['dividendRate'];
        }
    
    Code (markup):
    Table part:
    
    <td>
                        <?php echo '<br/>'. $divi.' : '. $value['stats']['dividendRate']; ?>
    </td>
    
    Code (markup):
    So is there any simple solution to this? Essentially, I just want to have the functionality of that loop inside of that table column but not sure how to do it.
    Hope that helps and thanks for your help.
     
    Frenzyy, Jan 22, 2018 IP