Netchain Posted October 26, 2019 Share Posted October 26, 2019 (edited) Hi all Would like to ask for help . Generally I would like to run a webpage which will display actual price + price movements of selected cryptocurrencies. I´m recieving data from extrernal API. (JSON) . I already integrated currency function (usd), but I need to add also function for showing percent (%) change . (at "24 hour change" section) This is how code looks like: <?php $json = file_get_contents('https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd&include_market_cap=true;include_24hr_vol=true;include_24hr_change=true'); $bitcoinPrice = json_decode($json); $json = file_get_contents('https://api.coingecko.com/api/v3/simple/price?ids=dash&vs_currencies=usd&include_market_cap=true;include_24hr_vol=true;include_24hr_change=true'); $dashPrice = json_decode($json); $json = file_get_contents('https://api.coingecko.com/api/v3/simple/price?ids=netchain&vs_currencies=usd&include_market_cap=true;include_24hr_vol=true;include_24hr_change=true'); $netchainPrice = json_decode($json); $json = file_get_contents('https://api.coingecko.com/api/v3/simple/price?ids=pivx&vs_currencies=usd&include_market_cap=true;include_24hr_vol=true;include_24hr_change=true'); $pivxPrice = json_decode($json); function formatter($amount) { // $formatter = new NumberFormatter('en_US', NumberFormatter::CURRENCY); return '$'.number_format($amount, 2);; } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <meta http-equiv="x-ua-compatible" content="ie=edge"> <title>Masternodes monitor</title> <!-- Font Awesome --> <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.2/css/all.css"> <!-- Bootstrap core CSS --> <link href="css/bootstrap.min.css" rel="stylesheet"> <!-- Material Design Bootstrap --> <link href="css/mdb.min.css" rel="stylesheet"> <!-- Your custom styles (optional) --> <link href="css/style.css" rel="stylesheet"> <!-- MDBootstrap Datatables --> <link href="css/addons/datatables.min.css" rel="stylesheet"> </head> <body> <table id="dtBasicExample" class="table table-striped table-bordered table-sm" cellspacing="0" width="100%"> <thead> <tr> <th class="th-sm">Coin name </th> <th class="th-sm">ROI </th> <th class="th-sm">Daily income </th> <th class="th-sm">Price </th> <th class="th-sm">24 change </th> <th class="th-sm">Volume </th> <th class="th-sm">Marketcap </th> <th class="th-sm">Node price </th> <th class="th-sm">Coins for MN </th> <th class="th-sm">Nodes </th> </tr> </thead> <tbody> <tr> <td><img src="/img/btc.png" width="15" height="15" title="Bitcoin logo" alt="Bitcoin logo" /> Bitcoin (BTC) </td> <td>Unknown</td> <td>Unknown</td> <td><?=formatter($bitcoinPrice->bitcoin->usd) ?></td> <td><?=formatter($bitcoinPrice->bitcoin->usd_24h_change) ?></td> <td><?=formatter($bitcoinPrice->bitcoin->usd_24h_vol) ?></td> <td><?=formatter($bitcoinPrice->bitcoin->usd_market_cap) ?></td> <td>Unknown</td> <td>Unknown</td> <td>Unknown</td> </tr> <tr> <td><img src="/img/pivx.png" width="15" height="15" title="Pivx logo" alt="Pivx logo" /> Pivx (PIVX)</td> <td>Unknown</td> <td>Unknown</td> <td><?=formatter($pivxPrice->pivx->usd) ?></td> <td><?=formatter($pivxPrice->pivx->usd_24h_change) ?></td> <td><?=formatter($pivxPrice->pivx->usd_24h_vol) ?></td> <td><?=formatter($pivxPrice->pivx->usd_market_cap) ?></td> <td>Unknown</td> <td>10,000</td> <td>1,536</td> </tr> <tr> <td><img src="/img/dash.png" width="15" height="15" title="Dash logo" alt="Dash logo" /> Dash (DASH)</td> <td>7.5%</td> <td>14,40 $</td> <td><?=formatter($dashPrice->dash->usd) ?></td> <td><?=formatter($dashPrice->dash->usd_24h_change) ?></td> <td><?=formatter($dashPrice->dash->usd_24h_vol) ?></td> <td><?=formatter($dashPrice->dash->usd_market_cap) ?></td> <td>$69,696.22</td> <td>1000</td> <td>4001</td> </tr> <tr> <td><img src="/img/ntx.png" width="15" height="15" title="Netchain logo" alt="Netchain logo" /> Netchain (NTX) </td> <td>350%</td> <td>0,12 $</td> <td>$0.00004</td> <td><?=formatter($netchainPrice->netchain->usd_24h_change) ?></td> <td><?=formatter($netchainPrice->netchain->usd_24h_vol) ?></td> <td><?=formatter($netchainPrice->netchain->usd_market_cap) ?></td> <td>$7</td> <td>100.000</td> <td>55</td> </tr> </tbody> <tfoot> <tr> <th>Coin name </th> <th>ROI </th> <th>Daily income </th> <th>Price </th> <th>24 change </th> <th>Volume </th> <th>Marketcap </th> <th>Node price </th> <th>Coins for MN </th> <th>Nodes </th> </tr> </tfoot> </table> <!-- SCRIPTS --> <!-- JQuery --> <script type="text/javascript" src="js/jquery-3.4.1.min.js"></script> <!-- Bootstrap tooltips --> <script type="text/javascript" src="js/popper.min.js"></script> <!-- Bootstrap core JavaScript --> <script type="text/javascript" src="js/bootstrap.min.js"></script> <!-- MDB core JavaScript --> <script type="text/javascript" src="js/mdb.min.js"></script> <!-- MDBootstrap Datatables --> <script type="text/javascript" src="js/addons/datatables.min.js"></script> <script> $(document).ready(function () { $('#dtBasicExample').DataTable(); $('.dataTables_length').addClass('bs-select'); setTimeout(function() { location.reload(); }, 900000); }); </script> </body> </html> I need section usd_24h_change in percent %. How to do that? I tryed : <td><($bitcoinPrice->dash->usd_24h_change) ? % ></td> <td><($dashPrice->dash->usd_24h_change) ? % ></td> <td><($netchainPrice->dash->usd_24h_change) ? % ></td> <td><($pivxPrice->dash->usd_24h_change) ? % ></td> But it doesn´t work… Please help Edited October 26, 2019 by Netchain Quote Link to comment https://forums.phpfreaks.com/topic/309418-php-need-to-change-currency-function-to-percentual-at-one-line/ Share on other sites More sharing options...
Barand Posted October 26, 2019 Share Posted October 26, 2019 percent = (amount_changed / yesterdays_closing_price) * 100 Quote Link to comment https://forums.phpfreaks.com/topic/309418-php-need-to-change-currency-function-to-percentual-at-one-line/#findComment-1571025 Share on other sites More sharing options...
requinix Posted October 26, 2019 Share Posted October 26, 2019 1 hour ago, Barand said: percent = (amount_changed / yesterdays_closing_price) * 100 Deja-vu. Quote Link to comment https://forums.phpfreaks.com/topic/309418-php-need-to-change-currency-function-to-percentual-at-one-line/#findComment-1571027 Share on other sites More sharing options...
Netchain Posted October 26, 2019 Author Share Posted October 26, 2019 How this can help me? Quote Link to comment https://forums.phpfreaks.com/topic/309418-php-need-to-change-currency-function-to-percentual-at-one-line/#findComment-1571028 Share on other sites More sharing options...
requinix Posted October 27, 2019 Share Posted October 27, 2019 I'm not sure it can help you... What is the amount changed since yesterday? Do you have that value? What is yesterday's price? Do you have that value? Do you possibly have a way of calculating that value? What is 100? Quote Link to comment https://forums.phpfreaks.com/topic/309418-php-need-to-change-currency-function-to-percentual-at-one-line/#findComment-1571036 Share on other sites More sharing options...
Netchain Posted October 27, 2019 Author Share Posted October 27, 2019 (edited) 4 hours ago, requinix said: I'm not sure it can help you... What is the amount changed since yesterday? Do you have that value? What is yesterday's price? Do you have that value? Do you possibly have a way of calculating that value? What is 100? I already have that value. (24hr_change) https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd&include_market_cap=true;include_24hr_vol=true;include_24hr_change=true I just need to somehow add symbol "%"..... Edited October 27, 2019 by Netchain Quote Link to comment https://forums.phpfreaks.com/topic/309418-php-need-to-change-currency-function-to-percentual-at-one-line/#findComment-1571040 Share on other sites More sharing options...
Barand Posted October 27, 2019 Share Posted October 27, 2019 Have you tried echo $change . '%'; Quote Link to comment https://forums.phpfreaks.com/topic/309418-php-need-to-change-currency-function-to-percentual-at-one-line/#findComment-1571042 Share on other sites More sharing options...
maxxd Posted October 27, 2019 Share Posted October 27, 2019 <td><?= $bitcoinPrice->dash->usd_24h_change ?>%</td> <td><?= $dashPrice->dash->usd_24h_change ?>%</td> <td><?= $netchainPrice->dash->usd_24h_change ?>%</td> <td><?= $pivxPrice->dash->usd_24h_change ?>%</td> Your markup is malformed - use this instead. Quote Link to comment https://forums.phpfreaks.com/topic/309418-php-need-to-change-currency-function-to-percentual-at-one-line/#findComment-1571048 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.