fantastapotamus Posted April 19, 2008 Share Posted April 19, 2008 Hi all, I'm new to php and I've been having problems implementing it into a (mock) website I'm woking on. I'm trying to implement tagging; so a user can either tag an item with what they seem fit, or click on a tag and that will retrieve a list of other items that has that tag. I've managed (just!) to allow users to tag and I can retrieve all tags that an item has (should probably mention that the items - its a fake e-commerce site - aren't stored in a database, just on seperate html pages) but don't know how to retrieve them as a HTML link, which the user can click on and then see a list of all other items with that tag! It's driving me a bit bonkers! I was surprised that I couldn't find a decent tutorial of implementing tagging - which I was kind of hoping on. Anyway, if anyone can help I'd be most appreciated. Quote Link to comment Share on other sites More sharing options...
unidox Posted April 19, 2008 Share Posted April 19, 2008 Try it this way: q = mysql_query(); while ($r = mysql_fetch_array($q)) { echo "<a href=\"" . $r['link'] . "\">Link</a>"; } Quote Link to comment Share on other sites More sharing options...
fantastapotamus Posted April 19, 2008 Author Share Posted April 19, 2008 Thanks for the response. Would you be able to just help me out with that in the context of my code? As I'm trying to get it to link to all pages that have that 'tag' on them, what 'link' should that point to? A new page? And what should be on that page? I thought I might have to pass a variable in the url - but I'm not entirely sure what that means (it just looked like something I had to when I was reading a php book!) Ta again Quote Link to comment Share on other sites More sharing options...
unidox Posted April 19, 2008 Share Posted April 19, 2008 Sure, post the code. Quote Link to comment Share on other sites More sharing options...
fantastapotamus Posted April 19, 2008 Author Share Posted April 19, 2008 Sure thing. <?php //connect to MySQL $connect = mysql_connect('localhost', 'xxx', 'xxx' or die(mysql_error()); mysql_select_db('xxx') or die (mysql_error()); //select tags for movie $query = 'SELECT * FROM tags WHERE name = "enchanted"'; $result = mysql_query($query, $connect) or die(mysql_error()); while ($nt=mysql_fetch_array($result)) { echo '<a href=\"tags.php?id='. $nt['id'] .' ">'.$nt['tags'].'</a>'; } ?> //for the user to insert a new tag into the database <form action="insert.php" method="post"> Tag: <input type="text" name="name" /> <input type="submit" /> </form> I tried another bit of code that I found on the forum, but I messed it up. What I'm unsure about is when the user clicks the tag - I'm guessing it needs to open a new php page? If so, what should be in that page? How do I do it so taht page knows what to retrieve (in this case, all items that have the same tag?) Many thanks Quote Link to comment Share on other sites More sharing options...
unidox Posted April 19, 2008 Share Posted April 19, 2008 <?php //connect to MySQL $connect = mysql_connect('localhost', 'xxx', 'xxx') or die(mysql_error()); mysql_select_db('xxx') or die(mysql_error()); //select tags for movie $query = 'SELECT * FROM `tags` WHERE `name` = "enchanted"'; $result = mysql_query($query, $connect) or die(mysql_error()); while ($nt=mysql_fetch_array($result)) { echo "<a href=\"tags.php?id=" . $nt['id'] . "\">" . $nt['tags'] . "</a>"; } ?> //for the user to insert a new tag into the database <form action="insert.php" method="post"> Tag: <input type="text" name="name" /> <input type="submit" /> </form> Now in tag.php in order to find the right row in mysql, use get. Like: $id = $_GET['id']; $q = mysql_query("SELECT * FROM `table` WHERE `table_id` = '$id'") or die(mysql_error()); Quote Link to comment Share on other sites More sharing options...
fantastapotamus Posted April 19, 2008 Author Share Posted April 19, 2008 I think I've messed up the query! while ($nt=mysql_fetch_array($result)) { echo '<a href=\"tags.php?id=" .$nt["id"]">[/url]'; } Doesn't display anything at all now. You posted above: while ($r = mysql_fetch_array($q)) { echo "<a href=\"" . $r['link'] . "\">Link[/url]"; } Could you tell me what should be in 'link' for me? The new tags.php? Quote Link to comment Share on other sites More sharing options...
fantastapotamus Posted April 19, 2008 Author Share Posted April 19, 2008 Sorry, that should be; $result = mysql_query($query, $connect) or die(mysql_error()); while ($nt=mysql_fetch_array($result)) { echo '<a href="tags.php?id=" .$nt["id"]"></a>'; } I'm really having trouble understanding whats in that echo statement. The tag table has tag_id (which is auto increment and PK) and name (name is what the user has typed in and submitted). So I'm trying to get it to display name as a HTML link and when the user clicks it, it uses the id to pass into tags.php? I think! 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.