0

PHP code

<?php

$url = "https://www.bitstamp.net/api/ticker/";
$fgc = file_get_contents($url);
$json = json_decode($fgc, true);

$price = $json["last"];
$high = $json["high"];
$low = $json["low"];
$date = date("m-d-Y - h:i:sa");
$open = $json["open"];

if($open < $price) 
{
    // price went up
    $indicator = "+";
    $change = $price - $open;
    $percent = $change / $open;
    $percent = $percent * 100;
    $percentChange = $indicator.number_format($percent, 2);
    $color = "green";
}

if($open > $price) 
{
    // price went up
    $indicator = "-";
    $change = $open - $price;
    $percent = $change / $open;
    $percent = $percent * 100;
    $percentChange = $indicator.number_format($percent, 2);
    $color = "red";
}

$table = <<<EOT
            <table>
                <tr>
                    <td rowspan="3" width="60%" id="lastPrice">$$price</td>
                    <td align="right" style="color: $color;">$percentChange%</td>
                </tr>
                <tr>
                    <td align="right">$$high </td>
                    <td align="right">$$low </td>
                </tr>
                <tr>
                    <td colspan="2" align="right" id="timeDate">$date</td>
                </tr>
            </table>
EOT;

echo $table;
?>

Html and JS

<html>
    <head>
        <title> Bitcoin Widget </title>
        <link rel="stylesheet" type="text/css"href="css/style.css">
        <script src="https://code.jquery.com/jquery-3.2.1.js" integrity="sha256-DZAnKJ/6XZ9si04Hgrsxu/8s717jcIzLy3oi35EouyE=" crossorigin="anonymous"></script>
    </head>
    <body>
        <div id="container">

        </div>
        <script>
                $('document').ready(function(){
                    refreshData();
                })

                function refreshData() {
                    $('#container').load("data.php", function(){
                        setTimeout(refreshData, 10000);
                    });
                }
            </script>
    </body>
</html>

Now I want to include this price ticker in another webpage. So, I copied the JS and pasted it in the corresponding HTML file and also copied the PHP file in the root directory but nothing is being displayed.

How can I include this widget in another HTML file.

Will
  • 69
  • 10

1 Answers1

0

Create index.html and copy html code

Create data.php and copy php code

Structure like

Your project folder
- data.php
- index.html
- css / style.css
dhruv jadia
  • 1,684
  • 2
  • 15
  • 28