title: Working With Tags for posts in Codeigniter
body: <p>i am working on a project that gives the user the ability to write a post then tag it. at the end other user comes in and votes for the best post. </p>
  
  <p>everything was going like a charm until i faced the tagging challenge : how to work out the easiest way to deal with them ? </p>
  
  <p>i used a many-to-many relationship to assign each post a number of tags. but i found difficulties before inserting them to the database. these difficulties included: 'how can i know that this tag is not already stored in DB ?'(of course i put the name field as UNIQUE, but if somebody entered a pre stored tag he will be faced with codeigniter error which i don't want it to be shown.) .. </p>
  
  <p>So in short words, i came up with this solution : </p>
  
  <ol>
  <li>Get All Tags from the DB.</li>
  <li>Store the in array $all.</li>
  <li>Get The User-Entered Tags. </li>
  <li>Store Them in Array $user</li>
  <li>Make an intersection $existed</li>
  <li>in a foreach loop
  <ul>
  <li>Query the tags Table for the id of the current tag</li>
  <li>Insert the ID in posts_has_tags table.</li>
  </ul></li>
  <li>for the other array i would make another for each
  <ul>
  <li>Insert the tag into tags table. </li>
  <li>Get the insert_id() $id</li>
  <li>Insert the $id to the posts_has_tags table. </li>
  </ul></li>
  </ol>
  
  <p>Well, i know that it's kind of mess. especially for looping in arrays which might be a little big with time ..  so, i am asking you guys :)
  am i correct .. ? 
  is there any easier way .. ?   </p>
  
  <p>Thanks. </p>