PHP Show how many players online
I am in the middle of adding PHP functions to my website for GunZ, and I am trying to figure out the players online part. I just want it to show '$players are currently online'
I tried this:
PHP Code:
<?php
$players = "SELECT CurrPlayer Opened FROM ServerStatus')";
?>
But nothing showed up, do I then need to add a certain part of code to the file outside of the PHP code?
I have tried looking at other server status releases here but with no luck into it answering my question.
Re: PHP Show how many players online
Just did this earlier, not sure if there are any syntax errors though. Feel free to test.
PHP Code:
<?php
$query = mssql_query ("SELECT CurrPlayer FROM ServerStatus WHERE Opened = 1");
if (mssql_num_rows($query) == 0)
{
echo "There are no servers available.";
return;
}
$totalplayers = 0;
while ($fetch = mssql_fetch_assoc($query))
{
$totalplayers += $fetch[CurrPlayer];
}
echo "There are ". $totalplayers ." players online.";
?>
Re: PHP Show how many players online
PHP Code:
$players = 0;
$q = mssql_query("SELECT CurrPlayer FROM ServerStatus WHERE Opened = 1");
while($b = mssql_fetch_array($q))
{
$players += $b['CurrPlayer'];
}
echo $players;
EDIT: lol posted together.
Re: PHP Show how many players online
Not tested but is suposed to work.
PHP Code:
<?php
servercount = $servercount + $srv['CurrPlayer']
?>
and for displaying in text
PHP Code:
<?=$servercount?> Players online.
Re: PHP Show how many players online
Quote:
Originally Posted by
Linear88
Just did this earlier, not sure if there are any syntax errors though. Feel free to test.
PHP Code:
<?php
$query = mssql_query ("SELECT CurrPlayer FROM ServerStatus WHERE Opened = 1");
if (mssql_num_rows($query) == 0)
{
echo "There are no servers available.";
return;
}
$totalplayers = 0;
while ($fetch = mssql_fetch_assoc($query))
{
$totalplayers += $fetch[CurrPlayer];
}
echo "There are ". $totalplayers ." players online.";
?>
Thanks, just tested it and this is the error that shows:
Fatal error: Call to undefined function mssql_query() in C:\xampp\htdocs\index.php on line 45
And on this line is:
PHP Code:
$query = mssql_query ("SELECT CurrPlayer FROM ServerStatus WHERE Opened = 1");
Re: PHP Show how many players online
Quote:
Originally Posted by
SheenUK
Thanks, just tested it and this is the error that shows:
Fatal error: Call to undefined function mssql_query() in C:\xampp\htdocs\index.php on line 45
And on this line is:
PHP Code:
$query = mssql_query ("SELECT CurrPlayer FROM ServerStatus WHERE Opened = 1");
Enable the php_mssql.dll extension in your php.ini. (Remove the ; in front of the line.)
After that, restart your Apache.
Re: PHP Show how many players online
Quote:
Originally Posted by
adz28
Not tested but is suposed to work.
PHP Code:
<?php
servercount = $servercount + $srv['CurrPlayer']
?>
and for displaying in text
PHP Code:
<?=$servercount?> Players online.
Parse error: syntax error, unexpected '=' in C:\xampp\htdocs\index.php on line 45
This is the error from this.
I will just show you my website code:
Code:
<?php
session_start();
include("secure/config.php");
include("secure/functions.php");
include("secure/anti_sql.php");
include("language/$language.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Games Anniversary</title>
<!-- meta tags -->
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta http-equiv="X-UA-Compatible" content="ie=edge,chrome=1"/>
<!-- styling -->
<link href="css/style.css" rel="stylesheet" type="text/css"/>
<!-- scripts -->
</head>
<body>
<div id="header_repeat">
<div id="header">
<img src="http://forum.ragezone.com/images/header.png" />
</div><!-- /header -->
</div><!-- /header_repeat -->
<div id="nav_bg">
<div id="nav">
<ul id="navigation">
<li><a href="index.html"><img src="http://forum.ragezone.com/images/nav/home.png" alt=""/></a></li>
<li><a href="http://europegunz.co.uk/register.php"><img src="http://forum.ragezone.com/images/nav/register.png" alt=""/></a></li>
<li><a href="#"><img src="http://forum.ragezone.com/images/nav/download.png" alt=""/></a></li>
<li><a href="http://forum.europe-gunz.eu"><img src="http://forum.ragezone.com/images/nav/forum.png" alt=""/></a></li>
<li><a href="#"><img src="http://forum.ragezone.com/images/nav/ranking.png" alt=""/></a></li>
<li><a href="#"><img src="http://forum.ragezone.com/images/nav/item_shop.png" alt=""/></a></li>
</ul><!-- /navigation -->
<div id="players_online">
<?php
servercount = $servercount + $srv['CurrPlayer']
?>
<?=$servercount?> Players online.
</div><!-- /players_online -->
</div><!-- /nav -->
</div><!-- /nav_bg -->
<div id="wrap">
<div id="sidebar">
<div id="sidebar_mid">
<div id="login_box">
<div class="divider"></div><!-- divider -->
</div><!-- /login_box -->
<div class="box">
<div class="title">
<p>» Advertisement</p>
</div><!-- /title -->
<div class="ctext">
<img src="ad-1.png" />
<div class="divider" style="margin: 0px;"></div><!-- divider -->
</div><!-- /ctext -->
</div><!-- /box -->
<div class="box">
<div class="title">
<p>» Official Sponsor</p>
</div><!-- /title -->
<div class="ctext">
<img src="sp-1.png" />
<div class="divider" style="margin: 0px;"></div><!-- divider -->
</div><!-- /ctext -->
</div><!-- /box -->
<div id="donate-img">
<a href="#"></a>
</div><!-- /donate-img -->
<div id="teamspeak-img">
<a href="#"></a>
</div><!-- /donate-img -->
<div id="game-img">
<a href="#"></a>
</div><!-- /donate-img -->
<div class="clear"></div><!-- clear -->
</div><!-- /sidebar_mid -->
<div id="sidebar_bot"></div>
</div><!-- /sidebar -->
<div id="content">
<div id="banners">
<div id="download">
<a href="#"></a>
</div><!-- /download -->
<div id="user_cp">
</div><!-- /user_cp -->
</div><!-- /banners -->
<div class="clear"></div><!-- clear -->
<div id="slider">
<script>var showfoldername='';</script><script charset="utf-8" src="show.js" type="text/javascript"></script>
</div><!-- /slider -->
<div id="base_bottom">
<div id="items">
<h1>Latest Items</h1>
<div class="divider"></div><!-- divider -->
<div style="float: left; width: 140px;">
<div class="item">
<img src="http://forum.ragezone.com/images/items/sample.png" align="left" alt=""/>
<p><strong>Item #1</strong></p>
<p>Type: Special</p>
<p>Sex: All</p>
<p>Level: 15</p>
<p>Price: G1111</p>
</div><!-- /item -->
<div class="item">
<img src="http://forum.ragezone.com/images/items/sample.png" align="left" alt=""/>
<p><strong>Item #1</strong></p>
<p>Type: Special</p>
<p>Sex: All</p>
<p>Level: 15</p>
<p>Price: G1111</p>
</div><!-- /item -->
<div class="item">
<img src="http://forum.ragezone.com/images/items/sample.png" align="left" alt=""/>
<p><strong>Item #1</strong></p>
<p>Type: Special</p>
<p>Sex: All</p>
<p>Level: 15</p>
<p>Price: G1111</p>
</div><!-- /item -->
</div>
<div style="float: right; width: 140px;">
<div class="item">
<img src="http://forum.ragezone.com/images/items/sample.png" align="left" alt=""/>
<p><strong>Item #1</strong></p>
<p>Type: Special</p>
<p>Sex: All</p>
<p>Level: 15</p>
<p>Price: G1111</p>
</div><!-- /item -->
<div class="item">
<img src="http://forum.ragezone.com/images/items/sample.png" align="left" alt=""/>
<p><strong>Item #1</strong></p>
<p>Type: Special</p>
<p>Sex: All</p>
<p>Level: 15</p>
<p>Price: G1111</p>
</div><!-- /item -->
<div class="item">
<img src="http://forum.ragezone.com/images/items/sample.png" align="left" alt=""/>
<p><strong>Item #1</strong></p>
<p>Type: Special</p>
<p>Sex: All</p>
<p>Level: 15</p>
<p>Price: G1111</p>
</div><!-- /item -->
</div>
<div class="clear"></div><!-- clear -->
</div><!-- /items -->
<div id="news">
<h1>News</h1>
<div class="divider"></div><!-- divider -->
<p>- <a href="#">- No news to display -</a></p>
<div class="clear1"></div><!-- clear1 -->
<h1>Events</h1>
<div class="divider"></div><!-- divider -->
<p>- <a href="#">- No active Events -</a></p>
<p class="hot"><a>HOT!</a> Items available!</p>
</div><!-- /news -->
</div><!-- /base_bottom -->
</div><!-- /content -->
<div class="clear" style="margin-top: 30px;"></div><!-- clear -->
<div id="footer">
<img src="http://forum.ragezone.com/images/tm_footer.png" alt="" align="left"/>
<div id="copyright">©2011 Europe GunZ.</div> All Rights Reserved.
<p><a href="#">Home</a> | <a href="http://forum.europe-gunz.eu">Forums</a> | <a href="#">Privacy Policy</a> | <a href="#">Download</a></p>
<div class="clear"></div><!-- clear -->
</div><!-- /footer -->
</div><!-- /wrap -->
</body>
</html>
config.php
Code:
$host = "*********\SQLEXPRESS";
$user = "**";
$pass = "*******";
$dbname = "GunzDB";
$connect = odbc_connect("Driver={SQL Server};Server={$host}; Database={$dbname}", $user, $pass) or die("Can't connect the MSSQL server.");
Quote:
Originally Posted by
SuperWaffle
Enable the php_mssql.dll extension in your php.ini. (Remove the ; in front of the line.)
After that, restart your Apache.
I did this when I setup xampp.
Re: PHP Show how many players online
Config for MSSQL: (Put this in your config.php)
PHP Code:
<?php
$host = "BLABLA\SQLEXPRESS";
$user = "";
$pass = "";
$dbn = "GunzDB";
$con = mssql_connect($host, $user, $pass) or die("Failed to connect!");
mssql_select_db($dbn, $con) or die("Failed to select the database!");
?>
Yes, the user and password has to be empty for MSSQL. (At least, it has to be empty to work on my host.)
Re: PHP Show how many players online
Ok i tested this so i know it works :).
PHP Code:
<?php
$q = mssql_query("SELECT * FROM ServerStatus");
$r = mssql_fetch_object($q);
$users = "<font color='#FFFFFF'>".$r->CurrPlayer."/".$r->MaxPlayer."</font>";
?>
Text displayed
PHP Code:
Players Online: <?=$users?>
It takes data from currplayer column in account and maxplayer column, like executing
Code:
SELECT CurrPlayer FROM ServerStatus
SELECT MaxPlayer FROM ServerStatus
so it works <.<
Re: PHP Show how many players online
Quote:
Originally Posted by
SuperWaffle
Config for MSSQL: (Put this in your config.php)
PHP Code:
<?php
$host = "BLABLA\SQLEXPRESS";
$user = "";
$pass = "";
$dbn = "GunzDB";
$con = mssql_connect($host, $user, $pass) or die("Failed to connect!");
mssql_select_db($dbn, $con) or die("Failed to select the database!");
?>
Yes, the user and password has to be empty for MSSQL. (At least, it has to be empty to work on my host.)
Error: Fatal error: Call to undefined function mssql_connect() in C:\xampp\htdocs\secure\config.php on line 41
Quote:
Originally Posted by
adz28
Ok i tested this so i know it works :).
PHP Code:
<?php
$q = mssql_query("SELECT * FROM ServerStatus");
$r = mssql_fetch_object($q);
$users = "<font color='#FFFFFF'>".$r->CurrPlayer."/".$r->MaxPlayer."</font>";
?>
Text displayed
PHP Code:
Players Online: <?=$users?>
It takes data from currplayer column in account and maxplayer column, like executing
Code:
SELECT CurrPlayer FROM ServerStatus
SELECT MaxPlayer FROM ServerStatus
so it works <.<
Error: Fatal error: Call to undefined function mssql_query() in C:\xampp\htdocs\index.php on line 45
Re: PHP Show how many players online
make this a new php script named "currpl.php"
PHP Code:
<?php
$q = mssql_query("SELECT * FROM ServerStatus");
$r = mssql_fetch_object($q);
$users = "<font color='#FFFFFF'>".$r->CurrPlayer."/".$r->MaxPlayer."</font>";
?>
<br />
<table>
<tr>
<td>
<font color="#FFFFFF">
Players Online:</font></td><td> <?=$users?></td>
</tr>
</table>
and in the part of the index you want to make visible the current players, just add this
PHP Code:
<?php include "currpl.php"; ?>
in a table or div frame or w.e
Re: PHP Show how many players online
Fatal error: Call to undefined function mssql_query() in C:\xampp\htdocs\currpl.php on line 2
Its kinda odd, there seems to be something wrong with the configuration yet ive removed the ; from php_mssql.dll in php.ini so what could it possibly be?
Re: PHP Show how many players online
Re: PHP Show how many players online
Quote:
Originally Posted by
alfredao
Yep i've done that, still the same error. >.>
Re: PHP Show how many players online
SheenUK, are you using XAMPP by chance? They removed support for MSSQL in 1.7.4, you'll have to get version 1.7.3 from sourceforge. Then do the php.ini/dll stuff if it's not working :)