...

View Full Version : comma after each item in a select



votter
06-30-2010, 05:31 PM
I was wondering how to put a comma after each item in a select, but keep it from putting one at the end.

With the select something like:



$getName = mysql_query("SELECT `Name` FROM `Whatever`")or die(mysql_error());
while ($Name = mysql_fetch_array($getName)) {
echo $Name['Person']. ',';
}

So it would look like: Bob, Bill, Sue Instead of Bob, Bill, Sue,

Thanks again.

Keleth
06-30-2010, 05:52 PM
Use either substr or put the names into an array and implode them


$list = '';
while ($Name = mysql_fetch_array($getName)) {
$list .= $Name['Person']. ',';
}
echo substr($list, 0, -1);


$list = array();
while ($Name = mysql_fetch_array($getName)) {
$list[] = $Name['Person'];
}
echo implode(',', $list);

votter
06-30-2010, 06:00 PM
Thanks! :). Is one better than the other to use, or no?

Keleth
06-30-2010, 06:24 PM
I'm not sure which is more resource intensive, but I guess it depends if you plan on doing anything else with the data. But in the short/small term, I don't think it matters.

votter
06-30-2010, 07:36 PM
Okay, thanks again. :)

udjamaflip
06-30-2010, 10:35 PM
This is more resource friendly iirc



$getName = mysql_query("SELECT `Name` FROM `Whatever`")or die(mysql_error());
$myVar = '';
while ($Name = mysql_fetch_array($getName)) {
$myVar = (!$myVar) ? $Name['Person'] : ','.$name['Person'];
}
echo $myVar


Hope it helps! :thumbsup:

Beagle
07-01-2010, 03:08 AM
Stop using substr for this! There's too many potential issues with substr.

Use rtrim (http://us2.php.net/manual/en/function.rtrim.php)


rtrim($csvList, " ,");

Beagle
07-01-2010, 03:12 AM
This is more resource friendly iirc



$getName = mysql_query("SELECT `Name` FROM `Whatever`")or die(mysql_error());
$myVar = '';
while ($Name = mysql_fetch_array($getName)) {
$myVar = (!$myVar) ? $Name['Person'] : ','.$name['Person'];
}
echo $myVar


Hope it helps! :thumbsup:

Don't see why you think running a conditional N times is more efficient than running substr 1 time.

In fact, if you're worried about efficiency, then the array accumulation with implode is the most efficient algorithmically (not necessarily the fastest for low values of N, but absolutely the fastest for high values of N). Looped string concatenation is O(n^2), implode is O(n).

Getting into the reasoning behind this will only be done on request because it's kind of long-winded.

Keleth
07-01-2010, 04:58 AM
Stop using substr for this! There's too many potential issues with substr.

Use rtrim (http://us2.php.net/manual/en/function.rtrim.php)


rtrim($csvList, " ,");

Out of curiosity, what are the potential issues?

Beagle
07-01-2010, 05:11 AM
substr requires you to do math. Do the math wrong and you're going to get an error. Call substr with numerical indexes on an empty string, you'll get an error. Supporting UTF-8? gotta use mb_substr (multibyte substr) or you'll get seemingly bizarre results. It's just a hassle.

rtrim is simple AND you can trim any number of characters from the end of a string. Most people just don't know it can do that.

Keleth
07-01-2010, 07:05 AM
substr requires you to do math. Do the math wrong and you're going to get an error. Call substr with numerical indexes on an empty string, you'll get an error. Supporting UTF-8? gotta use mb_substr (multibyte substr) or you'll get seemingly bizarre results. It's just a hassle.

rtrim is simple AND you can trim any number of characters from the end of a string. Most people just don't know it can do that.

Ah, makes sense.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum