Jump to content

seperate items from database with a 'comma'


svgmx5

Recommended Posts

I want to retrieve items from the DB and seperate them using a comma...

 

while i know how to actually do that, what i want is to automatically have stop adding comas at the last item...

 

so if there are 4 items i want it to show a comma after each item except the last one (1, 2, 3, 4).

 

Does anyone know of a function or can give me an example on how i can do this.

how do you mean?

 

i mean right now i just have the following:


$get_point_tags = mysql_query("SELECT * FROM points_tags WHERE point_id='$point_id'") or die(mysql_error());

while($point_tags = mysql_fetch_assoc($get_point_tags)){
$tag_id = $point_tags['tag_id'];

$get_tags = mysql_query("SELECT * FROM tags WHERE id='$tag_id' ORDER BY tag_name ASC") or die(mysql_error());
$tag = mysql_fetch_assoc($get_tags);

echo '<a href="../tags/name/'.stringForUrl($tag['tag_name']).'">'.$tag['tag_name'].'</a>; ';
}

 

 

To Explain a little...

 

the first query grabs the id's of the category that are stored in a table and then it looks through them to grab them all. the second query grabs the acutal information for each item ID

 

and well it then echo's out the link ...

 

so how can i implement that string you said?

I want to retrieve items from the DB and seperate them using a comma...

 

while i know how to actually do that, what i want is to automatically have stop adding comas at the last item...

 

 

This was your question.  Where is the string that you are creating that has the items with commas after them? That is the one you use the trim on.

$sql = "SELECT b.tag_name FROM points_tags AS a JOIN tags AS b ON a.tag_id = b.id WHERE a.point_id = '$point_id'";
$get_point_tags = mysql_query($sql) or die($sql . ' has an error<br />' . mysql_error());

while($point_tags = mysql_fetch_assoc($get_point_tags)){
$arr[] = '<a href="../tags/name/'.stringForUrl($point_tags['tag_name']).'">'.$point_tags['tag_name'].'</a>; ';
}

echo implode(', ',$arr);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.