• Unfortunately, we have experienced significant hard drive damage that requires urgent maintenance and rebuilding. The forum will be a state of read only until we install our new drives and rebuild all the configurations needed. Please follow our Facebook page for updates, we will be back up shortly! (The forum could go offline at any given time due to the nature of the failed drives whilst awaiting the upgrades.) When you see an Incapsula error, you know we are in the process of migration.

Web Displaying # Online

Newbie Spellweaver
Joined
Feb 13, 2017
Messages
14
Reaction score
0
Hello Ragezone, a bit of an issue here, if anyone knows the fix or if I'm doing it wrong please let me know! Trying to figure out how to display 'Amount' Players Online. Indicating Green!

PHP:
<?php
$plyonline = $mysqli->query("SELECT * FROM accounts where loggedin = 2")->fetch_row();
$url     = $_SERVER['REQUEST_URI'];
?><div class="container">
 <div class="sixteen columns">
  <div id="logo"></div>
  <nav>
   <ul>
    <li <?php if ($url == '/?p=home') { ?>class="nhome" <?php } ?>><a href="?p=home">Home</a></li>
    <li <?php if ($url == '/?p=register') { ?>class="nhome" <?php } ?>><a href="?p=register">Register</a></li>
    <li <?php if ($url == '/?p=download') { ?>class="nhome" <?php } ?>><a href="?p=download">Download</a></li>
    <li <?php if ($url == '/?p=vote') { ?>class="nhome" <?php } ?>><a href="?p=vote">Vote</a></li>
    <li <?php if ($url == '/?p=donate') { ?>class="nhome" <?php } ?>><a href="?p=donate">Donate</a></li>
    <li <?php if ($url == '/?p=rankings') { ?>class="nhome" <?php } ?>><a href="?p=rankings">Rankings</a></li>
    <li><a href="<?php echo $forum; ?>" target="_blank">Forums</a></li>
   </ul>
  </nav>
  <div id="scroller_box">
   <div id="scroller_inside">
    <marquee scrollamount="10"> Welcome to Knighty</marquee>
   </div>
  </div>
 </div>
<!-- Three Columns =================================== -->
<!-- first --> <div class="one-third column">
  <div class="top_contents">
   <div class="c_spacing">
    <div class="left">
     <?php
      if($plyonline > 0) {
       echo "<img src=\"./images/tools/online.png\">";
      } else {
       echo "<img src=\"./images/tools/offline.png\">";
      }
     ?>
    </div>
    <div class="top_right">
     <?php 
      if($plyonline > 1) {
      echo "<h5><span class=\"green\"{$plyonline}</span> Players Online</h5>";
      } else {
       echo "<h5><span class=\"red\"> Server Offline</h5>";
      }
     ?>
     <img src="./images/rate/image.php" style="position: absolute;top: 30px;left: -32px;"/>
    </div>
   </div>
  </div>
 </div>
 
Junior Spellweaver
Joined
Nov 26, 2012
Messages
156
Reaction score
35
I'm not sure in PHP, but in the meantime if there's no solution; if you make the plyonline >= 1, so that even if 1 player is online (urself) you can try it out and see what happens.
Through googling one option I've seen is:

Code:
echo "<span style = 'font-color: #ff0000'>  {$plyonline} Players online</span>";
You can try that i guess
 
Upvote 0
Newbie Spellweaver
Joined
Feb 13, 2017
Messages
14
Reaction score
0
I'm not sure in PHP, but in the meantime if there's no solution; if you make the plyonline >= 1, so that even if 1 player is online (urself) you can try it out and see what happens.
Through googling one option I've seen is:

Code:
echo "<span style = 'font-color: #ff0000'>  {$plyonline} Players online</span>";
You can try that i guess
Nope, this did not work, I've tried this before and had no success.
 
Upvote 0
Newbie Spellweaver
Joined
Aug 27, 2015
Messages
25
Reaction score
3
Code:
<?php
$query = $mysqli->query("SELECT * FROM accounts WHERE loggedin = 2");
$plyonline = $query->num_rows;
$url     = $_SERVER['REQUEST_URI'];
?><div class="container">
 <div class="sixteen columns">
  <div id="logo"></div>
  <nav>
   <ul>
    <li <?php if ($url == '/?p=home') { ?>class="nhome" <?php } ?>><a href="?p=home">Home</a></li>
    <li <?php if ($url == '/?p=register') { ?>class="nhome" <?php } ?>><a href="?p=register">Register</a></li>
    <li <?php if ($url == '/?p=download') { ?>class="nhome" <?php } ?>><a href="?p=download">Download</a></li>
    <li <?php if ($url == '/?p=vote') { ?>class="nhome" <?php } ?>><a href="?p=vote">Vote</a></li>
    <li <?php if ($url == '/?p=donate') { ?>class="nhome" <?php } ?>><a href="?p=donate">Donate</a></li>
    <li <?php if ($url == '/?p=rankings') { ?>class="nhome" <?php } ?>><a href="?p=rankings">Rankings</a></li>
    <li><a href="<?php echo $forum; ?>" target="_blank">Forums</a></li>
   </ul>
  </nav>
  <div id="scroller_box">
   <div id="scroller_inside">
    <marquee scrollamount="10"> Welcome to Knighty</marquee>
   </div>
  </div>
 </div>
<!-- Three Columns =================================== -->
<!-- first --> <div class="one-third column">
  <div class="top_contents">
   <div class="c_spacing">
    <div class="left">
     <?php
      if($plyonline > 0) {
       echo "<img src=\"./images/tools/online.png\">";
      } else {
       echo "<img src=\"./images/tools/offline.png\">";
      }
     ?>
    </div>
    <div class="top_right">
     <?php 
      if($plyonline > 1) {
      echo "<h5><span class=\"green\">".$plyonline."</span> Players Online</h5>";
      } else {
       echo "<h5><span class=\"red\"> Server Offline</h5>";
      }
     ?>
     <img src="./images/rate/image.php" style="position: absolute;top: 30px;left: -32px;"/>
    </div>
   </div>
  </div>
 </div>

Also, you might want to change how you detect whether the server is online/offline, since it could throw a false positive if you for example shut down the server terminal and it didn't change all players' loggedin status to 0, then the website would show X amount of players is online, when the server really is offline.
Take a look at fsockopen to check open ports.
 
Upvote 0
Everything is possible~
Loyal Member
Joined
Jan 9, 2008
Messages
818
Reaction score
847
ehm, I do not recommed fetching _all_ accounts, just to get the amount of rows.

PHP:
<?php
$query = $mysqli->query("SELECT COUNT(*) FROM accounts WHERE loggedin = 2");
$row = $query->fetch_array(); // Get first row from resultset
$query->free(); // Free up memory

$plyonline = $row[0]; // Get the COUNT(*) field
$url     = $_SERVER['REQUEST_URI'];
?><div class="container">
 <div class="sixteen columns">
  <div id="logo"></div>
  <nav>
   <ul>
    <li <?php if ($url == '/?p=home') { ?>class="nhome" <?php } ?>><a href="?p=home">Home</a></li>
    <li <?php if ($url == '/?p=register') { ?>class="nhome" <?php } ?>><a href="?p=register">Register</a></li>
    <li <?php if ($url == '/?p=download') { ?>class="nhome" <?php } ?>><a href="?p=download">Download</a></li>
    <li <?php if ($url == '/?p=vote') { ?>class="nhome" <?php } ?>><a href="?p=vote">Vote</a></li>
    <li <?php if ($url == '/?p=donate') { ?>class="nhome" <?php } ?>><a href="?p=donate">Donate</a></li>
    <li <?php if ($url == '/?p=rankings') { ?>class="nhome" <?php } ?>><a href="?p=rankings">Rankings</a></li>
    <li><a href="<?php echo $forum; ?>" target="_blank">Forums</a></li>
   </ul>
  </nav>
  <div id="scroller_box">
   <div id="scroller_inside">
    <marquee scrollamount="10"> Welcome to Knighty</marquee>
   </div>
  </div>
 </div>
<!-- Three Columns =================================== -->
<!-- first --> <div class="one-third column">
  <div class="top_contents">
   <div class="c_spacing">
    <div class="left">
      <?php if ($plyonline > 0): ?>
        <img src="./images/tools/online.png">
      <?php else: ?>
        <img src="./images/tools/offline.png">
      <?php endif; ?>
    </div>
    <div class="top_right">
      <h5>
        <?php if ($plyonline > 0): ?>
          <span class="green"><?php echo $plyonline; ?></span> Players Online
        <?php else: ?>
          <span class="red"> Server Offline</span>
        <?php endif; ?>
      </h5>
     <img src="./images/rate/image.php" style="position: absolute;top: 30px;left: -32px;"/>
    </div>
   </div>
  </div>
 </div>
 
Upvote 0
Back
Top