2013-12-27 17:02

如何在线显示用户数量? [关闭]


Im using radipanel to create a staff panel for my website, but my mind has gone blank i have coded this to display the users currently online, but i completely forgot how to display the numbers of users online.

Heres the code i have to display the names of users online;


                $query = $db->query( "SELECT DISTINCT user_id FROM sessions WHERE user_id != '0'" ); 
                    $i = 1; 

                while( $array = $db->assoc( $query ) ) { 

                    $queryU = $db->query( "SELECT * FROM users WHERE id = '{$array['user_id']}'" ); 
                    $arrayU = $db->assoc( $queryU ); 

                    $queryUG = $db->query( "SELECT * FROM usergroups WHERE id = '{$arrayU['displaygroup']}'" ); 
                    $arrayUG = $db->assoc( $queryUG ); 

                    echo "<a href=\"core.profile?u={$arrayU['username']}\" style=\"color: #{$arrayUG['colour']}; font-weight: bold;\">"; 
                    echo $arrayU['username'];
                                            $coverstatus2 = $arrayU['cover'];
                                            if($coverstatus2 == 1) {
                                            echo " <b style=\"color:#89c35c;\">+</b>";
                                            }elseif($coverstatus2 == 2) {
                                            echo " <b style=\"color:#c24641;\">-</b>";
                                            }elseif($coverstatus2 == 0) {
                                            echo "";
                    echo "</a>"; 
                    echo ( $i == $db->num( $query ) ) ? '' : '&nbsp;&nbsp;&nbsp;'; 




But how do i display the number?

For example what i want is

Users Online(Number here) Josh, Tom, Mark

So it would say Users Online (3)

So whats the code to display the number?

Thanks, Josh

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • doucongqian6644 doucongqian6644 8年前

    As far as I can tell from your code you are getting an array of all current sessions, then using the while statement iterations to echo off each user. After your i=1; and before your while statement put

    $array = $db->assoc( $query );
    $count = count($array);

    then you can echo $count where you want it on the page then

    while ($array) {
        // Your other code
    点赞 评论 复制链接分享
  • donglu5235 donglu5235 8年前

    You can add a row called "isonline" to your user's table.

    $onlineppl=mysql_num_rows(mysql_query("SELECT * FROM users WHERE isonline='1'");

    mysql_num_rows can help you to count your rows with select.

    You when users log off only need to update isonline='0' on db.

    I hope to help a little.

    点赞 评论 复制链接分享
  • dou44481 dou44481 8年前

    I think it's work for you. Follow the steps.

    1. 1st add a column in user table which store the user last activity in unix_timestamp which is you get by using time() method in php. So whenever a user do some activity it updates to table for corrosponding user. Now you have a column which show user last activity time.

    2.Now update last_activity columns with time() method everytime whenever a user do some activity on your server like refreshing page etc.

    Now run a query on your time based like for 60 secoonds.

    $query="select * from user_table where last_activity > ( time() - 60 ) ;"

    This query return you number of online user as well as their data.

    Hope Its helpfull to you.

    点赞 评论 复制链接分享
  • dtcuv8044 dtcuv8044 8年前

    Put an int++ counter on the while() statement so with each iteration it calculates the users. Or to do it without printing the names use a count() method or something similar.

    点赞 评论 复制链接分享