Last comma trouble?!?!

I’m trying to remove last comma from an loop/array like this:

$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:

$currGroups=eregi_replace(',$', '', $currGroups);
$currGroups = substr($currGroups, 0, strlen($currGroups) - strlen(','));
$currGroups=substr($currGroups, 0, -1);

The string comes out eaither with comma behind all results or with no commas at all…

What am I doing wrong?

In other words, it had nothing to do with the comma’s, but you just weren’t getting any results from that particular query?

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.


$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?

And VIOLA!!!

$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 :wink:

What do you mean by ‘its the two sql calls…’? And which two calls you want to make it one?

I think I now where it goes wrong… Its the two sql calls… How can I get that to be only one…?

.

$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($currGroups, 0, strlen($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!

How about like this instead of replacing last comma:


$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); 
} 

I have tryid all above… Nothing works!!!

$currGroups = substr($currGroups, 0, strlen($currGroups) - 1);

try changing -1 to -2 as mentioned above

But then I don’t get any results?

No.


$currGroups = substr($currGroups, 0, strlen($currGroups) - 2);

and


$currGroups = substr($currGroups, 0, -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…

Look carefully. Your string ends in comma-space.

So you should use:
rtrim($currGroups, “,<<space-here>>”);

The you can also use


$currGroups = substr($currGroups, 0, -2);

No need for strlen() :slight_smile:

I’d use substr() instead:


$currGroups = substr($currGroups, 0, strlen($currGroups) - 2);

If I do so it removes all commas in the string?!?!

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:

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

$currGroups = implode(", ", $currGroups);
echo $currGroups;

Now that’s just plain weird. Could you show the codes you’ve tried?