Yeah. Finally i write some post again..
hehe.. This post talk about Tag Cloud on my previous post, but in this post I’ll show you how to integrate it into Code Igniter >> my favorite framework..
First you must create it to you own library..
this is my script:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); //tag_cloud.php class Tag_cloud{ private $arrTags; private $sort; private $fMaks; private $fMin; /** * konstruktor * @param $arrTags tag array * @param $sort Boolean sort by aphabet * @param $fMaks Int maximum size of font * @param $fMin Int minimum size of font */ function __construct($arrTags, $sort = true, $fMaks = 36, $fMin = 12){ if(!is_array($arrTags)){ exit('First Argument must array!'); } $this->arrTags = $arrTags; $this->sort = $sort; $this->fMaks = $fMaks; $this->fMin = $fMin; } /** *Create and print tag cloud * */ function show(){ $arrTags = $this->arrTags; if ($this->sort){ //sort by aphabet ksort($arrTags); } //maximum item $jmlMaks = max(array_values($arrTags)); //minimum item $jmlMin = min(array_values($arrTags)); //Range total max and min $range = $jmlMaks - $jmlMin; //avoid divide by zero if ($range == 0){ $range = 1; } //for increment font tag size $step = ($this->fMaks - $this->fMin)/$range; $size = 0; $arr_item=array(); $arr_jml=array(); $arr_size=array(); $out=array(); $n=0; while (list($item, $jml)=each($arrTags)){ //increment font size $size =round($this->fMin+(($jml-$jmlMin)*$step)); $arr_item[$n]=$item; $arr_jml[$n]=$jml; $arr_size[$n]=$size; $n++; /*echo '<a href="tag/'.$item.'" style="font-size:'.$size.'px" tittle="'.$jml.' item on tag'.$item.'"> '.$item.' </a>';*/ } $arr1 = array_values($arr_item); $arr2 = array_values($arr_jml); $arr3 = array_values($arr_size); foreach($arr1 as $key1 => $value1) { $out[(string)$value1][0] = $arr2[$key1]; $out[(string)$value1][1] = $arr3[$key1]; } //print_r($out); // this will print out the contents of your new array return $out; } } ?> |
This script will return 3 dimentional array.
Then create tags_model.php to call database tags..
for this example i have tags table with contain tag_id and tag_name where tag_id as primary key and tag_name as unique key.
This is tags_model.php
<?php class Tags_model extends Model{ function __construct() { parent::Model(); } function get_all() { $query=$this->db->get('tags'); return $query->result(); } } ?> |
Create active record on your Post model to count how many tags on colum tags.
For colum tags i have sparate each tag by commas. Example: code, igniter, php.
So to count how many post that contain each tag you must add this function:
function count_by_tag($tag) { $this->db->select('count(*) AS MyCount'); $this->db->from('posts'); $this->db->like('tag', $tag, 'both'); $count=$this->db->count_all_results(); return $count; } |
Don’t forget to add each model to controller:
$this->load->model('posts_model'); $this->load->model('tags_model'); |
Then this is script to get data tags from database and load your own tags library.
//tags $query=$this->tags_model->get_all(); $tags=array(); foreach ($query as $row): $count=$this->news_model->count_by_tag($row->tag_name); $tags[$row->tag_name]=$count; endforeach; $this->load->library('tag_cloud', $tags); |
This tag cloud library will return 3d array.. so you must parsing each array on your view. Then create it to tag cloud using component.
this is my view:
<?php $arr_tags=$this->tag_cloud->show(); //print_r($this->tag_cloud->show()); // this will print out the contents of your new array foreach($arr_tags as $item => $value): foreach ($value as $row => $val): $jml=$value[0]; $size=$value[1]; endforeach; echo '<a href="'.$base.'news/tag/'.$item.'" style="font-size:'.$size.'px" title="'.$jml.' items on tag '.$item.'"> '.$item.' </a>'; endforeach; ?> |
Finish.. It’s pretty simple^^

