Jump to content

Help with displaying the results of a query


Aureole

Recommended Posts

Ok I have a query that shows online users, now say if there's 3 people online it would show like this:

 

Fred, Bob, Jim,

 

I need a way to eliminate the commar of the last returned result so it would look like:

 

Fred, Bob, Jim

 

I really can't think how to do it.

 

Here's my code.

 

<?php
$query = "SELECT mem_dname, mem_id FROM members WHERE mem_online=1 ORDER BY mem_dname DESC LIMIT 10";
$result = mysql_query($query) or die(mysql_error());
if (!mysql_num_rows($result)>0)
{
    echo("Nobody is Online. =(");
}
if (mysql_num_rows($result)<2)
{
    $commar = 0;
}
if (mysql_num_rows($result)>1)
{
    $commar = 1;
}
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
    if ($commar = 0)
{
            echo("<a href=\"profile.php?id=".$row['mem_id']."\" class=\"a\">".$row['mem_dname']."</a>");
}
else
{
            echo("<a href=\"profile.php?id=".$row['mem_id']."\" class=\"a\">".$row['mem_dname']."</a>, ");
}
}
?>

 

Thanks a lot.

Link to comment
Share on other sites

Try putting the user's name and such in an array. Then you could use something like:

 

 

while ($current <= count($array)) {

if ($current < count($array)) {

$useComma = "yes";

{

if ($current == count($array)) P

$useComma = "no";

}

$current++;

}

 

 

I'm sorry if this doesn't help much, but I am pretty much showing you how to determine when to use the comma.

Link to comment
Share on other sites

<?php
  $query="SELECT * FROM users_online";
  if (mysql_num_rows(mysql_query($query))>0) {
    $row=mysql_fetch_array(mysql_query($query));
    echo $row['name'].',';
    $mquery=mysql_query($query);
    while ($row=mysql_fetch_assoc($mquery)) {
      echo $row['name'].',';
    }
  } else {
    echo 'No users online';
  }
?>

Something like that. Don't know if it'll work (been awake for almost 24 hours) but thats the basic idea.

Link to comment
Share on other sites

<?php
$query = "SELECT mem_dname, mem_id FROM members WHERE mem_online=1 ORDER BY mem_dname DESC LIMIT 10";
$result = mysql_query($query) or die(mysql_error());
if (!mysql_num_rows($result)>0)
{
    echo("Nobody is Online. =(");
}
if (mysql_num_rows($result)<2)
{
    $commar = 0;
}
if (mysql_num_rows($result)>1)
{
    $commar = 1;
}
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
    if ($commar = 0)
{
            echo("<a href=\"profile.php?id=".$row['mem_id']."\" class=\"a\">".$row['mem_dname']."</a>");
}
else
{
           $str = "<a href=\"profile.php?id=".$row['mem_id']."\" class=\"a\">".$row['mem_dname']."</a>, ";

           echo substr('$str', 0, -7);
}
}
?>

 

?? I don't know, wouldn't that kind of remove the   and the commar from ALL of them? ...I don't get this. Could someone not show me how to do it then maybe I'll learn something 'cause right now this isn't really achieving anything.

 

Btw my comment above, don't take it the wrong way I'm just saying...I have no idea what I'm doing, if someone actually shows me what to do then I'll learn something otherwise posting little snippets and functions that I have no idea how to use isn't going to help in all honesty.

Link to comment
Share on other sites

<?php
$query = "SELECT mem_dname, mem_id FROM members WHERE mem_online=1 ORDER BY mem_dname DESC LIMIT 10";
$result = mysql_query($query) or die(mysql_error());
if (!mysql_num_rows($result)>0){
    echo("Nobody is Online. =(");
}

$users = "";
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
   $users .= '<a href="profile.php?id='.$row['mem_id'].'" class="a">'.$row['mem_dname'].'</a>, &nbsp';
}

$users = substr($users, 0, (strlen($users)-2));
print $users;
?>

 

Or:

 

<?php
$query = "SELECT mem_dname, mem_id FROM members WHERE mem_online=1 ORDER BY mem_dname DESC LIMIT 10";
$result = mysql_query($query) or die(mysql_error());
if (!mysql_num_rows($result)>0){
    echo("Nobody is Online. =(");
}

$users = array();
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
   $users[] = '<a href="profile.php?id='.$row['mem_id'].'" class="a">'.$row['mem_dname'].'</a>';
}

$users = implode($users, ', ');
print $users;
?>

 

PS: You don't need to put a class on a link to style it if all of your links are styled the same. Just use

a{
//css here
}

instead of:

.a{
//css here
}

 

PPS: My power went out right before I hit Post. Sorry for the late reply. Crazy storms!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.