Current Version of Codefight CMS Available for Download:

« Version 1.7.0 »

NOTE: The code available is same as the code used for newer than this site at the time of release. Check cmsigniter.com for latest demo.

Codefight CMS is based on CodeIgniter - Open source PHP web application framework which is very easy to learn.

It would be nice to hear back some feedback. Also you can contribute with code and helping translating language files in your language.

You can use this CMS in anyway you want. You can modify as you like and use commercially for free.

 

 

Select Language.[TESTING For Next Release.]

English | नेपाली | French | German | Korean

Login | Select Language | Wed, 08 Sep 10 01:34:05 -0600

How does TAG cloud works in Codefight CMS?

2009-04-05 04:38:26Damodar Bashyal

 

How does TAG cloud works in Codefight CMS?

Admin/Modules/Pages/Controllers/Pages.php:

In admin when creating page, if tags are set split tags separated with commas. Then clean up every tag for tag-key as $tag. Then insert the tags into tag clouds table.

CODE:

1-
2-<?php
3-if(isset($pages_tags))
4-{
5-$pages_tags = split(',',$pages_tags);
6-if(is_array($pages_tags) && count($pages_tags) > 0) foreach($pages_tags as $v)
7- {
8-//clean tag
9-$tag = $this->data_model->link_clean($v);
10-
11-//add|increment tag count
12-$this->data_model->tag_cloud_add($tag, 'page', $v);
13-
14-//insert tags to tags table
15-$this->db->insert('pages_tags', array('pages_id' => $page_id, 'tags' => $tag));
16- }
17-}
18-?>
19-
Admin/Models/Data_model.php
Link Clean:
CODE:

1-
2-<?php
3-function link_clean($segment = false)
4-{
5-//remove invalid characters with spaces, trim it and replace spaces with dashes
6- return preg_replace('/\s+?/','-',trim(preg_replace('/[^a-z0-9]+/i',' ',strtolower($segment))));
7-}
8-?>
9-
Insert Tags:
CODE:

1-
2-<?php
3-function tag_cloud_add ($tag = '', $type = 'page', $title='')
4-{
5-if(!empty($tag))
6- {
7-$this->db->where(array('tag' => $tag, 'type' => $type));
8-$count = $this->db->count_all_results('tags_cloud');
9-
10-//if tag found increment count by 1 else insert as new
11-if($count)
12- {
13-$this->db->set('count', 'count+1', FALSE);
14-$this->db->where(array('tag' => $tag, 'type' => $type));
15-$this->db->update('tags_cloud');
16- }
17- else {
18-$this->db->insert('tags_cloud', array('tag' => $tag, 'type' => $type, 'title' => $title));
19- }
20- }
21-}
22-?>
23-

OK, we inserted tags into tag cloud. Now its time to get the cloud in the frontend.

Frontend/Views/Templates/Core/Blocks/Tag_cloud.php
CODE:

1-
2-<?php
3-//Get Tag Cloud
4-$cloud = $this->data_model->tag_cloud_get();
5-
6-//Shuffle the Cloud
7-shuffle($cloud);
8-
9-//Display Cloud Tags
10-foreach($cloud as $c) { ?>
11- <a class=“ <?php echo $c['class']; ?> “ href=“ <?php echo site_url('tag/' . $c['type'] . '/' . $c['tag']); ?> “ title=“Total posts for <?php echo trim($c['title']); ?> is <?php echo $c['count']; ?> “> <?phpechotrim($c['title']); ?> </a > <?php
12-}
13-?>
14-
Frontend/Models/Data_model.php
CODE:

1-
2-<?php
3-function tag_cloud_get($type=false)
4-{
5-if($type)
6-$this->db->where('type', $type);
7-$this->db->order_by('count', 'desc');
8-$this->db->limit(20);
9-$query = $this->db->get('tags_cloud');
10-
11-$tag = $query->result_array();
12-foreach($tag as $k => $v) {
13-$tag[$k]['class'] = “cloud$k“;
14- }
15- return $tag;
16-}
17-?>
18-

So, the tags are sorted by tags count in descending order and we take first 20 and display them in our tag cloud block.

There must be more better ways to do it, but it works for me now. Also, i have set anchor class as cloud1, cloud2, cloud3 etc, so we can give size, color etc to our tags as we like.

Thats it!!!

Happy Tag Clouding :)


Bookmark and Share
 

 





Javascript must be enabled to post comments!