php_begins Posted July 21, 2011 Share Posted July 21, 2011 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. I know we can use php explode function. But i m not sure how to go about it. Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 21, 2011 Share Posted July 21, 2011 What code do you have so far? Can you run a query to get the value from the database. Show an example of the output you want to create. Give us something to go on. Quote Link to comment Share on other sites More sharing options...
php_begins Posted July 21, 2011 Author Share Posted July 21, 2011 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,tag3 to different URLS. Thanks. Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 21, 2011 Share Posted July 21, 2011 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,tag3 to different URLS. Ok, but where are you getting those URLs and how are you going to associate them with the correct tag? Here's some code to output the tags as links, but it just uses a hard-coded URL because I have no idea where you are getting them from. //Explode the DB result into an array using the commas $keywordsArray = explode(',', $blog_query_results['keywords']); //Iterate through each keyword foreach($keywordsArray as $keywordString) { //Parse the tag value. Don't know the purposes of stripslashes and strip_tags, but I left them in // - I would think htmlenteties woud be a better option $label = stripslashes(strip_tags(trim($keywordString))); //Get URL for the tag. You didn't provide any details, so this is just hard-coded $url = "http://www.yahoo.com"; //Output the final link echo "<a href=\"{$url}\">{$label}</a><br>\n"; } Quote Link to comment Share on other sites More sharing options...
php_begins Posted July 21, 2011 Author Share Posted July 21, 2011 Thank you, that is very close to what I wanted. I need to hardcode the url as $url=$rooturl."/".$tagname; so when i click on tag1 it should take me to $rooturl."/"$tag1, when i click on tag2 it should take me to $rooturl."/"$tag2 and so on Quote Link to comment Share on other sites More sharing options...
php_begins Posted July 21, 2011 Author Share Posted July 21, 2011 thank you i got it! This is what I did.. $url = $rooturl."/".$label; Thanks a lot again! Quote Link to comment 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.