useronline script help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
CrazyJimmy
Forum Commoner
Posts: 34
Joined: Tue Nov 19, 2002 1:40 pm

useronline script help

Post by CrazyJimmy »

Hello,

I have created the a script to view the usernames of people currently on my site. I want the names separated by a comma except the last one. The code below is what im using to display names, it displays names ok but I'm not sure how to get it to put a full stop after last name.

Code: Select all

<?
//snip
if (mysql_num_rows($result)==1) {
  while ($row = mysql_fetch_assoc($result)) {
  echo "User Online: <b>".$rowї'username'];
  }
  } else {
  echo "Users Online<b> ";
   while ($row = mysql_fetch_assoc($result)) {
   echo "<b>".$rowї'username'].", </b>";
  }
}
}
<?php
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

Code: Select all

<?php
if (mysql_num_rows($result)==1) { 
  while ($row = mysql_fetch_assoc($result)) { 
  echo "User Online: <b>".$rowї'username']; 
  } 
  } else { 
  echo "Users Online<b> "; 
   while ($row = mysql_fetch_assoc($result)) { 
   echo "<b>".$rowї'username'].", </b>"; 
  } 
}
echo "."; 
} 

?>
CrazyJimmy
Forum Commoner
Posts: 34
Joined: Tue Nov 19, 2002 1:40 pm

Post by CrazyJimmy »

Im trying to replace the comma with a fullstop if it is the last name, I think I need to say something like this but dont know how to do the code.

Psuedo Code

if last name in table then
echo $row['username'].".";

Any ideas?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Maybe something like this:

Code: Select all

<?php 
$users_online = array();
echo 'User(s) Online: '; 
while ($row = mysql_fetch_assoc($result)) {  
     $users_onlineї] =  $rowї'username'];  
}
echo '<b>'.implode(', ', $users_online).'.</b>';
?>
Mac
CrazyJimmy
Forum Commoner
Posts: 34
Joined: Tue Nov 19, 2002 1:40 pm

Post by CrazyJimmy »

Thanks that worked, I looked up the implode function but dont really understand it, anychance of a quick explanation is simple terms?

J
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

What happens in that script is that each $row['username'] value is stored in an array. implode() is then used to add each value of the $users_online array to a string separating it from the previous value by a comma and a space.

Is that simple? I'm not entirely convinced so if it's a bit confusing say so and I'll try again :) .

Mac
CrazyJimmy
Forum Commoner
Posts: 34
Joined: Tue Nov 19, 2002 1:40 pm

Post by CrazyJimmy »

Thats cleared it up thanks, I was thinking the while loop ended after the implode bit, need to read more carefully in future :)
Post Reply