svgmx5 Posted May 26, 2011 Share Posted May 26, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/237484-seperate-items-from-database-with-a-comma/ Share on other sites More sharing options...
gizmola Posted May 26, 2011 Share Posted May 26, 2011 Build you string with all commas at the end, then: $string = trim($string, ','); Quote Link to comment https://forums.phpfreaks.com/topic/237484-seperate-items-from-database-with-a-comma/#findComment-1220323 Share on other sites More sharing options...
svgmx5 Posted May 26, 2011 Author Share Posted May 26, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/237484-seperate-items-from-database-with-a-comma/#findComment-1220329 Share on other sites More sharing options...
gizmola Posted May 26, 2011 Share Posted May 26, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/237484-seperate-items-from-database-with-a-comma/#findComment-1220332 Share on other sites More sharing options...
jcbones Posted May 26, 2011 Share Posted May 26, 2011 $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); Quote Link to comment https://forums.phpfreaks.com/topic/237484-seperate-items-from-database-with-a-comma/#findComment-1220339 Share on other sites More sharing options...
svgmx5 Posted May 26, 2011 Author Share Posted May 26, 2011 Thanks! that seems to work. i'm still updating the scripts so i'll post if something comes up that Quote Link to comment https://forums.phpfreaks.com/topic/237484-seperate-items-from-database-with-a-comma/#findComment-1220492 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.