How To Perform Multiple Tags Search Inquiry Using Php, Mysql & Codeigniter?

I working on a tag based search. I accept 3 tables tag(id,name), tagXmedia(id,tag_id,media_id), as well as media(id,...).
tagXmedia is the mapping tabular array betwixt the tag as well as media tables. This is a 1 to many relationship.
For illustration I require to live on able to search for an entry inwards the media tabular array that is associated amongst both the "home" as well as "hawaii" tags.
 SELECT media_id FROM tagXmedia WHERE tag_id IN (SELECT id FROM tag WHERE get upwards IN ('home','hawaii')) GROUP BY media_id HAVING COUNT(tag_id) = 2; 
If y'all want to accept it agree to a greater extent than than but 2 tags, y'all tin easily add together them. Just retrieve to alter the 2 inwards the HAVING clause.

Perform Multiple Tags Search using URL Parameters as well as Codeigniter
Within the enquiry string, parameters (filters) are separated past times ampersands (&), as well as multiple values per parameter are separated past times commas. For example:
http://localhost/example/sheet?tags=value1,value2&param2=value

Here is the Codeigniter active-record enquiry for performing multiple tags using comma separated values. Table construction are explained above.
    $this->db->select('tag.name, media.*');   $this->db->join('tagXmedia', 'media.id = tagXmedia.media_id', 'left');   $this->db->join('tags', 'tags.id = tagXmedia.tag_id', 'left');    $tags = explode(',',$tags);   $a_tag  = array_unique($tags);   $tag_count = count($a_tag);         $this->db->where_in('tag.name', $a_tag);        $count = "COUNT(tagXmedia.tag_id) = $tag_count ";   $this->db->group_by('media.id');         $this->db->having($count);    $query = $this->db->get('media');  
Ref: http://stackoverflow.com/a/8762620

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?