...

View Full Version : separating variables



kred
09-08-2005, 09:26 PM
Hi all,
I'm trying to pull a block of 3 to 4 cities from a DB

but I can't get them separated without having the last city listed having the "|"
ie. what I get:
albany | manhattan | long island | buffalo |
what I want:
albany | manhattan | long island | buffalo

what is the function to get this to come out right?
Thanks for the help
Ken

devinemke
09-08-2005, 09:52 PM
what is the function to get this to come out right?
implode (http://www.php.net/implode)

marek_mar
09-08-2005, 09:52 PM
Implode is the easyest way. You could also use substr() to remove the chars but it's a way around in most cases.

CrzySdrs
09-08-2005, 09:56 PM
I may interpreting your post incorrectly, but I assume you are pulling items out of a db, and displaying them on the page and trying not to have that final bar when you display it. If I am incorrect, ignore this post. :rolleyes:



//Untested code: May contain syntactical errors.
$output = "";

while($row = mysql_fetch_assoc($rs))
{
//Appending to Output the bar and the name of the city
$output .= $row['name'] . " | ";
}

//Verifying that there is something in the string.
if (strlen($output) > 0)
{
//Removing the Final Bar
$output = substr($output, 0, strlen($output) - 3);

}

echo $output;

kred
09-08-2005, 11:02 PM
Thanks CrzySdrs, that is exactly what I needed.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum