Page 1 of 1

useronline script help

Posted: Sun Nov 24, 2002 3:10 pm
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

Posted: Sun Nov 24, 2002 6:47 pm
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 "."; 
} 

?>

Posted: Mon Nov 25, 2002 5:08 am
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?

Posted: Mon Nov 25, 2002 5:14 am
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

Posted: Mon Nov 25, 2002 9:52 am
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

Posted: Mon Nov 25, 2002 10:02 am
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

Posted: Mon Nov 25, 2002 11:07 am
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 :)