0

I'm trying to build something like https://coinranking.com/. I plan on doing this by sorting the results from the API at http://www.coincap.io/front using the position24 value as sorting parameter. So far, I managed to pull an ordered list with the prices, but I'd like to display the values from other objects (volume, mktcap, etc) as well in a table. Would I have to repeat this function for every object?

<php? 
function compare($a, $b) {
    return intval($a->position24) - intval($b->position24);
}

$json = file_get_contents('http://www.coincap.io/front');
$data = json_decode($json);
usort($data, 'compare');
foreach ($data as $item) {
    echo $item->price . "\n"<;
}
?>

2 Answers 2

1

You can just call it. For example like this:

echo $item->volume;
echo $item->mktcap;

in a table that would look like this:

<table>
    <thead>
        <tr>
            <th>Name</th>
            <th>Price</th>
            <th>Market Capitalization</th>
            <th>Volume</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach($data as $row) { ?>
        <tr>
            <td><?= $row->long; ?></td>
            <td><?= $row->price; ?></td>
            <td><?= $row->mktcap; ?></td>
            <td><?= $row->volume; ?></td>
        </tr>
        <?php } ?>
    </tbody>
</table>
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you, really. Working like a charm.
One more thing: is it possible to make a value (eg: price) display only 2 decimals? I tried 'number_format((float) 2, '.', '')', but that didn't work.
It is possible. But PHP is not like Java, so you should use a different function to convert the variable. So to do that you should use number_format(floatval($price), 2, '.','').
1

usort expecting array as parameter your $data is object maybe you should use json_decode($json,true); and

function compare($a, $b) {
    return intval($a['position24']) - intval($b['position24']);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.