Jump to content

Taggin system (super noobish)


zhangy

Recommended Posts

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'];

}
?>

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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?

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 />";
?>

 

Link to comment
Share on other sites

      $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 );

Link to comment
Share on other sites

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 );  
        }  
    }  

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.