unrelenting 06-28-2008, 04:36 PM 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:
while ($gm = mysql_fetch_array($get_gm))
{
echo $gm['name'] . ', ';
}
CFMaBiSmAd 06-28-2008, 04:44 PM 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 -
$content = "";
while ($gm = mysql_fetch_array($get_gm))
{
$content .= $gm['name'] . ', ';
}
$content = rtrim($content,', ');
echo $content;
abduraooft 06-28-2008, 04:46 PM http://us2.php.net/join may also help.
whizard 06-28-2008, 04:49 PM 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
whizard 06-28-2008, 04:51 PM oh.. oops. I kind of got distracted before posting this and others snuck in
:D
Dan
abduraooft 06-28-2008, 04:55 PM 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
Danbut that requires a lot of comparisons, depending upon the number of results.
CFMaBiSmAd 06-28-2008, 04:59 PM 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.
unrelenting 06-28-2008, 05:02 PM 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 :D)
unrelenting 06-28-2008, 05:04 PM Oh, one other thing, it is still placing a before and after the comma.
CFMaBiSmAd 06-28-2008, 05:11 PM For the first question - http://www.php.net/manual/en/language.operators.string.php
The 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.
unrelenting 06-28-2008, 05:25 PM For the first question - http://www.php.net/manual/en/language.operators.string.php
The 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.
unrelenting 06-28-2008, 06:15 PM 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:
$content .= $gm['name'] . ', ';
generates the "John, "
and the:
$content = rtrim($content,', ');
generates the "Robert"
If this is so then why doesn't this work:
$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.
oesxyl 06-29-2008, 01:31 AM $content = preg_replace("/,([^,]+)$/"," and$1",$content);
regards
unrelenting 06-29-2008, 02:40 AM $content = preg_replace("/,([^,]+)$/"," and$1",$content);
regards
Thanks but that didn't do it either.
What I have:
$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 ."
oesxyl 06-29-2008, 02:52 AM And using my John and Robert example it gives me this result:
"and the names are Robert and ."
$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
|
|