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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

$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);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.