Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Regular Coder
    Join Date
    Oct 2009
    Location
    United States
    Posts
    158
    Thanks
    8
    Thanked 4 Times in 4 Posts

    comma after each item in a select

    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:

    PHP Code:
    $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.
    Last edited by votter; 06-30-2010 at 06:35 PM.

  • #2
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,546
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Use either substr or put the names into an array and implode them

    PHP Code:
    $list '';
    while (
    $Name mysql_fetch_array($getName)) {
    $list .= $Name['Person']. ',';
    }
    echo 
    substr($list0, -1); 
    PHP Code:
    $list = array();
    while (
    $Name mysql_fetch_array($getName)) {
    $list[] = $Name['Person'];
    }
    echo 
    implode(','$list); 

  • Users who have thanked Keleth for this post:

    votter (06-30-2010)

  • #3
    Regular Coder
    Join Date
    Oct 2009
    Location
    United States
    Posts
    158
    Thanks
    8
    Thanked 4 Times in 4 Posts
    Thanks! . Is one better than the other to use, or no?

  • #4
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,546
    Thanks
    45
    Thanked 259 Times in 256 Posts
    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.

  • #5
    Regular Coder
    Join Date
    Oct 2009
    Location
    United States
    Posts
    158
    Thanks
    8
    Thanked 4 Times in 4 Posts
    Okay, thanks again.

  • #6
    Regular Coder
    Join Date
    Mar 2008
    Posts
    235
    Thanks
    6
    Thanked 21 Times in 20 Posts
    This is more resource friendly iirc

    PHP Code:
    $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!
    Andrew Sharman
    Web designer, developer and programmer.

    If you found my post helpful, why not give thanks! :)

  • #7
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    Stop using substr for this! There's too many potential issues with substr.

    Use rtrim

    Code:
    rtrim($csvList, " ,");

  • #8
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    Quote Originally Posted by udjamaflip View Post
    This is more resource friendly iirc

    PHP Code:
    $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!
    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.
    Last edited by Beagle; 07-01-2010 at 06:12 AM. Reason: the array accumulation and implode are the most efficient -> the array accumulation with implode is the most efficient

  • #9
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,546
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Quote Originally Posted by Beagle View Post
    Stop using substr for this! There's too many potential issues with substr.

    Use rtrim

    Code:
    rtrim($csvList, " ,");
    Out of curiosity, what are the potential issues?

  • #10
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    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.

  • #11
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,546
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Quote Originally Posted by Beagle View Post
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •