Long backstory on this, and this seems to be the simplest solution. I have a table that will populate with user id's from a database. I need to figure out a way to add a ma between id's if there is more than one. The td id is username
.
I'm not sure where to really start. I would need to index it and see if the html is > 1. And if so, add a ma between each word. Please help. This is a p.o.c forum and it doesn't suit me to add/use jQuery, so I need to do it in vanilla JavaScript.
Here is the code that pulls it from the database:
if ($this->post['post_thanks_amount'] > 0 && $this->thread['isdeleted'] == 0)
{
$this->post['post_thanks_bit'] = fetch_thanks_bit($this->thread['forumid'], $thanks);
$this->post['post_thanks_user'] = $post_thanks_user;
$this->post['post_thanks_amount_formatted'] = vb_number_format($this->post['post_thanks_amount']);
$post_thanks_box = fetch_post_thanks_template($this->post);
}
And the output with one user is this:
<tr valign="top">
<td style="background:#E8E8E8;" colspan="2" class="alt1">
<div>
<a rel="nofollow" href="member.php?u=20420">ninja1</a>
<a rel="nofollow" href="member.php?u=26154">testuser</a>
</div>
</td>
</tr>
so after each <a...>
if there is more than one, add a ma.
Long backstory on this, and this seems to be the simplest solution. I have a table that will populate with user id's from a database. I need to figure out a way to add a ma between id's if there is more than one. The td id is username
.
I'm not sure where to really start. I would need to index it and see if the html is > 1. And if so, add a ma between each word. Please help. This is a p.o.c forum and it doesn't suit me to add/use jQuery, so I need to do it in vanilla JavaScript.
Here is the code that pulls it from the database:
if ($this->post['post_thanks_amount'] > 0 && $this->thread['isdeleted'] == 0)
{
$this->post['post_thanks_bit'] = fetch_thanks_bit($this->thread['forumid'], $thanks);
$this->post['post_thanks_user'] = $post_thanks_user;
$this->post['post_thanks_amount_formatted'] = vb_number_format($this->post['post_thanks_amount']);
$post_thanks_box = fetch_post_thanks_template($this->post);
}
And the output with one user is this:
<tr valign="top">
<td style="background:#E8E8E8;" colspan="2" class="alt1">
<div>
<a rel="nofollow" href="member.php?u=20420">ninja1</a>
<a rel="nofollow" href="member.php?u=26154">testuser</a>
</div>
</td>
</tr>
so after each <a...>
if there is more than one, add a ma.
- Can you show us your current table and what should it like after the processing ? – VirtualTroll Commented May 26, 2011 at 14:53
- What do you mean? You want mas between the actual td contents? How will you know if you have more than one? What are you using to read from the database? – user686605 Commented May 26, 2011 at 14:54
- Your question is quite vague. You say the id's e from the database, but what 'format' is the data returned? How are your is your datastucture for those id's in js? In an array, an object, json, ? All of these formats would have different solutions. – Stephane Gosselin Commented May 26, 2011 at 14:57
- Apologies all, pls see edited question.thx – Dirty Bird Design Commented May 26, 2011 at 14:59
- 2 How are the links generated on the server side? Can't you add mas in PHP? – Felix Kling Commented May 26, 2011 at 15:08
2 Answers
Reset to default 5If ids is an array, then just use join
:
ids = [1, 2, 3, 4];
val = ids.join(', ');
// val is now "1, 2, 3, 4"
split()
automatically puts ma between words, if that's what you're looking for.
var words = 'a b c d';
document.write(words.split(' '));