How To Display Search Results Amongst Multiple Tags Using Codeigniter Active Record?

I was working on a tags organization inward CI and, what I require was to generate a interrogation that should entirely display posts alongside selected tags (i.e. entirely that accept the tags) equally an output. After searching many hours inward internet, I saw this amazing weblog alongside possible solutions.
http://tagging.pui.ch/post/37027745720/tags-database-schemas
Because of my database struture, i liked "Toxi Solution". The primary payoff of this solution is that it is to a greater extent than normalized than the other solutions, together with that you lot tin hand notice accept unlimited pose out of tags per post.


Since I require to entirely exhibit posts that accept the tags. So I accept to produce Intersection (AND) query.
Intersection (AND)
 Query for “bookmark+webservice+semweb”   SELECT b.* FROM tagmap bt, bookmark b, tag t WHERE bt.tag_id = t.tag_id AND (t.name IN ('bookmark', 'webservice', 'semweb')) AND b.id = bt.bookmark_id GROUP BY b.id HAVING COUNT( b.id )=3 
I accept to convert this interrogation using Codeigniter's Active record.
Steps To Display Search Results With Multiple Tags using Codeigniter activerecord are follows
1. Joined tags tabular array to posts table
2. Use where_in active record
3. Use group_by active record
4. Use having concern human relationship record
5. Use custom count arguing within having statement

Example
  $this->db->join('tagmap', 'bookmark.bookmark_id = tagmap.bookmark_id', 'left'); $this->db->join('tag', 'tag.tag_id = tagmap.tag_id', 'left');                    $a_tag = array();  //I am using static array for this example entirely (It tin hand notice hold out dynamic array from Front destination (eg. user inputs)) $filter_tag = array("bookmark", "webservice", "semweb");        $a_tag = explode(",", trim($filter_tag)); // Your tags equally an array.       $a_tag  = array_unique($a_tag);  // to accept unique array (protect duplicate array values)       $tag_count = count($a_tag); // count the pose out of elements inward the array       $this->db->where_in('tag.name', $a_tag);       $this->db->group_by('bookmark.bookmark_id');       $count = "COUNT(bookmark.bookmark_id) = $tag_count";   // Custom string        $this->db->having($count);  $query = $this->db->get('bookmark');     supply ($query->num_rows() > 0)  ? $query->result() : FALSE;      
In this means you lot tin hand notice recall posts that belong to a specific grouping of tags using Codeigniter Activerecord.

Ref: http://tagging.pui.ch/post/37027745720/tags-database-schemas

Sumber http://developer-paradize.blogspot.com

Comments

Popular posts from this blog

What Are The Main Components of a Computer System

Top Qualities To Look For In An IT Support Team

How To Integrate Google Adwords Api Into Codeigniter?