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

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

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.