Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-23-2008, 08:34 PM   PM User | #1
birdbrain24
New Coder

 
Join Date: Jul 2007
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
birdbrain24 has a little shameless behaviour in the past
Users Online

How can i sperate the users in my code without there being a - left at the end ?

PHP Code:
<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>
birdbrain24 is offline   Reply With Quote
Old 01-23-2008, 10:13 PM   PM User | #2
skyrider01
New to the CF scene

 
Join Date: Jan 2008
Posts: 9
Thanks: 0
Thanked 1 Time in 1 Post
skyrider01 is an unknown quantity at this point
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.
__________________
m6.net - windows webhosting
my readlog - what I read
skyrider01 is offline   Reply With Quote
Old 01-23-2008, 11:01 PM   PM User | #3
birdbrain24
New Coder

 
Join Date: Jul 2007
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
birdbrain24 has a little shameless behaviour in the past
I tried this and it still doesn't work!

PHP Code:
<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 ?
birdbrain24 is offline   Reply With Quote
Old 01-23-2008, 11:29 PM   PM User | #4
JohnDubya
Super Moderator


 
JohnDubya's Avatar
 
Join Date: Nov 2006
Location: Missouri
Posts: 634
Thanks: 12
Thanked 18 Times in 18 Posts
JohnDubya is on a distinguished road
Quote:
Originally Posted by birdbrain24 View Post
PHP Code:
<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.
__________________
JDub
http://johnnyzone.com/blog
JohnDubya is offline   Reply With Quote
Old 01-23-2008, 11:38 PM   PM User | #5
birdbrain24
New Coder

 
Join Date: Jul 2007
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
birdbrain24 has a little shameless behaviour in the past
i fixed what you said and it still shows the - after every username!

Look at the code now:

PHP Code:
<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>
birdbrain24 is offline   Reply With Quote
Old 01-24-2008, 04:43 AM   PM User | #6
skyrider01
New to the CF scene

 
Join Date: Jan 2008
Posts: 9
Thanks: 0
Thanked 1 Time in 1 Post
skyrider01 is an unknown quantity at this point
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

PHP Code:
while($users mysql_fetch_array($users_query)){ 

     echo 
$i "<br>";
    if(
$count $i){ 
   
    echo(
$users['username'].' - '); 
     
    
$i++; 
     
    } 
     
    elseif(
$count == $i){ 
   
    echo(
$users['username']);   
       
    } 
   
  } 
__________________
m6.net - windows webhosting
my readlog - what I read
skyrider01 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:04 PM.


Advertisement
Log in to turn off these ads.