SitePoint Sponsor

User Tag List

Results 1 to 21 of 21
  1. #1
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Last comma trouble?!?!

    I'm trying to remove last comma from an loop/array like this:
    PHP Code:
    $currGroups "";
    while(
    $row $db->sql_fetchrow($sql)){
        
    $currGroups .= "<a href='".$siteurl."/".$row["seoname"]."'>".$row["clubname"]."</a>, ";
    }

    $currGroups rtrim($currGroups",");
    echo 
    $currGroups
    But with no luck...

    I have also tryied these examples:
    PHP Code:
    $currGroups=eregi_replace(',$'''$currGroups);
    $currGroups substr($currGroups0strlen($currGroups) - strlen(','));
    $currGroups=substr($currGroups0, -1); 
    The string comes out eaither with comma behind all results or with no commas at all...

    What am I doing wrong?

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,097
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Look carefully. Your string ends in comma-space.

    So you should use:
    rtrim($currGroups, ",<<space-here>>");
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  3. #3
    SitePoint Enthusiast
    Join Date
    Sep 2006
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'd use substr() instead:
    Code:
    $currGroups = substr($currGroups, 0, strlen($currGroups) - 2);
    ArticleQueen: First Article Rewriter with Lexical Processor!

  4. #4
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,097
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by vnviews View Post
    I'd use substr() instead:
    Code:
    $currGroups = substr($currGroups, 0, strlen($currGroups) - 2);
    The you can also use
    PHP Code:
    $currGroups substr($currGroups0, -2); 
    No need for strlen()
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  5. #5
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ScallioXTX View Post
    The you can also use
    PHP Code:
    $currGroups substr($currGroups0, -2); 
    No need for strlen()
    If I do so it removes all commas in the string?!?!

  6. #6
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,097
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    No.

    PHP Code:
    $currGroups substr($currGroups0strlen($currGroups) - 2); 
    and

    PHP Code:
    $currGroups substr($currGroups0, -2); 
    Do exactly the same thing, only with different syntax.
    See http://nl3.php.net/manual/en/function.substr.php
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  7. #7
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ScallioXTX View Post
    No.

    PHP Code:
    $currGroups substr($currGroups0strlen($currGroups) - 2); 
    and

    PHP Code:
    $currGroups substr($currGroups0, -2); 
    Do exactly the same thing, only with different syntax.
    See http://nl3.php.net/manual/en/function.substr.php
    But still. None of the above does the trick... It either removes the commas from all or leave all the commas...

  8. #8
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,097
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by jmansa View Post
    But still. None of the above does the trick... It either removes the commas from all or leave all the commas...
    Now that's just plain weird. Could you show the codes you've tried?
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  9. #9
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    It sounds like you're using substr inside the loop; don't. (Though, I could be wrong.)

    As another route, you could build an array and use implode to join them with commas:

    PHP Code:
    $currGroups = array(); 
    while(
    $row $db->sql_fetchrow($sql)){ 
        
    $currGroups[] = "<a href='".$siteurl."/".$row["seoname"]."'>".$row["clubname"]."</a>"


    $currGroups implode(", "$currGroups);
    echo 
    $currGroups
    Salathe
    Software Developer and PHP Manual Author.

  10. #10
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Salathe View Post
    It sounds like you're using substr inside the loop; don't. (Though, I could be wrong.)

    As another route, you could build an array and use implode to join them with commas:

    PHP Code:
    $currGroups = array(); 
    while(
    $row $db->sql_fetchrow($sql)){ 
        
    $currGroups[] = "<a href='".$siteurl."/".$row["seoname"]."'>".$row["clubname"]."</a>"


    $currGroups implode(", "$currGroups);
    echo 
    $currGroups
    But then I don't get any results?

  11. #11
    SitePoint Wizard
    Join Date
    Nov 2005
    Posts
    1,191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But then I don't get any results?
    .
    Now that's just plain weird. Could you show the codes you've tried?

  12. #12
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by hash View Post
    .
    PHP Code:
    $sql $db->sql_query("SELECT * FROM ".$prefix."_club_users WHERE new_userid='$uid'");
    while(
    $row $db->sql_fetchrow($sql)){

        
    $myclubid $row['clubid'];
        
        
    $sql2 $db->sql_query("SELECT * FROM ".$prefix."_club WHERE clubid='$myclubid' ORDER BY clubname ASC");
        
    $currGroups "";
        while(
    $row2 $db->sql_fetchrow($sql2)){
            
            
    $currGroups .= "<a href='".$siteurl."/".$row2["seoname"]."'>".$row2["clubname"]."</a>, ";
            
        }
        
        
    $currGroups substr($currGroups0strlen($currGroups) - 1);
        
    //$currGroups = rtrim($currGroups, ",");
        //$currGroups=eregi_replace(',$', '', $currGroups);
        //$currGroups = substr($currGroups, 0, strlen($currGroups) - strlen(','));
        //$currGroups=substr($currGroups, 0, -1); 
        
    echo $currGroups;


    Hope this helps!

  13. #13
    SitePoint Wizard
    Join Date
    Nov 2005
    Posts
    1,191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $currGroups = substr($currGroups, 0, strlen($currGroups) - 1);

    try changing -1 to -2 as mentioned above

  14. #14
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How about like this instead of replacing last comma:
    PHP Code:
    $sql $db->sql_query("SELECT * FROM ".$prefix."_club_users WHERE new_userid='$uid'"); 
    while(
    $row $db->sql_fetchrow($sql)){ 
        
    $myclubid $row['clubid']; 
        
    $sql2 $db->sql_query("SELECT * FROM ".$prefix."_club WHERE clubid='$myclubid' ORDER BY clubname ASC"); 
        
    $currGroups = array(); 
        while(
    $row2 $db->sql_fetchrow($sql2)){ 
            
    $currGroups[] = "<a href='".$siteurl."/".$row2["seoname"]."'>".$row2["clubname"]."</a>"
        } 
        echo 
    implode(", "$currGroups); 

    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  15. #15
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have tryid all above... Nothing works?!?!

  16. #16
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,097
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by jmansa View Post
    I have tryid all above... Nothing works?!?!
    Quote Originally Posted by ScallioXTX View Post
    Now that's just plain weird. Could you show the codes you've tried?
    .
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  17. #17
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jmansa View Post
    I have tryid all above... Nothing works?!?!
    I don't know what is your requirement and you tried the way what i have suggested above or not but the following just works fine.
    PHP Code:
    $arr = array();
    $arr[] = "<a href='mysite/seoname1'>One</a>";
    $arr[] = "<a href='mysite/seoname2'>Two</a>";
    $arr[] = "<a href='mysite/seoname3'>Three</a>";
    $arr[] = "<a href='mysite/seoname4'>One</a>";

    echo 
    implode(", "$arr); 
    Can you post your latest code that you tried?
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  18. #18
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rajug View Post
    I don't know what is your requirement and you tried the way what i have suggested above or not but the following just works fine.
    PHP Code:
    $arr = array();
    $arr[] = "<a href='mysite/seoname1'>One</a>";
    $arr[] = "<a href='mysite/seoname2'>Two</a>";
    $arr[] = "<a href='mysite/seoname3'>Three</a>";
    $arr[] = "<a href='mysite/seoname4'>One</a>";

    echo 
    implode(", "$arr); 
    Can you post your latest code that you tried?
    I think I now where it goes wrong... Its the two sql calls... How can I get that to be only one...?

  19. #19
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jmansa View Post
    I think I now where it goes wrong... Its the two sql calls... How can I get that to be only one...?
    What do you mean by 'its the two sql calls...'? And which two calls you want to make it one?
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  20. #20
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jmansa View Post
    I think I now where it goes wrong... Its the two sql calls... How can I get that to be only one...?
    And VIOLA!!!!
    PHP Code:
    $sql "SELECT * FROM ".$prefix."_club c 
                INNER JOIN
                (SELECT * FROM "
    .$prefix."_club_users WHERE new_userid = $uid) AS cu
                ON c.clubid = cu.clubid
                WHERE cu.new_userid = 
    $uid";
    $result mysql_query($sql) or die("Error: (" mysql_errno() . ") " mysql_error());
    $arr = array();
    while(
    $row mysql_fetch_array($result)){ 
        
    $arr[] = "<a href='".$siteurl."/".$row["seoname"]."'>".$row["clubname"]."</a>";
    }
    echo 
    implode(", "$arr); 
    Thanks guys for all the help ;-)

  21. #21
    SitePoint Guru
    Join Date
    Oct 2006
    Location
    Queensland, Australia
    Posts
    852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In other words, it had nothing to do with the comma's, but you just weren't getting any results from that particular query?


Bookmarks

Posting Permissions

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