[PHP] Who's Online

Joined
Dec 4, 2007
Messages
1,935
Reaction score
96
PHP:
        echo "<tr><td class='tcate' style='width:100%;text-align:left'>";
        echo "Currently Active Users: ";

    $query =  mysql_query("SELECT * FROM users WHERE online ='1' ORDER BY username");     
    while($row = mysql_fetch_array($query))



    $username = $row['username'];
    echo "$username,";



        echo "</td></tr>";

This currently shows "Currently Active Users: Mike,".

How can I make it so if $row['rank'] = 3, the name is bold and red, if $row['rank'] = 2, the name is bold and green, and if $row['rank'] = 1, then the name is normal? Also, I'd like the last name on the list to have a period after it, not a comma...
 
Maybe use an if statement?

PHP:
        echo "<tr><td class='tcate' style='width:100%;text-align:left'>";
        echo "Currently Active Users: ";

    $query =  mysql_query("SELECT * FROM users WHERE online ='1' ORDER BY username");     
    while($row = mysql_fetch_array($query))



    $username = $row['username'];
    if($row['rank']==3) echo "<span style=\"font-weight:bold;color:red;\">$username</span>,";
    if($row['rank']==2) echo "<span style=\"font-weight:bold;color:green;\">$username</span>,";
    if($row['rank']==1) echo "$username,";

        echo "</td></tr>";
 
Here's what I came up with pretty quickly. There may be a better way to do this, but since I don't use the basic mysql library very often (I wrote up a database class that handles all Mysqli functions very elegantly), this may not be the most efficient way.

PHP:
<?php
    echo "<tr><td class='tcate' style='width:100%;text-align:left'>";
    echo "Currently Active Users: ";

    $query =  mysql_query("SELECT * FROM users WHERE online ='1' ORDER BY username");     
    
    // Let's store all the rows in an array for later use.
    $rows = array();
    while ($row = mysql_fetch_array($query)) {
        $rows[] = $row;
    }
    
    // Now we iterate over the whole rows array. We'll use a for loop so we have a usable specific index.
    for ($i = 0; $i < sizeof($rows); $i++) {
        // Let's store the following variables for ease of use and readability.
        $row = $rows[$i];
        $rank = $row['rank'];
        $username = $row['username'];
        
        // Now we'll apply the different rank markups.
        if ($rank == 1) {
            echo $username;
        } elseif ($rank == 2) {
            echo '<span style="font-weight: bold; color: green;">' . $username . '</span>';
        } elseif ($rank == 3) {
            echo '<span style="font-weight: bold; color: red;">' . $username . '</span>';
        }
        
        // Finally, decide if we use a comma or period after the end of the username.
        if ($i == (sizeof($rows) - 1)) {
            echo '.';
        } else {
            echo ',';
        }
    }
    
    echo "</td></tr>";  
?>

The majority of my changes are commented. G'luck.
 
Here's what I came up with pretty quickly. There may be a better way to do this, but since I don't use the basic mysql library very often (I wrote up a database class that handles all Mysqli functions very elegantly), this may not be the most efficient way.

PHP:
<?php
    echo "<tr><td class='tcate' style='width:100%;text-align:left'>";
    echo "Currently Active Users: ";

    $query =  mysql_query("SELECT * FROM users WHERE online ='1' ORDER BY username");     
    
    // Let's store all the rows in an array for later use.
    $rows = array();
    while ($row = mysql_fetch_array($query)) {
        $rows[] = $row;
    }
    
    // Now we iterate over the whole rows array. We'll use a for loop so we have a usable specific index.
    for ($i = 0; $i < sizeof($rows); $i++) {
        // Let's store the following variables for ease of use and readability.
        $row = $rows[$i];
        $rank = $row['rank'];
        $username = $row['username'];
        
        // Now we'll apply the different rank markups.
        if ($rank == 1) {
            echo $username;
        } elseif ($rank == 2) {
            echo '<span style="font-weight: bold; color: green;">' . $username . '</span>';
        } elseif ($rank == 3) {
            echo '<span style="font-weight: bold; color: red;">' . $username . '</span>';
        }
        
        // Finally, decide if we use a comma or period after the end of the username.
        if ($i == (sizeof($rows) - 1)) {
            echo '.';
        } else {
            echo ',';
        }
    }
    
    echo "</td></tr>";  
?>
The majority of my changes are commented. G'luck.

Changed it around to

PHP:
    echo "<tr><td class='tcate' style='width:100%;text-align:left'>";
    echo "Currently Active Users: ";

    $query =  mysql_query("SELECT * FROM users WHERE online ='1' ORDER BY username");     
    
    // Let's store all the rows in an array for later use.
    $rows = array();
    while ($row = mysql_fetch_array($query)) {
        $rows[] = $row;
    }
    
    // Now we iterate over the whole rows array. We'll use a for loop so we have a usable specific index.
    for ($i = 0; $i < sizeof($rows); $i++) {
        // Let's store the following variables for ease of use and readability.
        $row = $rows[$i];
        $rank = $row['rank'];
        $username = $row['username'];
        $userid = $row['id'];
        
        // Now we'll apply the different rank markups.
        if ($rank == 1) {
            echo '<span><a href="profile.php?id=' . $userid . '">' . $username . '</a></span>';
        } elseif ($rank == 2) {
            echo '<span style="font-weight: bold; color: green;"><a href="profile.php?id=' . $userid . '">' . $username . '</a></span>';
        } elseif ($rank == 3) {
            echo '<span style="font-weight: bold; color: red;"><a href="profile.php?id=' . $userid . '">' . $username . '</a></span>';
        }
        


        // Finally, decide if we use a comma or period after the end of the username.
        if ($i == (sizeof($rows) - 1)) {
            echo '.';
        } else {
            echo ',';
        }
    }
    
    echo "</td></tr>";

That way profiles are linked to.

Thanks so much man!
 
storing all the results in a array and using a for loop to iterate is lame.

PHP:
while($rows = mysql_fetch_array($query))
{
echo $rows["rank"];
echo $rows["username"];
}
It is simpler and will produce the same result.
and dont echo html. [2]

Your code will not produce the same result as my code nor will it produce the requested result from Mike.

The only reason a for loop was used is because he needed to differentiate between the last element of the array and all the others, hence the comma and period. I don't use the basic mysql library very often, and thus handle similar issues with my own projects quite differently. If there are better ways of getting this information available to the php mysql library, then please - speak up.

I will concur that it is not good practice to combine HTML and PHP in the same file, but the aforementioned code was done to solve a simple problem. Implementing a template system was not part of his problem.

In terms of displaying small blocks or one-liners of HTML without the use of a template system, I personally prefer using echo versus using pure HTML in the PHP file and only calling variables via <?php echo $var; ?>. If there are performance issues with doing this, then I was not aware. I am more than willing to be enlightened.
 
Last edited:
Here's what I came up with pretty quickly. There may be a better way to do this, but since I don't use the basic mysql library very often (I wrote up a database class that handles all Mysqli functions very elegantly), this may not be the most efficient way.

PHP:
<?php
    echo "<tr><td class='tcate' style='width:100%;text-align:left'>";
    echo "Currently Active Users: ";

    $query =  mysql_query("SELECT * FROM users WHERE online ='1' ORDER BY username");     
    
    // Let's store all the rows in an array for later use.
    $rows = array();
    while ($row = mysql_fetch_array($query)) {
        $rows[] = $row;
    }
    
    // Now we iterate over the whole rows array. We'll use a for loop so we have a usable specific index.
    for ($i = 0; $i < sizeof($rows); $i++) {
        // Let's store the following variables for ease of use and readability.
        $row = $rows[$i];
        $rank = $row['rank'];
        $username = $row['username'];
        
        // Now we'll apply the different rank markups.
        if ($rank == 1) {
            echo $username;
        } elseif ($rank == 2) {
            echo '<span style="font-weight: bold; color: green;">' . $username . '</span>';
        } elseif ($rank == 3) {
            echo '<span style="font-weight: bold; color: red;">' . $username . '</span>';
        }
        
        // Finally, decide if we use a comma or period after the end of the username.
        if ($i == (sizeof($rows) - 1)) {
            echo '.';
        } else {
            echo ',';
        }
    }
    
    echo "</td></tr>";  
?>

The majority of my changes are commented. G'luck.
He never asked for those commas, and they don't really make sense anyway, considering it will be a table, not "one-name-after-another" like here, on rz..

and were it list like here on RZ the far best would be this..

PHP:
<?php 
$query =  mysql_query("SELECT * FROM users WHERE online ='1' ORDER BY username");   
$result = mysql_fetch_array($query);

$ranks = array(
    2=>"moderator",
    3=>"administrator"
    666=>"banned"
);

?>
<ul id="users">
<?php 
    foreach ($result as $row) {
        echo '<li class="'.$ranks[$row['rank']].'">'.$row['username'].'</li>';

    }
?>
</ul>

PHP:
#users li {
    display: inline;
}

#users li:after {
    content: ',';
}

#users li:last-child:after {
    content: '';
}

#users .moderator {
    font-weight: bold;
    color: green;
}

#users .administrator {
    font-weight: bold;
    color: red;
}

#users .banned {
    font-style: italic;
    color: grey;
}

In IE6 it won't have that "," but who cares, really.

Browser Support for :before/:after Pseudo Elements | CSS-Tricks

also you could really just do

PHP:
<ul id="users">
<?php 
    foreach ($result as $row) {
        echo '<li class="rank_'.$row['rank'].'">'.$row['username'].'</li>';

    }
?>
</ul>

and in css

PHP:
.rank_2 {
    color: red; 
}

if you care not about forgetting what the hell that "2" meant, after a year or more
 
Back