Tibor SzászInteraction matters

  • Home
  • About me
  • Projects
  • Reqest price offer
  • Contact me
  • RSS

2009 1115

Easy tagging solution

This an easy solution for adding tag support for any website. This is not a complete script, or addon, just a method you can use easily. The result will produce a tag cloud, like the one you can see in my sidebar.
STEP 1: Setup. Let's assume that you have a site that stores the articles in a table. You have to add a field that will store the tags. The tags will be separated by a space character.


STEP 2: Generating the tag list:

function get()
{
	// CodeIgniter based database query.
	$this->db->select( 'p_tags');
	$this->db->from( 'posts' );
	$query = $this->db->get('posts');
    
	/**
	 *  If the loop on the tags founds a tag that is already 
	 *  represented in the $tag_cloud array as a key, then
	 *  its value will be inceremented by 1                  
	 */                 
	$tag_cloud = array();

	foreach ( $query->result_array() as $key => $t )
	{
	    $tags = $t['p_tags'];

	    if( $tags )
	    {
	        // Tags must be separated by 'space'
	        $tags = explode( ' ', $tags );
	        
            foreach( $tags as $tg )
	        {
    			if( !array_key_exists( $tg, $tag_cloud ) )
                {
                    $tag_cloud[ $tg ] = 1; // Default count
                }
                else
                {
                    $tag_cloud[ $tg ]++; // Tag exists, increment its value
                }
            }
        }
	}
	// Order by value, keep indexing, reverse order
	arsort( $tag_cloud );
	return $tag_cloud;
}

STEP 3: Filter posts for a tag. In my case the "posts" table's tag field is: "tagging php tutorial". If I want to list all the articles with the "php" tag, then I need to handle 4 cases: a tag can be the first tag, inside the string, the last and exact match.
$this->db->where( "p_tags LIKE '% $cat %' OR 
				   p_tags LIKE '$cat %' OR 
				   p_tags LIKE '% $cat'
				   p_tags = '$cat'" );
Note that the first and third rule has only one %.


Comments


Add new comment


Your name
Email
Your avatar
Website
Comment

Briefing

Me myself and I
I love coding, especially for the web. I'm in the online industry for 6 years. My favourite toys are:
JavaScript, PHP, XHTML, MySQL, Canvas, Flash
Váltás magyar nyelvre

Topics

  • Coffee break
  • Design & stuff
  • Generative arts
  • Programming

Friends

  • Kárpáti Róbert
  • Royaljerry
  • Bagi Zoltán
  • Pegazoo
  • Charles Alexander Deli
  • Bence Bangócs
  • Lunakid
  • Rapp design

Feeling so social

  • Facebook
  • IWIW
  • TurulMeme

Tags

javascript (12) php (6) mootools (4) iscaffold (3) codeigniter (3) crud (3) tutorial (2) google (2) renders (2) structure-synth (2) css (2) template (2) free (2) google-buzz (1) script (1) forge (1) hack (1) table (1) open-source (1) rc1 (1) infovis (1) rss (1) interface (1) canvas (1) design, (1) user (1) experience (1) soundcloud (1) resizing (1) joomla (1) bermuda-triangle (1) wordpress (1) drupal (1) data-visualisation (1) data-uri (1) konami-codes (1) tagging (1) slimbox (1) bugfix (1) cfdg (1) blackout (1) firefox-extension (1) utf8 (1) class (1) slideshow (1) nl2br (1) experiment (1) svg (1) library (1) ajax (1) sitebuild (1) tabs (1) inspiration (1) lists (1) drag-and-drop (1) wikipedia (1) dns (1) trick (1)




All rights reserved, ©2008-2010 - Built on CodeIgniter framework - Konami codes - Mostly Valid XHTML 1.1 - Valid CSS 2.1