<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: php code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Thu, 21 Aug 2008 00:52:34 GMT</pubDate>
    <description>DZone Snippets: php code</description>
    <item>
      <title>Flat-tag updater</title>
      <link>http://snippets.dzone.com/posts/show/465</link>
      <description>Pass new tags followed by a table name and database handle and this function updates the table so that each tag becomes properly associated/its record created, eg:&lt;br /&gt;&lt;br /&gt;table state:&lt;br /&gt;tagname  |  relatedtags&lt;br /&gt;-----------------------&lt;br /&gt;blog     |  rss;personal&lt;br /&gt;rss      |  blog&lt;br /&gt;personal |  blog&lt;br /&gt;&lt;br /&gt;call (table is named tags):&lt;br /&gt;updateTags("blog personal friends","tags",$db);&lt;br /&gt;&lt;br /&gt;table state:&lt;br /&gt;tagname  |  relatedtags&lt;br /&gt;-----------------------&lt;br /&gt;blog     |  rss;personal;friends&lt;br /&gt;rss      |  blog&lt;br /&gt;personal |  blog;friends&lt;br /&gt;friends  |  blog;personal&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;function updateTags($tags,$table,$db) {//category handling stuff&lt;br /&gt;   include("recordExists.php");//see http://bigbold.com/snippets/posts/show/464&lt;br /&gt;   $cats = explode(";",$tags);//parse tags into array cats&lt;br /&gt;   foreach($cats as $cat) {//loop through cats&lt;br /&gt;      $tmp = array($cat);//dummy array with cat in it&lt;br /&gt;      $tmp = array_diff($cats,$tmp);//get everything in cats that is not cat&lt;br /&gt;      if(!recordExists("tagname",$cat,$table,$db)) {//if there is no category by this name yet&lt;br /&gt;         $tmp = implode(";",$tmp);//make tmp into semicolon-separated string&lt;br /&gt;         $result = mysql_query("INSERT INTO ".$table." (tagname,relatedtags) VALUES ('$cat','$tmp')", $db) or die(mysql_error());//insert category into database&lt;br /&gt;      } else {&lt;br /&gt;         $result = mysql_query("SELECT relatedtags FROM ".$table." WHERE tagname='$cat'", $db) or die(mysql_error());//select already related tags&lt;br /&gt;         $result = mysql_fetch_array($result);//get the result row as an array&lt;br /&gt;         $result = explode(";",$result['relatedtags']);//parse relatedtags into result&lt;br /&gt;         $result = array_merge($tmp,$result);//merge the cats in this $tags (without cat, hence tmp) with the ones that were in there already&lt;br /&gt;         $result = array_unique($result);//strip duplicates&lt;br /&gt;         $result = implode(";",$result);//make result into semicolon-separated string&lt;br /&gt;         $result = mysql_query("UPDATE ".$table." SET relatedtags='$result' WHERE tagname='$cat'", $db) or die(mysql_error());//update category&lt;br /&gt;      }//end if-else !recordExists&lt;br /&gt;   }//end foreach cats&lt;br /&gt;}//end function updateTags&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 08 Jul 2005 01:50:07 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/465</guid>
      <author>singpolyma ()</author>
    </item>
    <item>
      <title>Record Exists</title>
      <link>http://snippets.dzone.com/posts/show/464</link>
      <description>In mySQL (from PHP), checks for the existance of a record based on id, passing the name of the id field followed by the id to check for, the table name, and the database resource, in that order.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;function recordExists($id,$idval,$table,$db) {//check for id=idval in table and return TRUE or FALSE&lt;br /&gt;   $result = mysql_query("SELECT * FROM ".$table." WHERE ".$id."='".$idval."'", $db) or die(mysql_error());&lt;br /&gt;   if($row = mysql_fetch_array($result)) {//if we did return a record&lt;br /&gt;      return 1;&lt;br /&gt;   }//end if row&lt;br /&gt;   return 0;&lt;br /&gt;}//end fuction recordExists&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 08 Jul 2005 01:27:38 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/464</guid>
      <author>singpolyma ()</author>
    </item>
  </channel>
</rss>
