<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: Singpolyma's Code Snippets</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sun, 27 Jul 2008 07:25:21 GMT</pubDate>
    <description>DZone Snippets: Singpolyma's Code Snippets</description>
    <item>
      <title>JavaScript var_dump (Mark 2)</title>
      <link>http://snippets.dzone.com/posts/show/4296</link>
      <description>Same as var_dump for PHP, but for JavaScript.  Useful if you do not have Firebug.&lt;br /&gt;&lt;br /&gt;A typical useage:&lt;br /&gt;&lt;br /&gt;document.write(var_dump(ANY-JS-VAR,'html'));&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;     function var_dump(data,addwhitespace,safety,level) {&lt;br /&gt;        var rtrn = '';&lt;br /&gt;        var dt,it,spaces = '';&lt;br /&gt;        if(!level) {level = 1;}&lt;br /&gt;        for(var i=0; i&lt;level; i++) {&lt;br /&gt;           spaces += '   ';&lt;br /&gt;        }//end for i&lt;level&lt;br /&gt;        if(typeof(data) != 'object') {&lt;br /&gt;           dt = data;&lt;br /&gt;           if(typeof(data) == 'string') {&lt;br /&gt;              if(addwhitespace == 'html') {&lt;br /&gt;                 dt = dt.replace(/&amp;/g,'&amp;amp;');&lt;br /&gt;                 dt = dt.replace(/&gt;/g,'&amp;gt;');&lt;br /&gt;                 dt = dt.replace(/&lt;/g,'&amp;lt;');&lt;br /&gt;              }//end if addwhitespace == html&lt;br /&gt;              dt = dt.replace(/\"/g,'\"');&lt;br /&gt;              dt = '"' + dt + '"';&lt;br /&gt;           }//end if typeof == string&lt;br /&gt;           if(typeof(data) == 'function' &amp;&amp; addwhitespace) {&lt;br /&gt;              dt = new String(dt).replace(/\n/g,"\n"+spaces);&lt;br /&gt;              if(addwhitespace == 'html') {&lt;br /&gt;                 dt = dt.replace(/&amp;/g,'&amp;amp;');&lt;br /&gt;                 dt = dt.replace(/&gt;/g,'&amp;gt;');&lt;br /&gt;                 dt = dt.replace(/&lt;/g,'&amp;lt;');&lt;br /&gt;              }//end if addwhitespace == html&lt;br /&gt;           }//end if typeof == function&lt;br /&gt;           if(typeof(data) == 'undefined') {&lt;br /&gt;              dt = 'undefined';&lt;br /&gt;           }//end if typeof == undefined&lt;br /&gt;           if(addwhitespace == 'html') {&lt;br /&gt;              if(typeof(dt) != 'string') {&lt;br /&gt;                 dt = new String(dt);&lt;br /&gt;              }//end typeof != string&lt;br /&gt;              dt = dt.replace(/ /g,"&amp;nbsp;").replace(/\n/g,"&lt;br&gt;");&lt;br /&gt;           }//end if addwhitespace == html&lt;br /&gt;           return dt;&lt;br /&gt;        }//end if typeof != object &amp;&amp; != array&lt;br /&gt;        for (var x in data) {&lt;br /&gt;           if(safety &amp;&amp; (level &gt; safety)) {&lt;br /&gt;              dt = '*RECURSION*';&lt;br /&gt;           } else {&lt;br /&gt;              try {&lt;br /&gt;                 dt = var_dump(data[x],addwhitespace,safety,level+1);&lt;br /&gt;              } catch (e) {continue;}&lt;br /&gt;           }//end if-else level &gt; safety&lt;br /&gt;           it = var_dump(x,addwhitespace,safety,level+1);&lt;br /&gt;           rtrn += it + ':' + dt + ',';&lt;br /&gt;           if(addwhitespace) {&lt;br /&gt;              rtrn += '\n'+spaces;&lt;br /&gt;           }//end if addwhitespace&lt;br /&gt;        }//end for...in&lt;br /&gt;        if(addwhitespace) {&lt;br /&gt;           rtrn = '{\n' + spaces + rtrn.substr(0,rtrn.length-(2+(level*3))) + '\n' + spaces.substr(0,spaces.length-3) + '}';&lt;br /&gt;        } else {&lt;br /&gt;           rtrn = '{' + rtrn.substr(0,rtrn.length-1) + '}';&lt;br /&gt;        }//end if-else addwhitespace&lt;br /&gt;        if(addwhitespace == 'html') {&lt;br /&gt;           rtrn = rtrn.replace(/ /g,"&amp;nbsp;").replace(/\n/g,"&lt;br&gt;");&lt;br /&gt;        }//end if addwhitespace == html&lt;br /&gt;        return rtrn;&lt;br /&gt;     }//end function var_dump&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 12 Jul 2007 15:29:58 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4296</guid>
      <author>singpolyma ()</author>
    </item>
    <item>
      <title>NP Problem Solution</title>
      <link>http://snippets.dzone.com/posts/show/4295</link>
      <description>Taken from hoodwink.d on http://xkcd.com/c287.html&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;items = ["Mixed Fruit", "French Fries", "Side Salad", "Hot Wings", "Mozzarella Sticks", "Sampler Plate"]&lt;br /&gt;prices = [2.15, 2.75, 3.35, 3.55, 4.20, 5.80]&lt;br /&gt;&lt;br /&gt;solution = []&lt;br /&gt;sum = 0&lt;br /&gt;loop do&lt;br /&gt;    break if sum == 15.05&lt;br /&gt;    if sum &gt; 15.05&lt;br /&gt;        solution = []&lt;br /&gt;        sum = 0&lt;br /&gt;        next&lt;br /&gt;    else&lt;br /&gt;        choice = rand prices.length&lt;br /&gt;        sum += prices[choice]&lt;br /&gt;        solution &lt;&lt; items[choice]&lt;br /&gt;    end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;p "A solution is: #{solution.inspect}" &lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 12 Jul 2007 15:17:04 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4295</guid>
      <author>singpolyma ()</author>
    </item>
    <item>
      <title>var_dump for javascript</title>
      <link>http://snippets.dzone.com/posts/show/759</link>
      <description>hackish implementation of the php 'var_dump()' in javascript:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;function var_dump(obj) {&lt;br /&gt;   if(typeof obj == "object") {&lt;br /&gt;      return "Type: "+typeof(obj)+((obj.constructor) ? "\nConstructor: "+obj.constructor : "")+"\nValue: " + obj;&lt;br /&gt;   } else {&lt;br /&gt;      return "Type: "+typeof(obj)+"\nValue: "+obj;&lt;br /&gt;   }&lt;br /&gt;}//end function var_dump&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 21 Sep 2005 23:31:16 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/759</guid>
      <author>singpolyma ()</author>
    </item>
    <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>
