shadrxninga Posted July 27, 2011 Share Posted July 27, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/242922-help-me-two-while-loops-dont-seem-to-work/ Share on other sites More sharing options...
Psycho Posted July 27, 2011 Share Posted July 27, 2011 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"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/242922-help-me-two-while-loops-dont-seem-to-work/#findComment-1247742 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.