douyin7416 2013-11-07 19:57
浏览 79
已采纳

PHP计算和引导进度条

This is part of my code to calculate how many players there are on a minecraft server right now and return that value in percent to the Bootstrap Progress bar so it can do the stuff it needs to do.

$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>

Now my problem is, The $percentage returns 0 all the time. But I don't know why.

This is my full file:

<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';

I hope someone can come to a solution.

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>
  • 写回答

2条回答 默认 最新

  • doujinyi1267 2013-11-08 21:42
    关注

    I'm not sure how those values are created. If you have access to the script that returns $playerson = $data[4] and $maxplayers = $data[5] then you may be able to fix the problem there.

    If not try...

    $playerson = (int)preg_replace( '/[^0-9]/', '', $data[4]);
    $maxplayers = (int)preg_replace( '/[^0-9]/', '', $data[5]);
    
    $percentage = floor($playerson / $maxplayers) * 100; // floor (round down) optional
    

    Basically replaces everything in the "string" $data[4] & $data[5] with 'nothing' so only numbers 0-9 are left and typecast to integer for good measure.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器