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>
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.
I think your array is empty, in the first step you have only created the array but you did't assign any values to that arrays. you are just dividing blank array by blank array therefor you get a 0, think about that.