zhangy Posted June 22, 2009 Share Posted June 22, 2009 Hello, I am trying to create for the first time a tagging system for my blog but I dont know very much about how to code this type of system properly. Does anyone know an easy way to do this? So far I have tags for each blog entry being stored in an mysql database row, varchar datatype, and am using the bellow code to output them. I guess what I am wondering now is how to make it so they output as links to their specific tag category. <?php require_once('load_data.php'); if (!mysql_connect($db_host, $db_user, $db_pwd)) die("Can't connect to database"); if (!mysql_select_db($database)) die("Can't select database"); $id = (int) $_GET['id']; $sql = "SELECT * FROM $table WHERE id=$id"; $result = mysql_query($sql) or die("Error ". mysql_error(). " with query ". $sql); if(mysql_num_rows($result) == 1){ $row = mysql_fetch_array($result); echo $row['tags']; } ?> Quote Link to comment Share on other sites More sharing options...
gevans Posted June 22, 2009 Share Posted June 22, 2009 Do you have any connection between the two db tables? i.e. does the tags table hold the id of the relative blog entry? Quote Link to comment Share on other sites More sharing options...
zhangy Posted June 22, 2009 Author Share Posted June 22, 2009 oh, I dont have a table dedicated to tags. Again Im not really sure how this works. As of now the table looks something like this: ID Title Entry Tags Date so the tags are going in a column in a row that holds the rest of the data associated with a post. Is this not the right way to do it? Quote Link to comment Share on other sites More sharing options...
gevans Posted June 22, 2009 Share Posted June 22, 2009 You can do it this way, It may proove less efficient if/when the system grows. But for day to day needs it will work fine. When you get tags from an sql query you will also be able to retrieve the relevant blog details. As they are all in the same row? Quote Link to comment Share on other sites More sharing options...
zhangy Posted June 22, 2009 Author Share Posted June 22, 2009 Well, I'm not sure if this is the best way to input them into the database but (as I dont know another way) I just have a html form field for tags and manually type them in as tag1, tag2, tag3. But now I'm wondering if these three tags are all put in the same column of the same row how can they be identified individually later on? Quote Link to comment Share on other sites More sharing options...
gevans Posted June 22, 2009 Share Posted June 22, 2009 You'd do that using some php. If you ensure that the tags are seperate via commas, you can split them via commas later. <?php $foo = "tag1, tag2, tag3, tag4"; $foo_array = explode(",", $foo); foreach($foo_array as $value) { $value = trim($value); echo $value ."<br />"; } ?> Have a look at whats going on there! Quote Link to comment Share on other sites More sharing options...
zhangy Posted June 22, 2009 Author Share Posted June 22, 2009 Oh i see, thats awesome! Thanks gevans. One last thing though, how to output them as links to their own specific category? For example, "tag1" as a link to a page listing other posts that have the same tag. Quote Link to comment Share on other sites More sharing options...
gevans Posted June 22, 2009 Share Posted June 22, 2009 If you want to use a simple get request, you can ammend the above to this; <?php $foo = "tag1, tag2, tag3, tag4"; $foo_array = explode(",", $foo); foreach($foo_array as $value) { $value = trim($value); echo '<a href="your-domain.com/your-page.php?tag='.$value.'>'.$value ."</a><br />"; } ?> On the foloowing page you need to use $_GET['tag'] to get the string, and check for that string in your db. Quote Link to comment Share on other sites More sharing options...
zhangy Posted June 22, 2009 Author Share Posted June 22, 2009 thanks gevan that helps me a lot. you rule! Quote Link to comment Share on other sites More sharing options...
zhangy Posted June 27, 2009 Author Share Posted June 27, 2009 Hi, the code is working perfectly but I was wondering if there is a way to have commas show in between tags? for example: tag1, tag2, tag3 At the moment I enter them into the database using commas but on the output they dont show up. <?php echo 'Tags: '; $tags = $row['tags']; $tags_array = explode(",", $tags); foreach($tags_array as $value) { $value = trim($value); echo '<a href="your-domain.com/your-page.php?tag='.$value.'">'.$value ."</a><br />"; ?> Quote Link to comment Share on other sites More sharing options...
Axeia Posted June 27, 2009 Share Posted June 27, 2009 $links = array(); foreach($tags_array as $value) { $value = trim($value); $links[] = '<a href="your-domain.com/your-page.php?tag='.$value.'">'.$value ."</a><br />"; } echo implode( ', ', $links ); Quote Link to comment Share on other sites More sharing options...
zhangy Posted June 27, 2009 Author Share Posted June 27, 2009 Axeia thanks, that looks good, but for some reason is still not outputting anything. ??? if(mysql_num_rows($result) == 1){ $row = mysql_fetch_array($result); echo 'Tags: '; $tags = $row['tags']; $tags_array = explode(",", $tags); $links = array(); foreach($tags_array as $value) { $value = trim($value); $links[] = '<a href="mysite.com/post.php?tag='.$value.'" title="view posts tagged '.$value.'">'.$value ."</a> "; } echo implode( ', ', $links ); } } 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.