Jump to content

[Help Me] Two While Loops Don't Seem to Work...


shadrxninga

Recommended Posts

I've made a user online list for my server.

I got the list working well - as seen here http://novacraftonline.com/connect.php

 

But I wanted to add a player count as well - Here http://novacraftonline.com/count.php

 

But it only displays the Count and not the player list

 

Here is the code for the one without the player count:

<?PHP

// Conect to the Mysql Server
$connect = mysql_connect("localhost","********","*********");

//connect to the database
mysql_select_db("**********");

//query the database
$query = mysql_query("SELECT * FROM users_online WHERE online = 1");


echo "Online: ";
        WHILE($rows = mysql_fetch_array($query)):
            $users = $rows['name'];
            echo "<font color='black'><font color='green'>$users</font></font>, ";
        endwhile;
?>

 

Here is the one with the player count

<?PHP

// Conect to the Mysql Server
$connect = mysql_connect("localhost","*****","*****");

//connect to the database
mysql_select_db("******");

//query the database
$query = mysql_query("SELECT * FROM users_online WHERE online = 1");
//count 
$x = 0;

       WHILE($l = mysql_fetch_array($query)):
            $n = $l['name'];
            $x++;
       endwhile;
       echo "Online: ";
       WHILE($rows = mysql_fetch_array($query)):
            $users = $rows['name'];
            echo "<font color='black'><font color='green'>$users</font></font>, ";
        endwhile;
?>

 

I'm not sure why it only displays "Online($x)" Instead of "Online($x): $users"

Thanks for your help

Link to comment
Share on other sites

Just do one query to get the names and determine the player count in the PHP code.

 

<?php

// Conect to the Mysql Server
$connect = mysql_connect("localhost","********","*********");

//connect to the database
mysql_select_db("**********");

//query the database
$query = "SELECT `name` FROM `users_online` WHERE `online` = 1";
$result = mysql_query($query);

$onlineCount = 0;    //Numeric variable to track count of users
$onlineArray = array();  //Array variable to store online user names
while($row = mysql_fetch_assoc($result))
{
    $onlineCount++;
    $onlineArray[] = $rows['name'];
}
//Convert array of users to comma separated string
$onlineStr = implode(', ', $onlineArray);

//Ouput the results
echo "Player Count: <span style=\"color:green;\">{$onlineStr}</span><br>\n";
echo "Online: <br>\n";

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.