Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

remove a , from a string 1

Status
Not open for further replies.

MJB3K

Programmer
Jul 16, 2004
524
GB
Hi, i have this code which outputs data from a MySQL db seperated by ,'s. i would like to know how to remove the last , from the list... any ideas??

mycode:
Code:
<?php

include "../../nep-yb/config.php";

$i=0;

$qT = mysql_query("SELECT uid FROM member") or die(mysql_error());
$amt = mysql_num_rows($qT);
echo "&amnt=$amt";
//
echo "&user=";
$q = mysql_query("SELECT nick FROM member") or die(mysql_error());
	while(list($n) = mysql_fetch_row($q)){
		echo $n . ",";
	}
//
echo "&uid=";
$q = mysql_query("SELECT uid FROM member") or die(mysql_error());
	while(list($uid) = mysql_fetch_row($q)){
		echo $uid . ",";
	}
?>

any ideas??


Regards,

Martin

Gaming Help And Info:
 
Instead of building the list like you're doing, use a temporary array and then use implode to echo your comma seperated string.
Code:
$tmp = array();
$q = mysql_query("SELECT nick FROM member") or die(mysql_error());
    while(list($n) = mysql_fetch_row($q))
        $tmp[] = $n;
    echo implode(',',$tmp);
Do the same for your other list.

Ken
 
Code:
<?php

include "../../nep-yb/config.php";

$i=0;

$qT = mysql_query("SELECT uid FROM member") or die(mysql_error());
$amt = mysql_num_rows($qT);
echo "&amnt=$amt";
//
echo "&user=";
$q = mysql_query("SELECT nick FROM member") or die(mysql_error());
    while(list($n) = mysql_fetch_row($q)){
        echo $n . ($i++<=$amt)?",":"";
    }
//
echo "&uid=";
$i=0;
$q = mysql_query("SELECT uid FROM member") or die(mysql_error());
    while(list($uid) = mysql_fetch_row($q)){
        echo $uid . ($i++<=$amt)?",":"";
    }
?>

----------
HTH
Gavin Ostlund
 
Ok, I admit, I was trying to be lazy and change as little as possible by just inserting 3 small parts... I shoulda thought it through and done it right, which I'll endevour to do now.
Code:
<?php

include "../../nep-yb/config.php";

$i=0;

$qT = mysql_query("SELECT uid FROM member") or die(mysql_error());
$amt = mysql_num_rows($qT);
echo "&amnt=$amt";
//
echo "&user=";
$q = mysql_query("SELECT nick FROM member") or die(mysql_error());
    while(list($n) = mysql_fetch_row($q)){
        echo $n;
        if($i++<=$amt)
                echo ",";
    }
//
echo "&uid=";
$i=0;
$q = mysql_query("SELECT uid FROM member") or die(mysql_error());
    while(list($uid) = mysql_fetch_row($q)){
        echo $uid;
        if($i++<=$amt)
                echo ",";
    }
?>

That *should* work better... however, I don't have immediate access to a php parser to confirm it...

----------
HTH
Gavin Ostlund
 
Aight, in my last submission, change the <= to just < and that should finally fix it...

----------
HTH
Gavin Ostlund
 
You can also do an
Code:
rtrim($string, ',');
to remove any trailing commas
 
When you said "none of them work", what happened when you tried the implode method?

Ken
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top