Aureole Posted September 5, 2007 Share Posted September 5, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/68012-help-with-displaying-the-results-of-a-query/ Share on other sites More sharing options...
SJames Posted September 5, 2007 Share Posted September 5, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/68012-help-with-displaying-the-results-of-a-query/#findComment-341896 Share on other sites More sharing options...
Yesideez Posted September 5, 2007 Share Posted September 5, 2007 <?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. Quote Link to comment https://forums.phpfreaks.com/topic/68012-help-with-displaying-the-results-of-a-query/#findComment-341905 Share on other sites More sharing options...
Aureole Posted September 5, 2007 Author Share Posted September 5, 2007 Ok I'll try both of your suggestions, thanks a lot. Quote Link to comment https://forums.phpfreaks.com/topic/68012-help-with-displaying-the-results-of-a-query/#findComment-341952 Share on other sites More sharing options...
Jessica Posted September 5, 2007 Share Posted September 5, 2007 Here are some functions: http://php.net/implode or: http://php.net/strlen http://php.net/substr Quote Link to comment https://forums.phpfreaks.com/topic/68012-help-with-displaying-the-results-of-a-query/#findComment-341955 Share on other sites More sharing options...
Aureole Posted September 5, 2007 Author Share Posted September 5, 2007 Thanks for the functions but even after looking at them I have no idea how to implement them... Quote Link to comment https://forums.phpfreaks.com/topic/68012-help-with-displaying-the-results-of-a-query/#findComment-341960 Share on other sites More sharing options...
Jessica Posted September 5, 2007 Share Posted September 5, 2007 Well, you have a string which has one too many characters on the end. (use the second two) Or you have an array you want separated by another string. (use the first). Just try it. Quote Link to comment https://forums.phpfreaks.com/topic/68012-help-with-displaying-the-results-of-a-query/#findComment-341967 Share on other sites More sharing options...
Aureole Posted September 5, 2007 Author Share Posted September 5, 2007 I'll try it but I'm new to all this...I'll post my findings here. Quote Link to comment https://forums.phpfreaks.com/topic/68012-help-with-displaying-the-results-of-a-query/#findComment-341970 Share on other sites More sharing options...
Aureole Posted September 5, 2007 Author Share Posted September 5, 2007 <?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. Quote Link to comment https://forums.phpfreaks.com/topic/68012-help-with-displaying-the-results-of-a-query/#findComment-341978 Share on other sites More sharing options...
Jessica Posted September 5, 2007 Share Posted September 5, 2007 <?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>,  '; } $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! Quote Link to comment https://forums.phpfreaks.com/topic/68012-help-with-displaying-the-results-of-a-query/#findComment-342185 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.