Reputation: 803
I am storing a list of tags in a comma separated list in my database.
An example of this data is:
Graphic Design, Illustration, Animation
I am outputting the comma separated list and creating the links with the following:
$Tags = explode(', ', $value['Tags']);
foreach($Tags as $key => $value) {
echo '<a href="' . BASE_URL . 'news/tag/' . $value . '">' . $value . '</a>';
}
When you click on a link it uses the query
WHERE Tags LIKE "%' . $tag . '%"
This only seems to work if I click on the second, third, fourth, etc. link but not the first one.
I am assuming it has something to do with the comma or white space.
Upvotes: 0
Views: 681
Reputation: 24116
No need to explode on command AND space. Explode on comma only and trim space later.
Try this:
$Tags = explode(',', 'Graphic Design, Illustration, Animation');
foreach ($Tags as $tag) {
$tag = trim($tag);
echo '<a href="' . BASE_URL . 'news/tag/' . $tag . '">' . $tag . '</a><br>';
}
Then on your query, do the following:
WHERE Tags LIKE "%' . urldecode(trim($tag)) . '%"
Outputs:
<a href="BASE_URLnews/tag/Graphic Design">Graphic Design</a><br>
<a href="BASE_URLnews/tag/Illustration">Illustration</a><br>
<a href="BASE_URLnews/tag/Animation">Animation</a><br>
Upvotes: 1
Reputation: 2511
The first tag "Graphic Design" has a space which is unsafe in urls. Remove it, replace it or use urlencode()
:
$Tags = explode(', ', $value['Tags']);
foreach($Tags as $key => $value) {
echo '<a href="' . BASE_URL . 'news/tag/' . urlencode($value) . '">' . $value . '</a>';
}
Upvotes: 0