<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>

<channel>
	<title>Codewerks</title>
	<atom:link href="http://www.metonymie.com/codewerks/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.metonymie.com/codewerks</link>
	<description>A weblog about web development, coding and meta-web issues by Emiliano MartÃ­nez Luque.</description>
	<pubDate>Mon, 23 Jun 2008 02:41:33 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5.1</generator>
	<language>en</language>
			<item>
		<title>Dom Manipulation of SVG embeded inside XHTML</title>
		<link>http://www.metonymie.com/codewerks/2008/06/21/dom-manipulation-of-svg-embeded-inside-xhtml.html</link>
		<comments>http://www.metonymie.com/codewerks/2008/06/21/dom-manipulation-of-svg-embeded-inside-xhtml.html#comments</comments>
		<pubDate>Sat, 21 Jun 2008 12:45:39 +0000</pubDate>
		<dc:creator>Emiliano Martínez Luque</dc:creator>
		
		<category><![CDATA[javascript]]></category>

		<category><![CDATA[svg]]></category>

		<category><![CDATA[dom]]></category>

		<category><![CDATA[xhtml]]></category>

		<guid isPermaLink="false">http://www.metonymie.com/codewerks/?p=15</guid>
		<description><![CDATA[Using the DOM to manipulate SVG elements inside an XHTML document. ]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s been awhile now since the <a href="http://www.w3c.org" title="World Wide Web Consortium">W3C</a> proposed the XHTML + SVG + MAthML document type declaration and a certain level of support has been available for a while. I&#8217;ve been testing XHTML + MathML lately (see <a href="http://www.metonymie.com/apuntes/2008/05/14/probabilidad-formulas-basicas.html" title="Example XHTML with embedded MathML">http://www.metonymie.com/apuntes/2008/05/14/probabilidad-formulas-basicas.html</a>). Now <a href="http://www.firefox.com" title="Firefox Web Browser">Firefox 3</a> is out, and supposedly it has support not only for embedded SVG but also for Dom manipulation the SVG nodes ( <strong>Correction</strong>: <a href="http://blog.codedread.com/">Jeff Schiller</a> has pointed out that SVG support has been available in Firefox since version 1.5 ). So this is a series of tests of the basics of working with SVG from Javascript.</p>
<h2>Getting the content to be recognized by the browser.</h2>
<p>While testing this on my machine, I found out that for the svg to be accepted inside the html I had to name the file with an <strong>.xhtml extension</strong>. Since the same document with a .html extension just wasn´t recognized by firefox (IE 7 didn´t recognize the embedded SVG one way or the other). An example: <a href="http://www.metonymie.com/projects/2008/svg-embedded-in-xhtml.xhtml">SVG embedded inline inside XHTML</a>.</p>
<p>So that was fine for local tests, but what about a dynamically generated document?.</p>
<p>Well first we need to define the type declaration, and the appropiate namespaces:</p>
<pre><code>	&lt;?xml version="1.0"?&gt;
	&lt;!DOCTYPE html PUBLIC    &#8220;-//W3C//DTD XHTML 1.1 plus MathML 2.0 plus SVG 1.1//EN&#8221;
	&#8220;http://www.w3.org/2002/04/xhtml-math-svg/xhtml-math-svg.dtd&#8221;&gt;&lt;html xmlns=&#8221;http://www.w3.org/1999/xhtml&#8221; xml:lang=&#8221;en&#8221;&gt;
&lt;html xmlns=&#8221;http://www.w3.org/1999/xhtml&#8221;
      xmlns:svg=&#8221;http://www.w3.org/2000/svg&#8221;
      xmlns:xlink=&#8221;http://www.w3.org/1999/xlink&#8221;
	  xmlns:mathml=&#8221;http://www.w3.org/1998/Math/MathML&#8221;&gt;
</code></pre>
<p><strong>Note:</strong> Declaring the namespace on the document level saves us from having to declare it in each and every instance of an SVG element. So out of laziness, I prefer to do it on the top of the document.</p>
<p>But this is not enough if we want the document to be recognized as XHTML, we need to either use the .xhtml extension or to use a header when serving the document with content type=&#8221;text/xml&#8221;. In php we can do it like this: </p>
<pre><code>	header("content-type:text/xml");
</code></pre>
<p>An example: <a href="http://www.metonymie.com/projects/2008/svg-embedded-in-xhtml.php">SVG embedded inline inside XHTML in a file with a non .xhtml extension</a>.</p>
<p>You can also set the content type to a more specific definition, for example (inside the head of the document): </p>
<pre><code>	&lt;meta http-equiv="Content-Type" content="application/xhtml+xml; charset=UTF-8"/&gt;
</code></pre>
<h2>Using SVG inside XHTML</h2>
<p>Now we&#8217;ll get into a little (just a little) more complex svg example that we&#8217;ll use for the rest of the post: <a href="http://www.metonymie.com/projects/2008/dom-manipulations-of-svg-inside-xhtml-example.php" title="Dom Manipulations of SVG Inside XHTML Examples">Dom Manipulations of SVG Inside XHTML Examples</a></p>
<h3>Defining the SVG elements</h3>
<p>First we´ll create some basic SVG elements inside our document. Basically a Rectangle and a Circle inside a div.</p>
<pre><code>	&lt;div id="svg-content"&gt;
	&lt;svg:svg height="300" width="700" is="svg-container"&gt;
	&lt;svg:rect fill="#ff5500" height="50" width="200" y="100" x="300" stroke="#000000" stroke-width="2px"/&gt;
    &lt;svg:circle cx="150px" cy="100px" r="50px" fill="#ff0000" stroke="#000000" stroke-width="5px" id="circle"/&gt;

	&lt;/svg:svg&gt;&lt;/div&gt;
</code></pre>
<h2>Accesing the SVG Objects Properties with Javascript</h2>
<h3>Getting the value of an SVG Object attribute</h3>
<p>It is as simple as retrieving the element by ID and getting the attribute as we would for any other DOM object. In the <a href="http://www.metonymie.com/projects/2008/dom-manipulations-of-svg-inside-xhtml-example.php" title="Dom Manipulations of SVG Inside XHTML Examples">example page</a>, this is the code on the first button:</p>
<pre><code>	alert( document.getElementById('circle').getAttribute('fill') );
</code></pre>
<h3>Changing the value of an SVG Object Attribute</h3>
<p>Again this is done as we would normally do with any other DOM Object.</p>
<pre><code>	document.getElementById('circle').setAttribute('fill', '#ffdd22');
</code></pre>
<h2>Creating and adding a new SVG Object</h2>
<p>This is a little more complex since we have to create a document node with the SVG Namespace defined.</p>
<pre><code>	function add_new_rectangle () {
		var atts = {"stroke-width":"1", "stroke":"blue", "fill":"yellow", "height":"20", "width":"40", "y":"100", "x":"220"};
		//Defining the SVG Namespace
		var svgNS = "http://www.w3.org/2000/svg";
		//Creating a Document by Namespace
		var node = document.createElementNS(svgNS, "rect");
		//Setting attributes by namespace
		node.setAttributeNS(null, "id", "new-rect");
		for(name in atts) {
			node.setAttributeNS(null, name, atts[name]);
		}
		var cont = document.getElementById(&#8221;svg-container&#8221;);
		//Appending the new node
		cont.appendChild(node);
	}
</code></pre>
<h2>Using innerHTML with SVG</h2>
<p>Now, a very simple way to deal with elements in HTML with the dom, is to use the innerHTML property. Luckily we can do the same with Divs containing SVG. Code for the fourth button in the example: </p>
<pre><code>	alert(document.getElementById('svg-content').innerHTML);
</code></pre>
<p>And of course we can manipulate and change it with a new SVG element declaration. The function that calls the fifth button: </p>
<pre><code>	function change_innerHTML_to_a_new_SVG() {
		var svg_str='<svg:svg height="300" width="700" version="1.1">&#8216;;
		for(var x=1; x<10; x++) {
			svg_str += '&lt;svg:circle cx="' + (x*30) + 'px" cy="100" r="' + (x*5) + 'px" fill="#ff' + (x*10) +  '00" stroke="#000000" stroke-width="3px"/&gt;';
		}
		svg_str += '&lt;/svg:svg&gt;';
		var cont = document.getElementById("svg-content");
		cont.innerHTML = svg_str;
	}
</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://www.metonymie.com/codewerks/2008/06/21/dom-manipulation-of-svg-embeded-inside-xhtml.html/feed</wfw:commentRss>
		</item>
		<item>
		<title>Javascript Global Variables Weirdness</title>
		<link>http://www.metonymie.com/codewerks/2008/05/23/javascript-global-variables-weirdness.html</link>
		<comments>http://www.metonymie.com/codewerks/2008/05/23/javascript-global-variables-weirdness.html#comments</comments>
		<pubDate>Fri, 23 May 2008 07:32:16 +0000</pubDate>
		<dc:creator>Emiliano Martínez Luque</dc:creator>
		
		<category><![CDATA[javascript]]></category>

		<category><![CDATA[javascript globals]]></category>

		<guid isPermaLink="false">http://www.metonymie.com/codewerks/?p=13</guid>
		<description><![CDATA[A bunch of test and conclusions on Javascript Global Variables.]]></description>
			<content:encoded><![CDATA[<p>This is a simple test of the use of Global variables in Javascript. Now suppose the following code writing outside of any function, class or whatever.</p>
<pre><code>
k = "k";

var t = "t";

alert("gl: " + k);

alert("gl: " + t);

</code></pre>
<p>Will alert us of both variables with those nice little javascript alerts</p>
<p>Now suppose we add this function to the code above</p>
<pre><code>
function test() {

	alert("t: " + k);

	alert("t: " + t);

	j = "J";

	var l = "l";

	alert("t: " + j);

	alert("t: " + l);

	k = "2k";

	t = "2t";

}

</code></pre>
<p>Now we run the test.</p>
<pre><code>
test();

</code></pre>
<p>which Alerts all the variables.</p>
<p>But now if we try the following:</p>
<pre><code>
alert("gl after test: " + k +" - " + "gl after test: " + t);

</code></pre>
<p>Alerts us with the following line <code>gl after test: 2k - gl after test: 2t</code>. So <strong>Globals can be modified from within functions</strong>. But what happens to the variables that were <strong>defined within the function</strong>?</p>
<pre><code>
 alert("gl: " + j);

<strong>//alert(&#8221;gl: &#8221; + l); //This won&#8217;t run, l was scoped within test and can&#8217;t be accessed outside of test.</strong>

</code></pre>
<p><strong>Conclusions: </strong>Variables initialized <strong>outside a function</strong> are <strong>global</strong> whether you use var or not. Variables initialized <strong>inside a function</strong> are <strong>not global</strong> if they are <strong>initialized with var</strong>, else they are <strong>global</strong>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.metonymie.com/codewerks/2008/05/23/javascript-global-variables-weirdness.html/feed</wfw:commentRss>
		</item>
		<item>
		<title>Using PDO in PHP 5 with MYSQL</title>
		<link>http://www.metonymie.com/codewerks/2008/05/07/using-pdo-in-php-with-mysql.html</link>
		<comments>http://www.metonymie.com/codewerks/2008/05/07/using-pdo-in-php-with-mysql.html#comments</comments>
		<pubDate>Wed, 07 May 2008 07:06:39 +0000</pubDate>
		<dc:creator>Emiliano Martínez Luque</dc:creator>
		
		<category><![CDATA[PDO]]></category>

		<category><![CDATA[php5]]></category>

		<guid isPermaLink="false">http://www.metonymie.com/codewerks/?p=12</guid>
		<description><![CDATA[A simple guide for using PDO in PHP 5 with mysql. Covers the basics, including installation, preparing and executing statements, the use of binded statements and error handling. ]]></description>
			<content:encoded><![CDATA[<h2>Installation</h2>
<p>You should first refer to the installation guide in <a href="http://www.php.net/manual/en/pdo.installation.php">PDO Installation.</a></p>
<p>In my particular case since I was intending to use this with the MySQL database, the configuration options I had to add where:<br />
<code> '--enable-pdo=shared' '--with-pdo-mysql=shared,/var/production/mysql' '--with-sqlite=shared' '--with-pdo-sqlite=shared'</code></p>
<p>And I also had to add the following lines to my php.ini file:<br />
<code> extension=pdo.so<br />
extension=pdo_mysql.so</code></p>
<h2>Testing the installation</h2>
<pre><code>
//How to get which drivers are available for PDO
print_r( PDO::getAvailableDrivers() );
</code></pre>
<p>which shows<code><br />
Array ( [0] =&gt; mysql )<br />
</code></p>
<h3>Starting a <acronym title="PHP Data Objects">PDO</acronym> Connection</h3>
<pre><code> 

	//Starting a PDO Connection
	try {
		//Please fill this in with appropriate data for your Mysql Database, User and User Password.
		$dbh = new PDO('mysql:host=localhost;dbname=test', 'test_user', 'test_user_password');
	} catch(PDOException $e) {
    	echo $e-&gt;getMessage();
	}
	print_r($dbh);
</code></pre>
<p>Which shows<code><br />
PDO Object ( )<br />
</code></p>
<p>You might get an exception there if you don set up your data ok, or if there is no driver for your DB engine.</p>
<p>For example if I try the following</p>
<pre><code>
	//Starting a PDO Connection
	try {
		//Sending the wrong Password.
		$dbh = new PDO('mysql:host=localhost;dbname=test', 'test_user', 'invalid_password');
	} catch(PDOException $e) {
    	echo $e-&gt;getMessage();
	}
</code></pre>
<p>I get <code>SQLSTATE[28000] [1045] Access denied for user &#8216;test_user&#8217;@'localhost&#8217; (using password: YES)PDO Object ( )</code>, so you <strong>should use try catch and write appropriate code for the event of the PDO connection failing</strong>.</p>
<h2>Simple <acronym title="PHP Data Objects">PDO</acronym> Operations</h2>
<p>For the purpose of this examples I&#8217;m setting up the following Table in a DB called Test, and I&#8217;m inserting some basic data.</p>
<pre><code>
 Create database test
 create database test;
 use test;
 CREATE TABLE `test_one` (
    `test_oneID` int(10) unsigned NOT NULL,
    `test_char` varchar(127) default NULL
 ) ENGINE=MyISAM DEFAULT CHARSET=latin1;

INSERT INTO `test_one` VALUES (1,'aaaa'),(2,'bbbb'),(3,'cccc'),(4,'dddd'),(5,'eeee');
</code></pre>
<h3>Executing a simple statement</h3>
<pre><code>
	//Preparing the statement
	$stm = $dbh-&gt;prepare("Select * from test_one");
	//Simple execution of the statement
	$stm-&gt;execute();
	print_r( $stm );
</code></pre>
<p>Shows <code>PDOStatement Object ( [queryString] =&gt; Select * from test_one )</code></p>
<h3>Getting the row count</h3>
<pre><code>
	//Getting row count of the statement
	$count = $stm-&gt;rowCount();
	echo("Count: " . $count . "
");
</code></pre>
<p>Shows <code>Count: 5</code></p>
<h3>Iterating through the rows of the <acronym title="PHP Data Objects">PDO</acronym> statement.</h3>
<pre><code>
	//PDO::FETCH_ASSOC means it fetches an associative array.
	while ( $row = $stm-&gt;fetch( PDO::FETCH_ASSOC ) ) {
		print_r($row);
		echo("
");
	}
</code></pre>
<p>Which gives:</p>
<pre><code>
Array ( [test_oneID] =&gt; 1 [test_char] =&gt; aaaa )
Array ( [test_oneID] =&gt; 2 [test_char] =&gt; bbbb )
Array ( [test_oneID] =&gt; 3 [test_char] =&gt; cccc )
Array ( [test_oneID] =&gt; 4 [test_char] =&gt; dddd )
Array ( [test_oneID] =&gt; 5 [test_char] =&gt; eeee )
</code></pre>
<h4>It is very important to close the cursor to free up resources.</h4>
<pre><code>
$stm-&gt;closeCursor();
</code></pre>
<h2>Fetching statements into objects</h2>
<pre><code>
	$stm = $dbh-&gt;prepare("Select * from test_one");
	//Simple execution of the statement
	$stm-&gt;execute();
	//PDO::FETCH_ASSOC means it fetches an Object.
	$stm-&gt;execute();
	while ( $row = $stm-&gt;fetch( PDO::FETCH_OBJ ) ) {
		print_r($row);
		echo("
");
	}
	$stm-&gt;closeCursor();
</code></pre>
<p>By using <strong><code>PDO::FETCH_OBJ</code></strong> we are retriving the data to a <code>stdClass Object</code>, which is PHP&#8217;s base class for Anonymous Objects</p>
<p>what we get in return then is:</p>
<pre><code>
stdClass Object ( [test_oneID] =&gt; 1 [test_char] =&gt; aaaa )
stdClass Object ( [test_oneID] =&gt; 2 [test_char] =&gt; bbbb )
stdClass Object ( [test_oneID] =&gt; 3 [test_char] =&gt; cccc )
stdClass Object ( [test_oneID] =&gt; 4 [test_char] =&gt; dddd )
stdClass Object ( [test_oneID] =&gt; 5 [test_char] =&gt; eeee )
</code></pre>
<h3>Extracting to a predefined object</h3>
<p>First we define a Mock object, and then we set the fetch mode to this object.</p>
<pre><code>
	//A Mock Object used for testing
	class Test {
		private $test_oneID;
		public $test_char;
		public $another_att ="predefined value";
		static $static_att = "An Static Attribute";
	}

	$stm = $dbh-&gt;prepare("Select * from test_one");
	$stm-&gt;execute();

	//Determining the Class to be fetched to.
	$stm-&gt;setFetchMode( PDO::FETCH_CLASS, 'Test');
	$row = $stm-&gt;fetch( PDO::FETCH_CLASS );
	print_r($row);
	$stm-&gt;closeCursor();
</code></pre>
<p>Which gives us the data of the first row inserted into an instance of our mock object:</p>
<pre><code>
Test Object
(
    [test_oneID:private] =&gt; 1
    [test_char] =&gt; aaaa
    [another_att] =&gt; predefined value
)
</code></pre>
<h4>It is important to remark that there are 2 ways to do this</h4>
<p>we could have done the same thing by changing:</p>
<p><code> $row = $stm-&gt;fetch( PDO::FETCH_CLASS );</code></p>
<p>to:</p>
<p><code> $row = $stm-&gt;fetchObject('Test');</code></p>
<h3>Fetching a set of rows to a predefined object</h3>
<p>We could have also fetched the full set of results to our mock class.</p>
<pre><code>
	$stm = $dbh-&gt;prepare("Select * from test_one");
	//For fetching a set of rows to objects.
	$stm-&gt;execute();
	while( $row = $stm-&gt;fetchObject('Test')) {
		print_r($row);
		echo("
");
	}
</code></pre>
<p>Which will give us:</p>
<pre><code>
Test Object ( [test_oneID:private] =&gt; 1 [test_char] =&gt; aaaa [another_att] =&gt; predefined value )
Test Object ( [test_oneID:private] =&gt; 2 [test_char] =&gt; bbbb [another_att] =&gt; predefined value )
Test Object ( [test_oneID:private] =&gt; 3 [test_char] =&gt; cccc [another_att] =&gt; predefined value )
Test Object ( [test_oneID:private] =&gt; 4 [test_char] =&gt; dddd [another_att] =&gt; predefined value )
Test Object ( [test_oneID:private] =&gt; 5 [test_char] =&gt; eeee [another_att] =&gt; predefined value )
</code></pre>
<h2>Binded <acronym title="PHP Data Objects">PDO</acronym> Statements</h2>
<p>One of the big advantaged of <acronym title="PHP Data Objects">PDO</acronym> is the use of Binded Statements, because:</p>
<ul>
<li>They prevent SQL Injections</li>
<li>They generally mean a huge speed increase, because of the preparation of the statement in the DB before execution.</li>
</ul>
<h4>A simple example</h4>
<pre><code>
	$stm2 = $dbh-&gt;prepare("Select * from test_one where test_oneID = :test_oneID");
	$stm2-&gt;bindValue(":test_oneID", 3);
	$stm2-&gt;execute();
	echo("
");
	$row = $stm2-&gt;fetchObject('Test');
	print_r($row);
</code></pre>
<p>Which returns:</p>
<pre><code>
Test Object ( [test_oneID:private] =&gt; 3 [test_char] =&gt; cccc [another_att] =&gt; predefined value )
</code></pre>
<h2>Dealing with statement errors</h2>
<p>Now let&#8217;s suppose the following code, where we are trying to fecth data from a non existent table:</p>
<pre><code>
	$sth = $dbh-&gt;prepare("Select * from non_existing table");
	$sth-&gt;execute();
	$row = $sth-&gt;fetch( PDO::FETCH_ASSOC );
	print_r($row);
</code></pre>
<p>Well, this code will produce no result at all. To access the <acronym title="PHP Data Objects">PDO</acronym> Statement error, we must explicitly address it.</p>
<pre><code>
	$arr = $sth-&gt;errorInfo();
	print_r($arr);
</code></pre>
<p>Which will show:</p>
<p><code>Array ( [0] =&gt; 00000 [1] =&gt; 1064 [2] =&gt; You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near &#8216;table&#8217; at line 1 )<br />
</code></p>
<h2>Setting <acronym title="PHP Data Objects">PDO</acronym> to treat <acronym title="PHP Data Objects">PDO</acronym> Statements errors as exceptions</h2>
<p>With the following line, we can set up the PDO object to treat all PDO Statements as Exceptions</p>
<pre><code>
$dbh-&gt;setAttribute(	PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
</code></pre>
<p>Now we can deal with statement errors as exceptions, for example:</p>
<pre><code>
	$dbh = new PDO('mysql:host=localhost;dbname=test', 'test_user', 'test_user_password');
	$dbh-&gt;setAttribute(	PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
	$sth = $dbh-&gt;prepare("Select * from test_onedfgsd");
	try {
		$sth-&gt;execute();
	} catch ( Exception $e) {
		var_dump($e);
	}
</code></pre>
<p>Which will give us:</p>
<pre><code>
object(PDOException)#3 (7) {
  ["message:protected"]=&gt;
  string(92) &#8220;SQLSTATE[42S02]: Base table or view not found: 1146 Table &#8216;test.test_onedfgsd&#8217; doesn&#8217;t exist&#8221;
  ["string:private"]=&gt;

  string(0) &#8220;&#8221;
  ["code:protected"]=&gt;
  string(5) &#8220;42S02&#8243;
  ["file:protected"]=&gt;
  string(36) &#8220;/home/www/ttiw/article.PDO_tests.php&#8221;
  ["line:protected"]=&gt;
  int(231)
  ["trace:private"]=&gt;
  array(1) {
    [0]=&gt;
    array(6) {
      ["file"]=&gt;

      string(36) &#8220;/home/www/ttiw/article.PDO_tests.php&#8221;
      ["line"]=&gt;
      int(231)
      ["function"]=&gt;
      string(7) &#8220;execute&#8221;
      ["class"]=&gt;
      string(12) &#8220;PDOStatement&#8221;
      ["type"]=&gt;
      string(2) &#8220;-&gt;&#8221;
      ["args"]=&gt;
      array(0) {
      }
    }
  }
  ["errorInfo"]=&gt;

  array(3) {
    [0]=&gt;
    string(5) &#8220;42S02&#8243;
    [1]=&gt;
    int(1146)
    [2]=&gt;
    string(40) &#8220;Table &#8216;test.test_onedfgsd&#8217; doesn&#8217;t exist&#8221;
  }
}</code></pre>
<p>Well that pretty much wraps up the basics of <acronym title="PHP Data Objects">PDO</acronym>. As a side note I would suggest that instead of using try-catch on every <acronym title="PHP Data Objects">PDO</acronym> Connection or <acronym title="PHP Data Objects">PDO</acronym> Statement, to better use a higher level exception handler and write some code for PDO Exceptions. For how to achieve this see <a href="http://ar.php.net/manual/en/function.set-exception-handler.php">set_exception_handler PHP Function</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.metonymie.com/codewerks/2008/05/07/using-pdo-in-php-with-mysql.html/feed</wfw:commentRss>
		</item>
		<item>
		<title>Simple &#8220;Days Iteration&#8221; Algorithm with a twist</title>
		<link>http://www.metonymie.com/codewerks/2007/01/08/simple-days-iteration-algorithm-with-a-twist.html</link>
		<comments>http://www.metonymie.com/codewerks/2007/01/08/simple-days-iteration-algorithm-with-a-twist.html#comments</comments>
		<pubDate>Mon, 08 Jan 2007 23:40:41 +0000</pubDate>
		<dc:creator>Emiliano Martínez Luque</dc:creator>
		
		<category><![CDATA[algorithms]]></category>

		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.metonymie.com/codewerks/2007/01/08/simple-days-iteration-algorithm-with-a-twist.html</guid>
		<description><![CDATA[Yet another one of this date iterations algorithms, due to the particulars of the script I was running I had to redo the date iteration to start on one date and iterate through all the days till another one. This is the new version, (that I&#8217;m blogging for not having to go through this again [...]]]></description>
			<content:encoded><![CDATA[<p>Yet another one of this date iterations algorithms, due to the particulars of the script I was running I had to redo the date iteration to start on one date and iterate through all the days till another one. This is the new version, (that I&#8217;m blogging for not having to go through this again in the future):</p>
<pre><code>

begA = 2001;
begM = 3;
begD = 1;
endA = 2007;
endM = 1;
endD = 1;

itA = begA;
itM = begM;
itD = begD;
while( ((itA!=endA) || (itM!=endM) || (itD!=endD)) ) {
	//Print
	document.write(itA + '-' + itM + '-' + itD + '');
	if(itM==12 &amp;&amp; itD==31) {
		//End of Year
		itA++;
		itM=01;
		itD=01;
	} else if(
		//End of Month
	( (itM==1 || itM==3 || itM==5 || itM==7 || itM==8 || itM==10 ) &amp;&amp; itD==31)
 	//Months with 31 days
	|| ( (itM==4 || itM==6 || itM==9 || itM==11 ) &amp;&amp; itD==30)
	//Months with 30 days
	|| (itM==2 &amp;&amp; itD==28 &amp;&amp; (itA%4!=0))
	//February not leap
	|| (itM==2 &amp;&amp; itD==29 &amp;&amp; (itA%4==0))
	//February leap
	) {
		itM++;
		itD=01;
	} else {
		itD++;
	}
}

</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://www.metonymie.com/codewerks/2007/01/08/simple-days-iteration-algorithm-with-a-twist.html/feed</wfw:commentRss>
		</item>
		<item>
		<title>Simple &#8220;Days Iteration&#8221; algorithm</title>
		<link>http://www.metonymie.com/codewerks/2006/12/22/simple-days-iteration-algorithm.html</link>
		<comments>http://www.metonymie.com/codewerks/2006/12/22/simple-days-iteration-algorithm.html#comments</comments>
		<pubDate>Sat, 23 Dec 2006 01:33:41 +0000</pubDate>
		<dc:creator>Emiliano Martínez Luque</dc:creator>
		
		<category><![CDATA[algorithms]]></category>

		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.metonymie.com/codewerks/2006/12/22/simple-days-iteration-algorithm.html</guid>
		<description><![CDATA[I had to do a script for iterating through a set of MySQL fields of type Datetime. Since I&#8217;ve run through this before, I&#8217;m blogging the litle thing for rememberance in the future. Here is the a version in Javascript.


for(var a=2001; a &#60;2007; a++) {

 for(var m=1; m&#60;13; m++) {

 	var monthdays = 31;

 	if(m==4 &#124;&#124; m==6 &#124;&#124; [...]]]></description>
			<content:encoded><![CDATA[<p>I had to do a script for iterating through a set of MySQL fields of type Datetime. Since I&#8217;ve run through this before, I&#8217;m blogging the litle thing for rememberance in the future. Here is the a version in Javascript.</p>
<pre><code>

for(var a=2001; a &lt;2007; a++) {

 for(var m=1; m&lt;13; m++) {

 	var monthdays = 31;

 	if(m==4 || m==6 || m==9 || m==11) {

 		monthdays = 30;

 	}

 i	f(m==2) {

 		if( a%4 == 0 ) {

 			monthdays = 29;

 		} else {

 			monthdays = 28;

 		}

 	}

 	for(var d=1; d&lt; (monthdays+1); d++) {

 		document.write(a + ' ' + m + ' ' + d + '&lt;br/&gt;');

 	}

 }

}

</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://www.metonymie.com/codewerks/2006/12/22/simple-days-iteration-algorithm.html/feed</wfw:commentRss>
		</item>
		<item>
		<title>Using Fulltext Search on MySQL</title>
		<link>http://www.metonymie.com/codewerks/2006/12/02/using-fulltext-search-on-mysql.html</link>
		<comments>http://www.metonymie.com/codewerks/2006/12/02/using-fulltext-search-on-mysql.html#comments</comments>
		<pubDate>Sat, 02 Dec 2006 23:34:32 +0000</pubDate>
		<dc:creator>Emiliano Martínez Luque</dc:creator>
		
		<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://www.metonymie.com/codewerks/2006/12/02/using-fulltext-search-on-mysql.html</guid>
		<description><![CDATA[Simple post on my experience with using full text search on MySQL]]></description>
			<content:encoded><![CDATA[<p>Fulltext indexes improve the performance of queries from text or long varchar fields, compared to using like or Rlike they are way faster. They also provide more intelligence to searching by matching and sorting not just by existence of a word in the field but by considering the weight of the word in that text ( my guess on the meaning of this is actual count of the word in the text divided by total words in the text). A more comprehensive explanation of MySql&#8217;s implementation of fulltext search can be accessed <a href="http://216.239.51.104/search?q=cache:cgVeMgTcR38J:www.php-kongress.de/2003/slides/database_track/golubchik_mysql_fulltext_search_2003.pdf+golubchik+mysql+full+text+search&#038;hl=es&#038;gl=ar&#038;ct=clnk&#038;cd=9">here</a> (I&#8217;m linking to google&#8217;s cache since the original presentation is not accessible anymore). There is an interesting explanation there of how the index is actually created.</p>
<h2>Basic Syntax</h2>
<p>To query a table by full text search firts the field needs to be indexed as fulltext:</p>
<pre><code>
Alter table mytable add Fulltext mytable_fieldToSearch( fieldToSearch );
</code></pre>
<p>To do a basic search the syntax is the following:</p>
<pre><code>
Select elemID from fieldToSearch where Match( fieldToSearch ) against("word1 word2") limit 10
</code></pre>
<h4>Considerations</h4>
<ul>
<li>When searching by fulltext search it&#8217;s <strong>not</strong> posible to add other conditions to the select statement.</li>
<li>It&#8217;s important to add a limit to the query</li>
<li>To further improve performance (specially when searching within multiple fulltext indexed fields) is to create an intermediate table with just the ID of what we are searching for and the different fields of text concatenated into one, and then querying that table directly.</li>
<li>It may also be important to collate the database field to a character set (I&#8217;m writing this to remember in the future):
<pre><code>
ALTER TABLE mytable DEFAULT CHARACTER SET=utf8 DEFAULT COLLATE=utf8_general_ci;
</code></pre>
</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.metonymie.com/codewerks/2006/12/02/using-fulltext-search-on-mysql.html/feed</wfw:commentRss>
		</item>
		<item>
		<title>Static (Class Definition) attributes on PHP 5</title>
		<link>http://www.metonymie.com/codewerks/2006/11/29/static-class-definition-attributes-on-php-5.html</link>
		<comments>http://www.metonymie.com/codewerks/2006/11/29/static-class-definition-attributes-on-php-5.html#comments</comments>
		<pubDate>Wed, 29 Nov 2006 23:22:11 +0000</pubDate>
		<dc:creator>Emiliano Martínez Luque</dc:creator>
		
		<category><![CDATA[oop]]></category>

		<category><![CDATA[php5]]></category>

		<guid isPermaLink="false">http://www.metonymie.com/codewerks/2006/11/29/static-class-definition-attributes-on-php-5.html</guid>
		<description><![CDATA[Some examples and explanations on how static attributes work on PHP 5.]]></description>
			<content:encoded><![CDATA[<p>Static attributes (or methods) can be considered class definition attributes (or methods). They are not accessed from a Class instance but directly from the definition of the class itself, as such they can be accessed directly using the <code>Class_Name::$attribute_name</code> syntax or within the class by the <code>Self::$attribute_name</code> syntax.</p>
<p>The following are a couple of tests of this.</p>
<h3>We create a simple class with an Static Attribute</h3>
<pre><code>
class Static_Test {
	public static $counter = 0;
	function show_counter() {
		echo self::$counter;
	}
	function increase_counter() {
		self::$counter++;
	}
}
</code></pre>
<h3>Accessing the static attribute from the class definition</h3>
<pre><code>
echo Static_Test::$counter;
</code></pre>
<p>Results: 0</p>
<h3>Accessing the static attribute within a method from an instance of the Class</h3>
<pre><code>
$obj1 = new Static_Test;
$obj1-&gt;show_counter();
</code></pre>
<p>Results: 0</p>
<h3>Modifying the attribute value</h3>
<pre><code>
Static_Test::$counter++;
$obj1-&gt;show_counter(); //1
</code></pre>
<p>Results: 1</p>
<pre><code>
// $obj1-&gt;counter++; //Does Nothing.
</code></pre>
<p>Results: nothing. Static Attributes <strong>can not</strong> be accessed directly, even when declared as public.</p>
<pre><code>
$obj1-&gt;increase_counter();
$obj1-&gt;show_counter(); //2
</code></pre>
<p>Results: 2. They can be accessed within methods if the syntax used in the method is appropiate.</p>
<h3>Accessing the static attribute within a method from another instance of the Class</h3>
<pre><code>
$obj2 = new Static_Test;
$obj2-&gt;increase_counter();

$obj1-&gt;show_counter(); //3
</code></pre>
<p>Results: 3. Changes done to a static attribute on an instance are reflected across all instances of the class.</p>
<h3>A graphical representation of what we&#8217;ve done</h3>
<p style="text-align: center"><img src="http://www.metonymie.com/projects/2006/class_definition_attributes.gif" alt="Class Definition Attributes Graph" /></p>
<h3>Accessing Static Attributes from a Child Class</h3>
<pre><code>
class Static_Child_Test extends Static_Test {
	function increase_parent_counter() {
		parent::$counter++ ;
	}
}
$obj3 = new Static_Child_Test;
$obj3-&gt;increase_parent_counter();

$obj2-&gt;show_counter(); //4
</code></pre>
<p>Results: 4.</p>
<h3>Accessing Static Methods</h3>
<p>The same holds true for static methods. An example:</p>
<pre><code>
class Static_Test2 {
	public static function static_method() {
		echo 'This is an static Function. Called directly from the Class Definition and not from an Instance of the Class.';
	}
}

Static_Test2::static_method();</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://www.metonymie.com/codewerks/2006/11/29/static-class-definition-attributes-on-php-5.html/feed</wfw:commentRss>
		</item>
		<item>
		<title>Notes on Attribute Visibility on PHP 5</title>
		<link>http://www.metonymie.com/codewerks/2006/11/28/attributes-visibility-on-php-5.html</link>
		<comments>http://www.metonymie.com/codewerks/2006/11/28/attributes-visibility-on-php-5.html#comments</comments>
		<pubDate>Tue, 28 Nov 2006 23:15:45 +0000</pubDate>
		<dc:creator>Emiliano Martínez Luque</dc:creator>
		
		<category><![CDATA[oop]]></category>

		<category><![CDATA[php5]]></category>

		<guid isPermaLink="false">http://www.metonymie.com/codewerks/2006/11/28/attributes-visibility-on-php-5.html</guid>
		<description><![CDATA[Public, Protected and Public Attributes ( and methods ) visibility explanation.]]></description>
			<content:encoded><![CDATA[<p>A couple of years ago when I was studying PHP 5 OOP, I did a couple of scripts to test the different aspects of PHP&#8217;s particular object implementation. Since I&#8217;m currently migrating a whole bunch of PHP 4 scripts to PHP 5, I&#8217;ve decided to consult my notes, which I&#8217;m presenting here, plus some explanations for extra clarity.</p>
<p>This are my notes on Attributes and Method visibility within Objects.</p>
<p>First we create a simple class with an attribute (variable) of each type of visibility:</p>
<pre><code>
class MyClass {
	public $public = 'public';
	protected $protected = 'protected';
	private $private = 'private';
	function printHello() {
		echo $this-&gt;public; echo ', ';
		echo $this-&gt;protected; echo ', ';
		echo $this-&gt;private;
	}
}</code></pre>
<h3>Accessing Attributes Directly</h3>
<pre><code>
$obj = new MyClass;
echo $obj-&gt;public; //Works
//echo $obj-&gt;protected; //Fatal Error
//echo $obj-&gt;private; //Fatal Error

$obj-&gt;printHello(); //Prints Everything.
</code></pre>
<h4>Access Conclusions</h4>
<p><strong>Public</strong> attributes can be accessed directly.</p>
<p><strong>Protected and Private</strong> can <strong>not</strong>.</p>
<h3>Accesibility in Child classes</h3>
<pre><code>
class MyClass2 extends MyClass {
	function testPublic() {
		echo $this-&gt;public; //Prints "Public".
	}
	function testProtected() {
		echo $this-&gt;protected; //Prints "Protected".
	}
	function testPrivate() {
		echo $this-&gt;private; //Prints nothing.
	}
}
$obj2 = new MyClass2;
$obj2-&gt;printHello(); //Prints everything.
$obj2-&gt;testProtected(); //Prints "Protected".
$obj2-&gt;testPrivate(); //Prints nothing.
</code></pre>
<h4>Conclusions</h4>
<p><strong>Public and Protected</strong> attributes can be accessed in child objects.</p>
<p><strong>Private</strong> can <strong>not</strong>.</p>
<h3>Redeclaring (or Overriding ) attributes in Child classes</h3>
<pre><code>
class myClass3 extends myClass {
	public $public = 'new public value';
	protected $protected = 'new protected value';
	private $private = 'new private value';
}
$obj3 = new MyClass3;
$obj3-&gt;printHello(); //Prints "new public value, new protected value, private"
</code></pre>
<h4>Conclusions</h4>
<p><strong>Public and Protected</strong> attributes can be redeclared in child objects.</p>
<p><strong>Private</strong> can <strong>not</strong>. If redeclared, the redeclaration will not take effect.</p>
<h3>Final Conclusions</h3>
<p><strong>Note</strong>: The same holds true for <strong>object methods</strong>.</p>
<p>Encapsulation is the reason behind the separation of methods and attributes by levels of visibility. When refactoring an object, Private methods and attributes can be modified freely since they are only accesible within the object and never directly in the scripts or by inheritance in another objects. Protected makes things a little more complex since even though they can not be accessed directly, they can be accessed, redeclared or overriden by child classes. Lots of care needs to be taken when refactoring Public methods or attributes.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.metonymie.com/codewerks/2006/11/28/attributes-visibility-on-php-5.html/feed</wfw:commentRss>
		</item>
		<item>
		<title>Notes on Object Assignment under PHP 5</title>
		<link>http://www.metonymie.com/codewerks/2006/11/26/object-assignment-on-php-5.html</link>
		<comments>http://www.metonymie.com/codewerks/2006/11/26/object-assignment-on-php-5.html#comments</comments>
		<pubDate>Sun, 26 Nov 2006 21:18:00 +0000</pubDate>
		<dc:creator>Emiliano Martínez Luque</dc:creator>
		
		<category><![CDATA[oop]]></category>

		<category><![CDATA[php5]]></category>

		<guid isPermaLink="false">http://www.metonymie.com/codewerks/2006/11/26/object-assignment-on-php-5.html</guid>
		<description><![CDATA[The different ways to create, assign, reference and clone objects under PHP5. Extensive explanation. ]]></description>
			<content:encoded><![CDATA[<p>This is an extended explanation of the examples on Object Assignment (example 19-5) from <a href="http://www.php.net/manual/en/language.oop5.basic.php" title="OOP5 Basics">OOP5 Basics</a> on the PHP Manual.</p>
<p>The full script can be accessed <a href="http://www.metonymie.com/projects/2006/php5_object_assignment.zip">here</a></p>
<p>First we create the simple class from the example.</p>
<pre><code>
class SimpleClass {
	//member declaration
	public $var = 'default value';
	//method declaration
	public function displayVar() {
		echo $this-&gt;var;
	}
}
</code></pre>
<h3>Creating a New Instance</h3>
<pre><code>
$instance = new SimpleClass();
</code></pre>
<p>What exactly does this do?. It Allocates an space in memory for this new object and then references the variable <code>$instance</code> to this space in memory (representing the instance of the object).</p>
<p style="text-align: center"><img src="http://www.metonymie.com/projects/2006/instance_equals_new_object.gif" alt="Instance equals new object Graph" /></p>
<h3>Assigning a Variable to an already created object</h3>
<pre><code>
$assigned = $instance;
</code></pre>
<p>References the variable <code>$assigned</code> to the object in memory referenced by <code>$instance</code>.</p>
<p style="text-align: center"><img src="http://www.metonymie.com/projects/2006/assigned_equals_instance.gif" alt="Assigned equals instance Graph" /></p>
<h3>Referencing a Variable to a variable that references an object</h3>
<pre><code>
$reference &amp;= $instance;
</code></pre>
<p>References the variable <code>$reference</code> not to the object in memory referenced by <code>$instance</code> but to the variable <code>$instance</code> itself.</p>
<p style="text-align: center"><img src="http://www.metonymie.com/projects/2006/reference_references_instance.gif" alt="Reference references instance Graph" /></p>
<h3>Cloning an object</h3>
<pre><code>
$cloned = clone $instance
</code></pre>
<p>Allocate in memory space for a new instance of the Object. Copy all the values from the instance referenced by <code>$instance</code>. References the variable <code>$cloned</code> to this new instance of the object in memory.</p>
<p style="text-align: center"><img src="http://www.metonymie.com/projects/2006/cloned_clones_instance.gif" alt="Cloned clones instance Graph" /></p>
<h3>Full Picture up to now</h3>
<p>If we dump with var_dump all of this (see <a href="http://www.metonymie.com/projects/2006/php5_object_assignment.zip">script</a>). We  get the following:</p>
<pre>Instance: object(SimpleClass)#1 (1) { ["var"]=&gt;  string(13) &#8220;default value&#8221; }
Assigned: object(SimpleClass)#1 (1) { ["var"]=&gt; string(13) &#8220;default value&#8221; }
Referenced: object(SimpleClass)#1 (1) { ["var"]=&gt; string(13) &#8220;default value&#8221; }
Cloned: object(SimpleClass)#2 (1) { ["var"]=&gt; string(13) &#8220;default value&#8221; }</pre>
<p>A good graphical representation of this is:</p>
<p style="text-align: center"><img src="http://www.metonymie.com/projects/2006/object_assignments_full-picture-1.gif" alt="PHP 5Object Assignment Script State 1" /></p>
<h3>Changing the value of a Member and then Dereferencing $Instance</h3>
<p>Now, we&#8217;ll do a couple of changes so as to make all of this even clearer.</p>
<pre><code>
//Change variable in instance (will be changed in all but cloned)
$instance-&gt;var = 'New value';

//Derefence $instance.
$instance = NULL;
</code></pre>
<p>What we have just done is change the value of the member var in Obj 1 and then derefencing $instance to obj1.</p>
<p style="text-align: center"><img src="http://www.metonymie.com/projects/2006/instance_equals_null.gif" alt="Instance equals null Graph" /></p>
<h3>Full Final Picture of the script</h3>
<p>Let&#8217;s see what happens now when we dump all of the variables (see <a href="http://www.metonymie.com/projects/2006/php5_object_assignment.zip">script</a>). Results:</p>
<pre>Instance: NULL
Reference: NULL
Assigned: object(SimpleClass)#1 (1) { ["var"]=&gt; string(9) &#8220;New value&#8221; }
Cloned: object(SimpleClass)#2 (1) { ["var"]=&gt; string(13) &#8220;default value&#8221; }</pre>
<p>A graphical representation of the final state of our script:</p>
<p style="text-align: center"><img src="http://www.metonymie.com/projects/2006/object_assignments_full-picture-2.gif" alt="PHP 5Object Assignment Script State 2" /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.metonymie.com/codewerks/2006/11/26/object-assignment-on-php-5.html/feed</wfw:commentRss>
		</item>
		<item>
		<title>hCard_xtract v0.0.2 released</title>
		<link>http://www.metonymie.com/codewerks/2006/10/16/hcard_xtract-v002-released.html</link>
		<comments>http://www.metonymie.com/codewerks/2006/10/16/hcard_xtract-v002-released.html#comments</comments>
		<pubDate>Tue, 17 Oct 2006 04:58:38 +0000</pubDate>
		<dc:creator>Emiliano Martínez Luque</dc:creator>
		
		<category><![CDATA[hCard]]></category>

		<category><![CDATA[microformats]]></category>

		<category><![CDATA[projects]]></category>

		<guid isPermaLink="false">http://www.metonymie.com/codewerks/2006/10/16/hcard_xtract-v002-released.html</guid>
		<description><![CDATA[Version 0.0.2 of my hCard parsing library.]]></description>
			<content:encoded><![CDATA[<p>I have just finished version 0.0.2 of my <a href="http://www.metonymie.com/hCard_extract/app.html">hCard Extract Application</a>. The changes on this new version are:</p>
<ul>
<li>added support for FN ORG Optimization</li>
<li>added support for mail type</li>
<li>added support for multiple nicknames and for nickname inside n</li>
<li>added support for multiple categories</li>
</ul>
<p>I&#8217;ve also decided to release the hCard Parser Library under the <a href="http://www.gnu.org/licenses/lgpl.html">LGPL</a>, source code can be accessed <a href="http://www.metonymie.com/hCard_extract/extract_hCard.rar">here</a>. This is quite probably the last release of this, since I would like to do a full production version using <a href="http://ar.php.net/manual/en/ref.tidy.php">Tidy</a> under PHP 5 (that gives an HTML parser quite similar to the one I was using).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.metonymie.com/codewerks/2006/10/16/hcard_xtract-v002-released.html/feed</wfw:commentRss>
		</item>
	</channel>
</rss>
