开发者

display tags one by one

Hi I have tags in my database in a row called tags. They are separated by commas.

For example, they are stored in the database as tag1,tag2,tag3.

I want to retrieve them from the database and display them one by one separately and before displaying each tag, I want to link it to a URL.

Here's what I am doing so far,

$keywords = strip_tags($blog_query_results['keywords']); //Retrives the tags from the database

echo wordwrap(stripslashes($keywords), 65, "<br>",true); // this prints tag1,tag2, and so on.

While printing them I want to link tag1, tag2, and ta开发者_Go百科g3 to different URLS.


If you have a string like this :

$tags = 'tag1,tag2,tag3';

You can use the explode() function to get an array of tags :

$arr = explode(',', $tags);

And, then, just iterate over that array, to build a link for each item -- typically, using foreach() :

foreach ($arr as $t) {
    echo '<a href="...">' . htmlspecialchars($t, ENT_COMPAT, 'UTF-8') . '</a><br />';
}


As a sidenote : your database's design is probably a bit wrong, if you store more than one information in a single field.

If you have 3 tags for a post, you should have 3 rows (one per tag), either :

  • In the tags table,
  • Or in a join-table between your posts and tags tables.


You can do:

$tags = explode(',', $keywords);
foreach($tags as $tag)
{
    echo "<a href='...' >$tag</a>";
}

etc...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜