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 15 of 15
  1. #1
    Regular Coder
    Join Date
    Jan 2007
    Posts
    142
    Thanks
    9
    Thanked 1 Time in 1 Post

    How to place commas between results

    How do you do this?

    I have a query that results in a long list of names. I want to display them with a comma between each of them but no comma after the last one. I keep getting one after the last one. I also get a space before the comma.

    This is what needs edited:

    Code:
    while ($gm = mysql_fetch_array($get_gm))
    {
    	echo $gm['name'] . ', ';
    }

  • #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,068
    Thanks
    2
    Thanked 319 Times in 311 Posts
    The quickest and simplest logic is to build (concatenate) the content into a string, trim the last ', ' off of the end, and echo the results -

    PHP Code:
    $content "";
    while (
    $gm mysql_fetch_array($get_gm))
    {
        
    $content .= $gm['name'] . ', ';
    }
    $content rtrim($content,', ');
    echo 
    $content
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • Users who have thanked CFMaBiSmAd for this post:

    unrelenting (06-28-2008)

  • #3
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,861
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    http://us2.php.net/join may also help.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • Users who have thanked abduraooft for this post:

    unrelenting (06-28-2008)

  • #4
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    use mysql_num_rows to get the number of results, then create a counter in the loop to know where you are and when you reach the last one, don't write a comma.

    HTH
    Dan
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • Users who have thanked whizard for this post:

    unrelenting (06-28-2008)

  • #5
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    oh.. oops. I kind of got distracted before posting this and others snuck in



    Dan
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #6
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,861
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Quote Originally Posted by whizard View Post
    use mysql_num_rows to get the number of results, then create a counter in the loop to know where you are and when you reach the last one, don't write a comma.

    HTH
    Dan
    but that requires a lot of comparisons, depending upon the number of results.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #7
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,068
    Thanks
    2
    Thanked 319 Times in 311 Posts
    implode/join will require an intermediate array to hold all the values.

    Using a counter/mysql_num_rows will cause the loop to take about 8x - 10x longer for each iteration.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #8
    Regular Coder
    Join Date
    Jan 2007
    Posts
    142
    Thanks
    9
    Thanked 1 Time in 1 Post
    Thanks for the replies. I have it working perfectly with CFMaBiSmAd's method.

    The problem is, I can't grasp what is happening.

    The .= part is the first time I have seen that combination, I actually thought it was a typo. Just so I completely understand what is happening, would you mind explaining how this works (in an intermediate users terms )

  • #9
    Regular Coder
    Join Date
    Jan 2007
    Posts
    142
    Thanks
    9
    Thanked 1 Time in 1 Post
    Oh, one other thing, it is still placing a &nbsp; before and after the comma.

  • #10
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,068
    Thanks
    2
    Thanked 319 Times in 311 Posts
    For the first question - http://www.php.net/manual/en/languag...ors.string.php

    The &nbsp; must be part of your data in the database (or the code you posted is not everything present.) If it is part of your data and you want to leave it in your database, then a simple str_replace() can be used to remove them.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • Users who have thanked CFMaBiSmAd for this post:

    unrelenting (06-28-2008)

  • #11
    Regular Coder
    Join Date
    Jan 2007
    Posts
    142
    Thanks
    9
    Thanked 1 Time in 1 Post
    Quote Originally Posted by CFMaBiSmAd View Post
    For the first question - http://www.php.net/manual/en/languag...ors.string.php

    The &nbsp; must be part of your data in the database (or the code you posted is not everything present.) If it is part of your data and you want to leave it in your database, then a simple str_replace() can be used to remove them.
    You are correct. Thank you on both counts.

  • #12
    Regular Coder
    Join Date
    Jan 2007
    Posts
    142
    Thanks
    9
    Thanked 1 Time in 1 Post
    This is making me feel stupid.

    I have tried to add a "and " on the last name. By looking at the code I have to assume that:

    Code:
    $content .= $gm['name'] . ', ';
    generates the "John, "

    and the:

    Code:
    $content = rtrim($content,', ');
    generates the "Robert"

    If this is so then why doesn't this work:

    Code:
    $content = substr_replace((rtrim($content,', ')), 'and ', 0, 0);
    When I use that it shows up like this:

    "and the names are and John, Robert"

    I just don't get it.

  • #13
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    PHP Code:
    $content preg_replace("/,([^,]+)$/"," and$1",$content); 
    regards

  • #14
    Regular Coder
    Join Date
    Jan 2007
    Posts
    142
    Thanks
    9
    Thanked 1 Time in 1 Post
    Quote Originally Posted by oesxyl View Post
    PHP Code:
    $content preg_replace("/,([^,]+)$/"," and$1",$content); 
    regards
    Thanks but that didn't do it either.

    What I have:

    Code:
    $content = "";
    while ($gm = mysql_fetch_array($get_gm))
    {
        $content .= $gm['name'] . ', ';
    }
    $content = preg_replace("/,([^,]+)$/"," and$1",$content);
    echo $content;
    And using my John and Robert example it gives me this result:

    "and the names are Robert and ."

  • #15
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by unrelenting View Post
    And using my John and Robert example it gives me this result:

    "and the names are Robert and ."
    Code:
    $content = "";
    while ($gm = mysql_fetch_array($get_gm))
    {
        $content .= $gm['name'] . ', ';
    }
    echo $content;
    $content = preg_replace("/,([^,]+)$/"," and$1",$content);
    echo $content;
    do you have a comma between John and Robert before replace?
    this could be the problem and in that case I suggest to try using your previous code and if don't work use preg_replace.

    PS: I test preg_replace and work.

    regards


  •  

    Posting Permissions

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