...

View Full Version : Users Online



birdbrain24
01-23-2008, 08:34 PM
How can i sperate the users in my code without there being a - left at the end ?


<html>
<head>
<title>RACE</title>
</head>
<body>
<?php
# Common functions!
session_start();
include('functions.php');
pageinfo('January 23 2008', 'January 23 2008');
connect();
sessioncheck();
lastaction();
$username=$_SESSION['username'];

$timenow = time();
$select = mysql_query("SELECT * FROM users WHERE lastaction > '$timenow' ORDER by username ASC");

while($grab = mysql_fetch_array($select)){

if($users['userlevel'] == 'Admin'){

echo('<a href=\'profile.php?user='.$users['username'].'\'><b><font color=red>'.$users['username'].'</font></b></a> -"');

}

elseif($users['userlevel'] == 'Head'){

echo('<a href=\'profile.php?user='.$users['username'].'\'><b><font color=orange>'.$users['username'].'</font></b></a> -"');

}

elseif($users['userlevel'] == 'Mod'){

echo('<a href=\'profile.php?user='.$users['username'].'\'><b><font color=yellow>'.$users['username'].'</font></b></a> -"');

}else{

echo('<a href=\'profile.php?user='.$users['username'].'\'><b><font color=lightblue>'.$users['username'].'</font></b></a> -"');

}

}
?>
</body>
</html>

skyrider01
01-23-2008, 10:13 PM
You could check with an if statement if its the last entry, and then dont add the '-'

one way to do that would be to get the number of records from mysql, using mysql_num_rows, and then use a variable that keeps track of where in the loop you are in.

birdbrain24
01-23-2008, 11:01 PM
I tried this and it still doesn't work!


<html>
<head>
<title>RACE</title>
</head>
<body>
<?php
# Common functions!
session_start();
include('functions.php');
pageinfo('January 23 2008', 'January 23 2008');
connect();
sessioncheck();
lastaction();
$username=$_SESSION['username'];

$timenow = time();
$users_query = mysql_query("SELECT * FROM users WHERE lastaction > '$timenow' ORDER by username ASC");
$count = mysql_num_rows($users_query);
$i = 0;

while($users = mysql_fetch_array($users_query)){
$i = 0;
if($count > $i){

echo($users['username'].' - ');

$i++;

}

elseif($count == $i){

echo($users['username']);

}

}
?>
</body>
</html>

Anyone think they could help me ?

JohnDubya
01-23-2008, 11:29 PM
<html>
<head>
<title>RACE</title>
</head>
<body>
<?php
# Common functions!
session_start();
include('functions.php');
pageinfo('January 23 2008', 'January 23 2008');
connect();
sessioncheck();
lastaction();
$username=$_SESSION['username'];

$timenow = time();
$users_query = mysql_query("SELECT * FROM users WHERE lastaction > '$timenow' ORDER by username ASC");
$count = mysql_num_rows($users_query);
$i = 0;

while($users = mysql_fetch_array($users_query)){
$i = 0;
if($count > $i){

echo($users['username'].' - ');

$i++;

}

elseif($count == $i){

echo($users['username']);

}

}
?>
</body>
</html>

Well, you set $i to equal 0 before the while AND inside the while. So each time you're looping back through the while statement, it's setting $i to 0 again. Set $i to 0 outside only and let us know if that helps.

birdbrain24
01-23-2008, 11:38 PM
i fixed what you said and it still shows the - after every username!

Look at the code now:


<html>
<head>
<title>RACE</title>
</head>
<body>
<?php
# Common functions!
session_start();
include('functions.php');
pageinfo('January 23 2008', 'January 23 2008');
connect();
sessioncheck();
lastaction();
$username=$_SESSION['username'];

$timenow = time();
$users_query = mysql_query("SELECT * FROM users WHERE lastaction > '$timenow' ORDER by username ASC");
$count = mysql_num_rows($users_query);
$i = 0;

while($users = mysql_fetch_array($users_query)){

if($count > $i){

echo($users['username'].' - ');

$i++;

}

elseif($count == $i){

echo($users['username']);

}

}
?>
</body>
</html>

skyrider01
01-24-2008, 04:43 AM
to debug what is going on, you can echo $i . "<br>"; or something so you see what the $i value is in different iterations inside the while loop, that way it will be easy to fix for you. the variable is probably only 1 off for it to work, the approach is OK


while($users = mysql_fetch_array($users_query)){

echo $i . "<br>";
if($count > $i){

echo($users['username'].' - ');

$i++;

}

elseif($count == $i){

echo($users['username']);

}

}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum