这是我的代码的一部分,用于计算我的世界服务器上现在有多少玩家,并将该值以百分比形式返回到 Bootstrap 进度条,以便它可以做它需要做的事情。
$playerson = $data[4];
$maxplayers = $data[5];
$percentage = ($playerson / $maxplayers) * 100;
echo $playerson;
echo $maxplayers;
?>
<div class="container">
<div class="progress">
<div class="progress-bar" role="progressbar" style="width: <?php echo $percentage; ?>%;">
</div>
</div>
</div>
现在我的问题是, $percentage 一直返回 0。但我不知道为什么。
这是我的完整文件:
<html>
<head>
<title>Bootstrap 101 Template</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Bootstrap -->
<link href="css/bootstrap.min.css" rel="stylesheet" media="screen">
</head>
<BODY>
<?php
// your servers ip
$ip= 'hub.exclusivecraft.org';
// your servers port
$port = '25565';
我希望有人能提出解决方案。
function mc_status($host,$port) {
$fp = fsockopen($host,$port,$errno,$errstr,$timeout=10);
fputs($fp, "\xFE\x01");
$response = '';
$response .= fgets($fp);
fclose($fp);
$response = explode("\x00\x00", $response);
return $response;
}
$data = mc_status($ip,$port);
$playerson = $data[4];
$maxplayers = $data[5];
$percentage = ($playerson / $maxplayers) * 100;
echo $playerson;
echo $maxplayers;
?>
<div class="container">
<div class="progress">
<div class="progress-bar" role="progressbar" style="width: <?php echo $percentage; ?>%;">
</div>
</div>
</div>
<script src="http://code.jquery.com/jquery.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>