我制作了一个简单的scipt,用于从API获取数据。 Evryhing工作正常,但如果价格差异为负或正面,我想添加箭头。
类似的东西:
+5,15⬆ -2,44⬇
有人能给我一个提示怎么做吗?谢谢!
$url = "https://min-api.cryptocompare.com/data/pricemultifull?fsyms=BTC,ETH,XRP,LTC,BCH&tsyms=USD&e=Bitstamp";
$data = json_decode(file_get_contents($url), true);
// get btc price
echo "BTC: ";
echo "<span class='price'>";
echo $data['DISPLAY']['BTC']['USD']['PRICE'] . " ";
echo "</span>";
// get btc difference
echo "<span class='difference'>";
echo $data['DISPLAY']['BTC']['USD']['CHANGEPCT24HOUR'] . "%";
echo "</span>";
echo "<hr>";
答案 0 :(得分:1)
我会添加一项检查,看看它是否大于这样:
if($data['DISPLAY']['BTC']['USD']['CHANGEPCT24HOUR'] > 0){
echo '⬆';
}else{
echo '⬇';
}
整个脚本看起来像:
$url = "https://min-api.cryptocompare.com/data/pricemultifull?fsyms=BTC,ETH,XRP,LTC,BCH&tsyms=USD&e=Bitstamp";
$data = json_decode(file_get_contents($url), true);
// get btc price
echo "BTC: ";
echo "<span class='price'>";
echo $data['DISPLAY']['BTC']['USD']['PRICE'] . " ";
echo "</span>";
// get btc difference
echo "<span class='difference'>";
if($data['DISPLAY']['BTC']['USD']['CHANGEPCT24HOUR'] > 0){
echo '⬆';
}else{
echo '⬇';
}
echo $data['DISPLAY']['BTC']['USD']['CHANGEPCT24HOUR'] . "%";
echo "</span>";
echo "<hr>";
答案 1 :(得分:0)
$change = (float) $data['DISPLAY']['BTC']['USD']['CHANGEPCT24HOUR'];
$change_increased = $change > 0;
$change_decreased = $change < 0;
$change_increased
&& print('+' . $change . '⬆');
$change_decreased
&& print($change . '⬇');
我假设CHANGEPCT24HOUR是您要输出的变量
还有一个问题是API输入是否被视为用户输入以及您希望保护自己的程度,但我认为这是另一个问题/答案