<?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"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Joey Rivera &#187; PHP</title>
	<atom:link href="http://www.joeyrivera.com/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.joeyrivera.com</link>
	<description>Blogging about PHP, Actionscript, MySQL, and other interests.</description>
	<lastBuildDate>Wed, 04 Aug 2010 14:52:07 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Automate Db Model Creation with Zend_CodeGenerator_Php_Class</title>
		<link>http://www.joeyrivera.com/2009/automate-model-creation-with-zend_codegenerator_php_class/</link>
		<comments>http://www.joeyrivera.com/2009/automate-model-creation-with-zend_codegenerator_php_class/#comments</comments>
		<pubDate>Tue, 15 Dec 2009 19:47:35 +0000</pubDate>
		<dc:creator>Joey</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[codegenerator]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[zend]]></category>
		<category><![CDATA[zend framework]]></category>
		<category><![CDATA[zend_codegenerator_php_class]]></category>
		<category><![CDATA[zend_db_table_abstract]]></category>

		<guid isPermaLink="false">http://www.joeyrivera.com/?p=462</guid>
		<description><![CDATA[I&#8217;m working on a new tool at work that will automate several processes for a few employees so they don&#8217;t have to spend too much time doing very repetitive tasks. This tool has to do a good bit of database manipulation so I&#8217;ve decided I&#8217;ll build it in PHP using Zend Framework.
I&#8217;ll be using Zend_Db_Table_Abstract [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m working on a new tool at work that will automate several processes for a few employees so they don&#8217;t have to spend too much time doing very repetitive tasks. This tool has to do a good bit of database manipulation so I&#8217;ve decided I&#8217;ll build it in PHP using Zend Framework.</p>
<p>I&#8217;ll be using <a title="Zend Db Table Docs" href="http://framework.zend.com/manual/en/zend.d.table.html">Zend_Db_Table_Abstract</a> to communicate with the db tables from my project and I&#8217;ll be creating a model for each table as well to store and manipulate data. I&#8217;ll be working with lots of tables in the database and many have lots of fields.</p>
<p>I start by opening up Zend Studio on one monitor and SQL Query Analyzer on the other and get to work. The first table I want to work with is the &#8216;Student&#8217; table. I create a new file in my project called Student.php. Place it on my models/DbTable folder and inside I simply have to declare &#8216;_name&#8217; as a protected property with the value &#8216;Student&#8217; and extend &#8216;Zend_Db_Table_Abstract&#8217;. Easy enough but now I want to create the model I will be using to convert the database data into workable objects through my mapper class.</p>
<h4>Problem</h4>
<p>I create a new file called &#8216;Student.php&#8217; and save it to my models folder. I open the file up and now I have to create a property (it&#8217;s actually an array _data with all properties defined as keys inside) for each field in the Student table&#8230; all 50 of them! I have to be careful to name each correctly as well as to not accidentally miss some field. It ends up being a time consuming process and inefficient so I start looking for a better way to accomplish this.<span id="more-465"></span></p>
<h4>Solution</h4>
<p>This is where <a title="Zend CodeGenerator Php Class" href="http://framework.zend.com/manual/en/zend.codegenerator.html">Zend_CodeGenerator_Php_Class</a> comes in. This component of Zend allows you to create php code on the fly. My theory was, instead of manually creating all these files and typing all the field names (very time consuming with lots of room for error), I could use Zend_CodeGenerator_Php to create all the content of my classes and echo it on the screen so I could copy/paste. Once I had this working, I took it to the next step and included the process of saving that content to files on my project folder. After working on this last night, everything worked like a charm and my classes are easily and quickly created, mapped directly to my database structure, by simply calling one method. No room for error either and I can run this at any time!</p>
<h4>Code</h4>
<p>I&#8217;ll explain the code below. <a title="File of code below" href="http://www.joeyrivera.com/blog_files/462/file.txt">You can download the file if you like</a>.</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw2">&lt;?php</span><br />
<span class="re0">$db</span> = Zend_Registry::<span class="me2">get</span><span class="br0">&#40;</span><span class="st0">&#8216;db&#8217;</span><span class="br0">&#41;</span>;<br />
<span class="re0">$paths</span> = Zend_Registry::<span class="me2">get</span><span class="br0">&#40;</span><span class="st0">&#8216;paths&#8217;</span><span class="br0">&#41;</span>;<br />
<span class="re0">$temp_path</span> = <span class="re0">$paths</span><span class="br0">&#91;</span><span class="st0">&#8216;temp&#8217;</span><span class="br0">&#93;</span>;</p>
<p><span class="co1">// get all tables in db</span><br />
<span class="re0">$tables</span> = <span class="re0">$db</span>-&gt;<span class="me1">listTables</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</p>
<p><span class="kw1">foreach</span><span class="br0">&#40;</span><span class="re0">$tables</span> <span class="kw1">as</span> <span class="re0">$table</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="co1">// need to remove underline first, ucwords, and then remove space</span><br />
&nbsp; &nbsp; <span class="re0">$name</span> = <a href="http://www.php.net/str_replace"><span class="kw3">str_replace</span></a><span class="br0">&#40;</span><span class="st0">&#8216; &#8216;</span>, <span class="st0">&#8221;</span>, <a href="http://www.php.net/ucwords"><span class="kw3">ucwords</span></a><span class="br0">&#40;</span><a href="http://www.php.net/str_replace"><span class="kw3">str_replace</span></a><span class="br0">&#40;</span><span class="st0">&#8216;_&#8217;</span>, <span class="st0">&#8216; &#8216;</span>, <span class="re0">$table</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; <span class="co1">// create new class generator</span><br />
&nbsp; &nbsp; <span class="re0">$class</span> = <span class="kw2">new</span> Zend_CodeGenerator_Php_Class<span class="br0">&#40;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; <span class="co1">// configure docblock</span><br />
&nbsp; &nbsp; <span class="re0">$docblock</span> = <span class="kw2">new</span> Zend_CodeGenerator_Php_Docblock<span class="br0">&#40;</span><a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8217;shortDescription&#8217;</span> =&gt; <span class="re0">$name</span> . <span class="st0">&#8216; model&#8217;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;tags&#8217;</span> =&gt; <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;name&#8217;</span> =&gt; <span class="st0">&#8216;author&#8217;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;description&#8217;</span> =&gt; <span class="st0">&#8216;Joey Rivera&#8217;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// set name and docblock</span><br />
&nbsp; &nbsp; <span class="re0">$class</span>-&gt;<span class="me1">setName</span><span class="br0">&#40;</span><span class="st0">&#8216;Ct_Model_&#8217;</span> . <span class="re0">$name</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="re0">$class</span>-&gt;<span class="me1">setDocblock</span><span class="br0">&#40;</span><span class="re0">$docblock</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; <span class="co1">// get all fields</span><br />
&nbsp; &nbsp; <span class="re0">$fields</span> = <span class="re0">$db</span>-&gt;<span class="me1">describeTable</span><span class="br0">&#40;</span><span class="re0">$table</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; <span class="co1">// want to track primary ids for table</span><br />
&nbsp; &nbsp; <span class="re0">$primary</span> = <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; <span class="co1">// add to columns each field with a default value</span><br />
&nbsp; &nbsp; <span class="re0">$columns</span> = <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="kw1">foreach</span><span class="br0">&#40;</span><span class="re0">$fields</span> <span class="kw1">as</span> <span class="re0">$field</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// if int field default to 0</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$columns</span><span class="br0">&#91;</span><span class="re0">$field</span><span class="br0">&#91;</span><span class="st0">&#8216;COLUMN_NAME&#8217;</span><span class="br0">&#93;</span><span class="br0">&#93;</span> =<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/strpos"><span class="kw3">strpos</span></a><span class="br0">&#40;</span><span class="re0">$field</span><span class="br0">&#91;</span><span class="st0">&#8216;DATA_TYPE&#8217;</span><span class="br0">&#93;</span>, <span class="st0">&#8216;int&#8217;</span><span class="br0">&#41;</span> !== <span class="kw2">false</span> ? <span class="nu0">0</span> : <span class="st0">&#8221;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// track primary field(s) for table</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span><span class="re0">$field</span><span class="br0">&#91;</span><span class="st0">&#8216;PRIMARY&#8217;</span><span class="br0">&#93;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$primary</span><span class="br0">&#91;</span><span class="br0">&#93;</span> = <span class="re0">$field</span><span class="br0">&#91;</span><span class="st0">&#8216;COLUMN_NAME&#8217;</span><span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; <span class="co1">// add data array property to class</span><br />
&nbsp; &nbsp; <span class="re0">$class</span>-&gt;<span class="me1">setProperty</span><span class="br0">&#40;</span><a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;name&#8217;</span> =&gt; <span class="st0">&#8216;_data&#8217;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;visibility&#8217;</span> =&gt; <span class="st0">&#8216;protected&#8217;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;defaultValue&#8217;</span> =&gt; <span class="re0">$columns</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;docblock&#8217;</span> =&gt; <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;tags&#8217;</span> =&gt; <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">new</span> Zend_CodeGenerator_Php_Docblock_Tag<span class="br0">&#40;</span><a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;name&#8217;</span> =&gt; <span class="st0">&#8216;var&#8217;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;description&#8217;</span> =&gt; <span class="st0">&#8216;array Maps to all fields in table&#8217;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="re0">$class</span>-&gt;<span class="me1">generate</span><span class="br0">&#40;</span><span class="br0">&#41;</span> . PHP_EOL;<br />
&nbsp; &nbsp; &nbsp; &nbsp; file_put_contents<span class="br0">&#40;</span><span class="re0">$temp_path</span> . <span class="re0">$name</span> . <span class="st0">&#8216;.php&#8217;</span>, <span class="st0">&#8216;&lt;?php&#8217;</span> . PHP_EOL . <span class="re0">$class</span>-&gt;<span class="me1">generate</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// create zend_db_table_abstract</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$db_class</span> = <span class="kw2">new</span> Zend_CodeGenerator_Php_Class<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$db_class</span>-&gt;<span class="me1">setName</span><span class="br0">&#40;</span><span class="st0">&#8216;Ct_Model_DbTable_&#8217;</span> . <span class="re0">$name</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$db_class</span>-&gt;<span class="me1">setDocblock</span><span class="br0">&#40;</span><span class="kw2">new</span> Zend_CodeGenerator_Php_Docblock<span class="br0">&#40;</span><a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8217;shortDescription&#8217;</span> =&gt; <span class="re0">$name</span> . <span class="st0">&#8216; db table abstract&#8217;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;tags&#8217;</span> =&gt; <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;name&#8217;</span> =&gt; <span class="st0">&#8216;author&#8217;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;description&#8217;</span> =&gt; <span class="st0">&#8216;Joey Rivera&#8217;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$db_class</span>-&gt;<span class="me1">setExtendedClass</span><span class="br0">&#40;</span><span class="st0">&#8216;Zend_Db_Table_Abstract&#8217;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$db_class</span>-&gt;<span class="me1">setProperty</span><span class="br0">&#40;</span><a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;name&#8217;</span> =&gt; <span class="st0">&#8216;_name&#8217;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;visibility&#8217;</span> =&gt; <span class="st0">&#8216;protected&#8217;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;defaultValue&#8217;</span> =&gt; <span class="re0">$table</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;docblock&#8217;</span> =&gt; <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;tags&#8217;</span> =&gt; <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">new</span> Zend_CodeGenerator_Php_Docblock_Tag<span class="br0">&#40;</span><a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;name&#8217;</span> =&gt; <span class="st0">&#8216;var&#8217;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;description&#8217;</span> =&gt; <span class="st0">&#8217;string Name of db table&#8217;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span><a href="http://www.php.net/count"><span class="kw3">count</span></a><span class="br0">&#40;</span><span class="re0">$primary</span><span class="br0">&#41;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$db_class</span>-&gt;<span class="me1">setProperty</span><span class="br0">&#40;</span><a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;name&#8217;</span> =&gt; <span class="st0">&#8216;_primary&#8217;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;visibility&#8217;</span> =&gt; <span class="st0">&#8216;protected&#8217;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;defaultValue&#8217;</span> =&gt; <a href="http://www.php.net/count"><span class="kw3">count</span></a><span class="br0">&#40;</span><span class="re0">$primary</span><span class="br0">&#41;</span> &gt; <span class="nu0">1</span> ? <span class="re0">$primary</span> : <span class="re0">$primary</span><span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;docblock&#8217;</span> =&gt; <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;tags&#8217;</span> =&gt; <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">new</span> Zend_CodeGenerator_Php_Docblock_Tag<span class="br0">&#40;</span><a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;name&#8217;</span> =&gt; <span class="st0">&#8216;var&#8217;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;description&#8217;</span> =&gt; <span class="st0">&#8217;string or array of fields in table&#8217;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="re0">$db_class</span>-&gt;<span class="me1">generate</span><span class="br0">&#40;</span><span class="br0">&#41;</span> . PHP_EOL;<br />
&nbsp; &nbsp; &nbsp; &nbsp; file_put_contents<span class="br0">&#40;</span><span class="re0">$temp_path</span> . <span class="st0">&#8216;DbTable/&#8217;</span> . <span class="re0">$name</span> . <span class="st0">&#8216;.php&#8217;</span>, <span class="st0">&#8216;&lt;?php&#8217;</span> . PHP_EOL . <span class="re0">$db_class</span>-&gt;<span class="me1">generate</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></div>
<p>I start out by getting some needed variables such as my $db adapter and paths that I will use to store my files in. For this example I am using MySQL (5.1.x) database and Zend Framework 1.9.5. If you are not familiar on how to setup your database adapter <a title="Zend Db Adapter Doc" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://framework.zend.com/manual/en/zend.db.html#zend.db.adapter.connecting.constructor');" href="http://framework.zend.com/manual/en/zend.db.html#zend.db.adapter.connecting.constructor">here is the documentation</a>.</p>
<p>The next step is to call listTables on the db adapter to get an array of all the tables in the database. We want to loop through the array and create two files for each table: an object model and a db table abstract. Each file has a corresponding instance of Zend_CodeGenerator_Php_Class. The first one we create in the code is for the object model. The docblock is the optional comments for the class if you want them. setName is the name you want for the class. In this case I&#8217;m using &#8216;Ct_Model_$name&#8217; where name is the name of the table without any underscores and with the first letter of each word in uppercase. For example, if the table name is: &#8216;user_info&#8217;, name is &#8216;UserInfo&#8217;.</p>
<p>The next information I need is all the fields for that table so I can add them to my data array for each model. Calling describeTable(&#8216;tableName&#8217;) returns that. Now that I have my array with all the fields information for this first table, so I loop through to:</p>
<ul>
<li>assign names and default value(s) of 0 for an int field type or null for anything else into the columns array.</li>
<li>track the primary key(s) for each table. Some tables can have more than one.</li>
</ul>
<p>setProperty is where we create the protected data property array for this class. The name of the property is &#8216;_data&#8217; and the default value is the array we just created &#8216;columns&#8217;. You can also add a docblock to the property which I did here. Since there is no Zend_CodeGenerator_Php_Docblock_Tag_Property I just used the default Tag class passing it name &#8216;var&#8217;. Had I used Zend_CodeGenerator_Php_Docblock_Tag_Param, my comments would have shown &#8216;@param&#8217; when it is really &#8216;@var&#8217;. Try it and you&#8217;ll see what I mean.</p>
<p>Our first class for the model is now complete! I&#8217;m echoing it to the screen so I can see it when I view-source or just use Zend_Debug::dump() instead to see it formatted. The next line saves the newly generated code to a file at a temp_path location. It&#8217;s important to add &#8216;&lt;?php&#8217; before class-&gt;generate() so your file have the php tag in them.</p>
<p>The next part of the code creates the second class that extends <a title="Zend Db Table Abstract Doc" href="http://framework.zend.com/manual/en/zend.db.table.html">Zend_Db_Table_Abstract</a>. I won&#8217;t go into detail with what is happening there since it&#8217;s very similar to the above. If you have any questions do feel free to ask. That&#8217;s pretty much it. Run this code, make sure you have your directories created and after executing you should now have two files for each table in your database.</p>
<p>Here is an example of what you should see after running the page:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="coMULTI">/**<br />
&nbsp;* Bio model<br />
&nbsp;*<br />
&nbsp;* @author Joey Rivera<br />
&nbsp;*/</span><br />
<span class="kw2">class</span> Ct_Model_Bio<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="coMULTI">/**<br />
&nbsp; &nbsp; &nbsp;* @var array Maps to all fields in table<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; protected <span class="re0">$_data</span> = <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;bio_id&#8217;</span> =&gt; <span class="nu0">0</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;user_id&#8217;</span> =&gt; <span class="nu0">0</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;bio_type_id&#8217;</span> =&gt; <span class="nu0">0</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;title&#8217;</span> =&gt; <span class="kw2">null</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;content&#8217;</span> =&gt; <span class="kw2">null</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;last_update&#8217;</span> =&gt; <span class="kw2">null</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;enabled&#8217;</span> =&gt; <span class="nu0">0</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><br />
<span class="coMULTI">/**<br />
&nbsp;* Bio db table abstract<br />
&nbsp;*<br />
&nbsp;* @author Joey Rivera<br />
&nbsp;*/</span><br />
<span class="kw2">class</span> Ct_Model_DbTable_Bio <span class="kw2">extends</span> Zend_Db_Table_Abstract<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="coMULTI">/**<br />
&nbsp; &nbsp; &nbsp;* @var string Name of db table<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; protected <span class="re0">$_name</span> = <span class="st0">&#8216;bio&#8217;</span>;<br />
&nbsp; &nbsp; <span class="coMULTI">/**<br />
&nbsp; &nbsp; &nbsp;* @var string or array of fields in table<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; protected <span class="re0">$_primary</span> = <span class="st0">&#8216;bio_id&#8217;</span>;<br />
<span class="br0">&#125;</span></div>
<p>This is a work in progress since I&#8217;m still trying different things. After I finished writing this code I went back to the zend docs and noticed there is <a title="Zend CodeGenerator Php File Doc" href="http://framework.zend.com/manual/en/zend.codegenerator.examples.html#zend.codegenerator.examples.file">Zend_CodeGenerator_Php_File</a>. Seems to do the same thing I did above with file_put_contents. Feel free to try that instead.</p>
<h4>Conclusion</h4>
<p>I really enjoy the flexibility of using this method instead of typing everything out myself. It&#8217;s quicker with a lot less room for error. I&#8217;m sure there are already other programs/apps out there that do this but hey, it&#8217;s more fun to reinvent the wheel! Plus I learned about another Zend Framework module that I had not used before.</p>
<p>EDIT:</p>
<p>I had to make to small edits to this post to work more efficiently. The first was to change the line where we set the default value of each field for the columns array to &#8221; instead of null. The second was to set the primary property only if there was a primary field found on that table since not every table will always have one.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.joeyrivera.com/2009/automate-model-creation-with-zend_codegenerator_php_class/feed/</wfw:commentRss>
		<slash:comments>21</slash:comments>
		</item>
		<item>
		<title>Caching using PHP/Zend_Cache and MySQL</title>
		<link>http://www.joeyrivera.com/2009/caching-using-phpzend_cache-and-mysql/</link>
		<comments>http://www.joeyrivera.com/2009/caching-using-phpzend_cache-and-mysql/#comments</comments>
		<pubDate>Fri, 06 Nov 2009 19:25:06 +0000</pubDate>
		<dc:creator>Joey</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[cache]]></category>
		<category><![CDATA[caching]]></category>
		<category><![CDATA[zend]]></category>
		<category><![CDATA[zend framework]]></category>
		<category><![CDATA[zend_cache]]></category>
		<category><![CDATA[zend_cache_backend_file]]></category>
		<category><![CDATA[zend_cache_frontend_core]]></category>
		<category><![CDATA[zend_cache_frontend_page]]></category>
		<category><![CDATA[zf]]></category>

		<guid isPermaLink="false">http://www.joeyrivera.com/?p=443</guid>
		<description><![CDATA[I like the definition used in Wikipedia: &#8220;a cache is a temporary storage area where often accessed data can be stored for quick access&#8221;. The idea is to get &#8216;often accessed data&#8217; from a database and store it in memory (RAM or as a file in your local file system). This is because:

it&#8217;s quicker for a [...]]]></description>
			<content:encoded><![CDATA[<p>I like the definition used in <a href="http://simple.wikipedia.org/wiki/Cache">Wikipedia</a>: &#8220;a cache is a temporary storage area where often accessed data can be stored for quick access&#8221;. The idea is to get &#8216;often accessed data&#8217; from a database and store it in memory (RAM or as a file in your local file system). This is because:</p>
<ul>
<li>it&#8217;s quicker for a machine to read from memory than to connect to a database and query data.</li>
<li>it&#8217;s more efficient for the database to not waste time and resources returning the same dataset multiple times when it could be focusing on other tasks.</li>
</ul>
<p>As long as the data, in this scenario from the database, doesn&#8217;t change, there is no need to query it again.</p>
<p>Resources are limited on systems and to take advantage of your resources, you need to make sure time isn&#8217;t spent on tasks that could be handled better elsewhere. Here is a silly real world example. Imagine on a daily basis, I have to track how many magazines I have and send this information to Person X. I get new magazines at the beginning of each month only. To track the number of magazines I have every day I could</p>
<ol type="A">
<li>Count them, one by one every day and send Person X the total. If I have 50 magazines this could take some time and assume I get 10 more every month, after a year or two I could spend all day just counting how many magazines I have instead of working. Sound productive?</li>
<li>Count them once and write the number down on a piece of paper (caching!). Everyday when Person X asks how many magazines I have, I read the number from the piece of paper. Only when I get new magazines (once a month) do I count them again (or just add the current number + the new amount) to get my new total. Then I update my piece of paper with the new total (updating the value in cache).</li>
</ol>
<p>The latter is definitely the more productive choice.</p>
<p>The same idea applies to computer systems. In the web, you have static and dynamic files. Static files are quicker to serve on a server because the server only has to read the contents of the file and send it to the browser requesting it. Dynamic pages take more time and resources because the server needs to execute the code in the page and only once it&#8217;s done can it send the request back. PHP can be used to create dynamic pages. The server executes the php code and spits out a file that then is read by the browser. If a database is involved, then the database has to run it&#8217;s task as well before the final file is returned.</p>
<p>When ever possible, it&#8217;s more efficient to serve a static file or static content. We use cache to accomplish this. In this post I&#8217;m going to talk about caching files and database queries to local files on the server.<span id="more-443"></span></p>
<h4>Zend_Cache</h4>
<p>There are different ways to achieve this. I personally use <a href="http://framework.zend.com/manual/en/">Zend Framework</a> on my projects so I&#8217;ll be using <a href="http://framework.zend.com/manual/en/zend.cache.html">Zend_Cache</a> in my examples. I will only be using Zend_Cache as a standalone module, not the entire framework. This way, those of you who don&#8217;t use Zend Framework can still follow this guide. There are other options if you don&#8217;t have Zend such as <a href="http://pear.php.net/manual/en/package.caching.cache-lite.intro.php">Cache_Lite</a> which is part of the <a href="http://pear.php.net/">PEAR Framework</a>. Both work very similarly.</p>
<p>Zend_Cache is very flexible in that it lets you decide what you want to cache (the frontend) and where you want to put it (the backend). The different frontends for Zend_Cache include (taken from the <a href="http://framework.zend.com/manual/en/zend.cache.frontends.html#zend.cache.frontends.core">Zend docs</a>):</p>
<ul>
<li><strong>Zend_Cache_Core</strong> is a special frontend because it is the core of the module. It is a generic cache frontend and is extended by other classes.</li>
<li><strong>Zend_Cache_Frontend_Output</strong> is an output-capturing frontend. It utilizes output buffering in PHP to capture everything between its start() and end() methods.</li>
<li><strong>Zend_Cache_Frontend_Function</strong> caches the results of function calls. It has a single main method named call() which takes a function name and parameters for the call in an array.</li>
<li><strong>Zend_Cache_Frontend_Class</strong> is different from Zend_Cache_Frontend_Function because it allows caching of object and static method calls.</li>
<li><strong>Zend_Cache_Frontend_File</strong> is a frontend driven by the modification time of a &#8220;master file&#8221;. It&#8217;s really interesting for examples in configuration or templates issues. It&#8217;s also possible to use multiple master files.</li>
<li><strong>Zend_Cache_Frontend_Page</strong> is like Zend_Cache_Frontend_Output but designed for a complete page. It&#8217;s impossible to useZend_Cache_Frontend_Page&lt; for caching only a single block.</li>
</ul>
<p>The backends include:</p>
<ul>
<li><strong>Zend_Cache_Backend_File</strong> &#8211; This (extended) backends stores cache records into files (in a choosen directory).</li>
<li><strong>Zend_Cache_Backend_Sqlite</strong> &#8211; This (extended) backends stores cache records into a SQLite database.</li>
<li><strong>Zend_Cache_Backend_Memcached</strong> &#8211; This (extended) backends stores cache records into a memcached server. memcached is a high-performance, distributed memory object caching system. To use this backend, you need a memcached daemon and the memcache PECL extension.</li>
<li><strong>Zend_Cache_Backend_Apc</strong> &#8211; This (extended) backends stores cache records in shared memory through the APC (Alternative PHP Cache) extension (which is of course need for using this backend).</li>
<li><strong>Zend_Cache_Backend_Xcache</strong> &#8211; This backends stores cache records in shared memory through the XCache extension (which is of course need for using this backend).</li>
<li>and a couple more you can check in the Zend docs.</li>
</ul>
<p>For my first example I&#8217;ll be using the &#8216;Core&#8217; frontend (to cache a variable) and the &#8216;File&#8217; backend (to save that variable to a file on the server). I will actually be using the &#8216;File&#8217; backend on all my examples since I have not had the opportunity to work with and of the other backend methods. Reading from RAM is quicker than reading from the file system so using other backend methods which take advantage of this would yield better results.</p>
<h4>Setting up the Environment</h4>
<p>Before I go into the first example, let me explain how I set up my environment. Like i mentioned earlier, I won&#8217;t be using the Zend Framework, instead I will only be using Zend_Cache as a standalone module. To accomplish this, I create a library folder in my site root. Then I create a Zend folder inside of library where I will be putting the Cache module in. The next step requires that you download the <a href="http://framework.zend.com/download/current/">Zend Framework zip file</a> so you can copy the cache module found in the zip (minimal package is all you need). Once you download the file, open it and copy Cache.php from /library/Zend/Cache.php and the /library/Zend/Cache folder to your Zend folder in library. Here is an image of what your structure should look like:</p>
<div class="wp-caption aligncenter" style="width: 190px"><img title="Folder Structure" src="http://farm3.static.flickr.com/2802/4078156497_c2b13c294d_m.jpg" alt="Folder Structure" width="180" height="125" /><p class="wp-caption-text">Folder Structure</p></div>
<h4>Example 1 &#8211; Caching a Variable</h4>
<p>The first example is a slightly modified version of the example given on the Zend_Cache docs:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw2">&lt;?php</span><br />
<span class="kw1">include</span> <span class="st0">&#8216;library/Zend/Cache.php&#8217;</span>;</p>
<p><span class="re0">$frontendOptions</span> = <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><br />
&nbsp; &nbsp;<span class="st0">&#8216;lifetime&#8217;</span> =&gt; <span class="nu0">10</span>,<br />
&nbsp; &nbsp;<span class="st0">&#8216;automatic_serialization&#8217;</span> =&gt; <span class="kw2">true</span><br />
<span class="br0">&#41;</span>;</p>
<p><span class="re0">$backendOptions</span> = <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><br />
&nbsp; &nbsp; <span class="st0">&#8216;cache_dir&#8217;</span> =&gt; <span class="st0">&#8216;tmp/&#8217;</span><br />
<span class="br0">&#41;</span>;</p>
<p><span class="re0">$cache</span> = Zend_Cache::<span class="me2">factory</span><span class="br0">&#40;</span><span class="st0">&#8216;Core&#8217;</span>, <span class="st0">&#8216;File&#8217;</span>, <span class="re0">$frontendOptions</span>, <span class="re0">$backendOptions</span><span class="br0">&#41;</span>;<br />
<span class="re0">$id</span> = <span class="st0">&#8216;myBigLoop&#8217;</span>;</p>
<p><span class="re0">$start_time</span> = <a href="http://www.php.net/microtime"><span class="kw3">microtime</span></a><span class="br0">&#40;</span><span class="kw2">true</span><span class="br0">&#41;</span>;</p>
<p><span class="kw1">if</span><span class="br0">&#40;</span>!<span class="br0">&#40;</span><span class="re0">$data</span> = <span class="re0">$cache</span>-&gt;<span class="me1">load</span><span class="br0">&#40;</span><span class="re0">$id</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="st0">&quot;Not found in Cache&lt;br /&gt;&quot;</span>;</p>
<p>&nbsp; &nbsp; <span class="re0">$data</span> = <span class="st0">&#8221;</span>;<br />
&nbsp; &nbsp; <span class="kw1">for</span> <span class="br0">&#40;</span><span class="re0">$i</span> = <span class="nu0">0</span>; <span class="re0">$i</span> &lt; <span class="nu0">1000000</span>; <span class="re0">$i</span>++<span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$data</span> = <span class="re0">$data</span> . <span class="re0">$i</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="re0">$cache</span>-&gt;<span class="me1">save</span><span class="br0">&#40;</span><span class="re0">$data</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><br />
<span class="kw1">else</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="st0">&quot;Running from Cache&lt;br /&gt;&quot;</span>;<br />
<span class="br0">&#125;</span></p>
<p><a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <a href="http://www.php.net/sprintf"><span class="kw3">sprintf</span></a><span class="br0">&#40;</span><span class="st0">&#8216;%01.4f&#8217;</span>, <a href="http://www.php.net/microtime"><span class="kw3">microtime</span></a><span class="br0">&#40;</span><span class="kw2">true</span><span class="br0">&#41;</span> &#8211; <span class="re0">$start_time</span><span class="br0">&#41;</span>;</div>
<p>What&#8217;s going on? First I include Zend_Cache. Next I declare two arrays with configuration values needed to use Zend_Cache. The frontendOptions array is setting a cache lifetime of 10 seconds. Meaning, after a cache file is created, it will only live for 10 seconds. After that, the cache file will be recreated. In the backendOptions array, I set the folder where I want my cache files to be saved to. I&#8217;m using a folder called &#8216;tmp&#8217; that I created in the root of my site. Make sure to create that folder or your code may not work.</p>
<p>Next I create my $cache variable telling Zend_Cache that I want to use &#8216;Core&#8217; as the frontend and &#8216;File&#8217; as the backend. $id is just any name you want to give to this particular cached value. If you wanted to cache two different variables, you would want to use two different id&#8217;s for each to not overwrite one-another. $start_time is going to track when my code started running so I can check how long it took to execute my loop at the end.</p>
<p>This is where it gets fun yet so simple. The if statement checks the following</p>
<ul>
<li>$cache-&gt;load($id) will check to see if a valid cache file exists for that $id and return it.</li>
<li>$data is set to the return of $cache-&gt;load (whether there is or isn&#8217;t anything there)</li>
<li>Finally the if checks to see if there is NO data in $data and if so, processes the loop else echos &#8216;Running from Cache&#8217;</li>
</ul>
<p>If the if statement determines there is NO data in cache, it will continue with the code to do a loop. The for statement will loop 1,000,000 &#8211; one million times, and append each number to the variable $data. After it is done running a million times, it saves the variable $data into cache using the $id declared in the $cache-&gt;load() call. When it&#8217;s done the code spits out the time it took to execute this code. In my server it&#8217;s usually around 0.4 seconds:</p>
<div class="wp-caption aligncenter" style="width: 143px"><img title="Without Cache" src="http://farm3.static.flickr.com/2662/4078911942_d75a502d73_m.jpg" alt="Without Cache" width="133" height="45" /><p class="wp-caption-text">in seconds</p></div>
<p>Now if it run my code again, hitting refresh, the code will go to the if() and find there is a cache file for that $id and load it &#8211; so it will not run the for loop. In this scenario, the page usually only takes about .02 to .03 seconds to execute:</p>
<div class="wp-caption aligncenter" style="width: 143px"><img title="Using Cache" src="http://farm3.static.flickr.com/2563/4078156469_be20770d09_m.jpg" alt="Using Cache" width="133" height="45" /><p class="wp-caption-text">in seconds</p></div>
<p>That&#8217;s a nice improvement. For the next 10 seconds (while the cache file is valid since we set its lifetime to 10 seconds) the page will only take 0.03 seconds to run instead of 0.4 seconds. That&#8217;s over 15 times (94%) faster! &#8220;Rudimentary, this is a difference of serving 149 requests per minute versus 2307 requests per minute.&#8221; You can always look inside your tmp folder and see if files are being created there to make sure things are really working. If you delete them, next time you execute the page it should recreate the cache files.</p>
<h4>Example 2 &#8211; Caching a Database RecordSet</h4>
<p>This example will be extremely similar to the previous one. In both, we are setting a variable to hold a value, then we save that variable in cache. Each time we run the page we check for the cache file, if it exists we use it, else we query the database, get the recordset, and store it in cache.</p>
<h4>Setting up the Database</h4>
<p>In this example we will be using the same table &#8216;users&#8217; that I used in some of my previous posts. Below is the create statement to create the table &#8216;users&#8217; in the database &#8216;test&#8217;:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw1">DROP TABLE</span> IF <span class="kw1">EXISTS</span> `test`.`users`;<br />
<span class="kw1">CREATE TABLE</span> &nbsp;`test`.`users` <span class="br0">&#40;</span><br />
&nbsp; `users_id` <span class="kw2">INT</span><span class="br0">&#40;</span><span class="nu0">10</span><span class="br0">&#41;</span> <span class="kw3">NOT NULL</span> <span class="kw3">AUTO_INCREMENT</span>,<br />
&nbsp; `first_name` <span class="kw2">VARCHAR</span><span class="br0">&#40;</span><span class="nu0">100</span><span class="br0">&#41;</span> <span class="kw3">NOT NULL</span>,<br />
&nbsp; `last_name` <span class="kw2">VARCHAR</span><span class="br0">&#40;</span><span class="nu0">100</span><span class="br0">&#41;</span> <span class="kw3">NOT NULL</span>,<br />
&nbsp; <span class="kw1">PRIMARY KEY</span> <span class="br0">&#40;</span>`users_id`<span class="br0">&#41;</span><br />
<span class="br0">&#41;</span> ENGINE=<span class="kw1">INNODB</span> <span class="kw3">DEFAULT</span> <span class="kw3">CHARSET</span>=latin1;</div>
<p>Once you create the table insert the following data:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw1">INSERT</span> <span class="kw1">INTO</span> users<br />
<span class="kw1">VALUES</span> <span class="br0">&#40;</span><span class="kw3">NULL</span>, <span class="st0">&#8216;Joey&#8217;</span>, <span class="st0">&#8216;Rivera&#8217;</span><span class="br0">&#41;</span>, <span class="br0">&#40;</span><span class="kw3">NULL</span>, <span class="st0">&#8216;John&#8217;</span>, <span class="st0">&#8216;Doe&#8217;</span><span class="br0">&#41;</span>, <span class="br0">&#40;</span><span class="kw3">NULL</span>, <span class="st0">&#8216;Joey&#8217;</span>, <span class="st0">&#8216;Tester&#8217;</span><span class="br0">&#41;</span>,<br />
<span class="br0">&#40;</span><span class="kw3">NULL</span>, <span class="st0">&#8216;Joey&#8217;</span>, <span class="st0">&#8216;Test&#8217;</span><span class="br0">&#41;</span>, <span class="br0">&#40;</span><span class="kw3">NULL</span>, <span class="st0">&#8216;Billy&#8217;</span>, <span class="st0">&#8216;Bob&#8217;</span><span class="br0">&#41;</span>;</div>
<p>Ok, now the code. The objective will be to grab all the information for all the users in the table from the database. Once we have it, store it in cache until that table is updated and we need to query it again. We are going to use a null value for lifetime so that the cache file never expires and we are then going to manually &#8211; in code, delete the cache file when we know the table has been changed.</p>
<p>This is what the code looks like:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw2">&lt;?php</span><br />
<span class="kw1">include</span> <span class="st0">&#8216;library/Zend/Cache.php&#8217;</span>;</p>
<p><span class="re0">$frontendOptions</span> = <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><br />
&nbsp; &nbsp;<span class="st0">&#8216;lifetime&#8217;</span> =&gt; <span class="kw2">null</span>,<br />
&nbsp; &nbsp;<span class="st0">&#8216;automatic_serialization&#8217;</span> =&gt; <span class="kw2">true</span><br />
<span class="br0">&#41;</span>;</p>
<p><span class="re0">$backendOptions</span> = <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><br />
&nbsp; &nbsp; <span class="st0">&#8216;cache_dir&#8217;</span> =&gt; <span class="st0">&#8216;tmp/&#8217;</span><br />
<span class="br0">&#41;</span>;</p>
<p><span class="re0">$cache</span> = Zend_Cache::<span class="me2">factory</span><span class="br0">&#40;</span><span class="st0">&#8216;Core&#8217;</span>, <span class="st0">&#8216;File&#8217;</span>, <span class="re0">$frontendOptions</span>, <span class="re0">$backendOptions</span><span class="br0">&#41;</span>;<br />
<span class="re0">$id</span> = <span class="st0">&#8216;rs&#8217;</span>;</p>
<p><span class="re0">$start_time</span> = <a href="http://www.php.net/microtime"><span class="kw3">microtime</span></a><span class="br0">&#40;</span><span class="kw2">true</span><span class="br0">&#41;</span>;</p>
<p><span class="kw1">if</span><span class="br0">&#40;</span>!<span class="br0">&#40;</span><span class="re0">$data</span> = <span class="re0">$cache</span>-&gt;<span class="me1">load</span><span class="br0">&#40;</span><span class="re0">$id</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="st0">&quot;Not found in Cache&lt;br /&gt;&quot;</span>;</p>
<p>&nbsp; &nbsp; <a href="http://www.php.net/mysql_connect"><span class="kw3">mysql_connect</span></a><span class="br0">&#40;</span><span class="st0">&#8216;localhost&#8217;</span>, <span class="st0">&#8216;user&#8217;</span>, <span class="st0">&#8216;password&#8217;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <a href="http://www.php.net/mysql_select_db"><span class="kw3">mysql_select_db</span></a><span class="br0">&#40;</span><span class="st0">&#8216;test&#8217;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="re0">$query</span> = <span class="st0">&#8217;select * from users&#8217;</span>;<br />
&nbsp; &nbsp; <span class="re0">$rs</span> = <a href="http://www.php.net/mysql_query"><span class="kw3">mysql_query</span></a><span class="br0">&#40;</span><span class="re0">$query</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; <span class="re0">$data</span> = <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="kw1">while</span><span class="br0">&#40;</span><span class="re0">$row</span> = <a href="http://www.php.net/mysql_fetch_assoc"><span class="kw3">mysql_fetch_assoc</span></a><span class="br0">&#40;</span><span class="re0">$rs</span><span class="br0">&#41;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$data</span><span class="br0">&#91;</span><span class="br0">&#93;</span> = <span class="re0">$row</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; <span class="re0">$cache</span>-&gt;<span class="me1">save</span><span class="br0">&#40;</span><span class="re0">$data</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><br />
<span class="kw1">else</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="st0">&quot;Running from Cache&lt;br /&gt;&quot;</span>;<br />
<span class="br0">&#125;</span></p>
<p><span class="co1">//echo &#8216;&lt;pre&gt;&#8217;;</span><br />
<span class="co1">//print_r($data);</span><br />
<span class="co1">//echo &#8216;&lt;/pre&gt;&#8217;;</span><br />
<a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <a href="http://www.php.net/sprintf"><span class="kw3">sprintf</span></a><span class="br0">&#40;</span><span class="st0">&#8216;%01.4f&#8217;</span>, <a href="http://www.php.net/microtime"><span class="kw3">microtime</span></a><span class="br0">&#40;</span><span class="kw2">true</span><span class="br0">&#41;</span> &#8211; <span class="re0">$start_time</span><span class="br0">&#41;</span>;</div>
<p>This code is very similar to the first example. The differences are I changed the frontendOptions lifetime value from 10 seconds to null &#8211; so the cache file never expires. Then I changed the $id value to &#8216;rs&#8217; so it doesn&#8217;t overwrite the example one cache file. Now instead of looping in the if statement, I connect to the database, query the users table, and create an array of all the rows returns. Then I save it in cache and echo out the time it took to execute. The next time, the code will find the cache file for this $id and go to the else statement and then echo the time.</p>
<p>Time taken without cache (querying database):</p>
<div class="wp-caption aligncenter" style="width: 143px"><img title="Without Cache" src="http://farm3.static.flickr.com/2635/4079111872_f5d4c607db_m.jpg" alt="in seconds" width="133" height="45" /><p class="wp-caption-text">in seconds</p></div>
<p>Time taken with cache (not querying the database):</p>
<div class="wp-caption aligncenter" style="width: 143px"><img title="With Cache" src="http://farm3.static.flickr.com/2650/4079111856_fc14ce7f52_m.jpg" alt="in seconds" width="133" height="45" /><p class="wp-caption-text">in seconds</p></div>
<p>As you can see, big improvement again &#8211; 33 times (97%) faster. &#8220;This is a difference of serving 4,477 requests per minute versus 150,000 requests per minute!&#8221; Feel free to uncomment the echo/print_r to see the data from the array. You can then update one of the users name in the database and run this page again. Notice you don&#8217;t see the new change. This is because we told the cache file to never expire so no matter what changes you make to your users in the users table, this page will continue to load the data from the cache file.</p>
<h4>Clearing the Cache File</h4>
<p>Depending on your needs, you may want the cache file to expire every 5 minutes, 2 hours, each day, or never. Even if you set a time interval for the cache file to expire, you may at some point find yourself needing to clear the cache early so this will show you how.</p>
<p>Clearing the cache is as simple as calling $cache-&gt;remove($id).  We need to add some code to delete a cache file when the users table is updated.</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw2">&lt;?php</span><br />
<span class="kw1">include</span> <span class="st0">&#8216;library/Zend/Cache.php&#8217;</span>;</p>
<p><span class="re0">$frontendOptions</span> = <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><br />
&nbsp; &nbsp;<span class="st0">&#8216;lifetime&#8217;</span> =&gt; <span class="kw2">null</span>,<br />
&nbsp; &nbsp;<span class="st0">&#8216;automatic_serialization&#8217;</span> =&gt; <span class="kw2">true</span><br />
<span class="br0">&#41;</span>;</p>
<p><span class="re0">$backendOptions</span> = <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><br />
&nbsp; &nbsp; <span class="st0">&#8216;cache_dir&#8217;</span> =&gt; <span class="st0">&#8216;tmp/&#8217;</span><br />
<span class="br0">&#41;</span>;</p>
<p><span class="re0">$cache</span> = Zend_Cache::<span class="me2">factory</span><span class="br0">&#40;</span><span class="st0">&#8216;Core&#8217;</span>, <span class="st0">&#8216;File&#8217;</span>, <span class="re0">$frontendOptions</span>, <span class="re0">$backendOptions</span><span class="br0">&#41;</span>;<br />
<span class="re0">$id</span> = <span class="st0">&#8216;rs&#8217;</span>;</p>
<p><span class="kw1">if</span><span class="br0">&#40;</span><a href="http://www.php.net/isset"><span class="kw3">isset</span></a><span class="br0">&#40;</span><span class="re0">$_GET</span><span class="br0">&#91;</span><span class="st0">&#8216;form_submit&#8217;</span><span class="br0">&#93;</span><span class="br0">&#41;</span> &amp;&amp; <span class="re0">$_GET</span><span class="br0">&#91;</span><span class="st0">&#8216;form_submit&#8217;</span><span class="br0">&#93;</span> == <span class="st0">&#8216;clear&#8217;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$cache</span>-&gt;<span class="me1">remove</span><span class="br0">&#40;</span><span class="re0">$id</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></p>
<p><span class="re0">$start_time</span> = <a href="http://www.php.net/microtime"><span class="kw3">microtime</span></a><span class="br0">&#40;</span><span class="kw2">true</span><span class="br0">&#41;</span>;</p>
<p><span class="kw1">if</span><span class="br0">&#40;</span>!<span class="br0">&#40;</span><span class="re0">$data</span> = <span class="re0">$cache</span>-&gt;<span class="me1">load</span><span class="br0">&#40;</span><span class="re0">$id</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="st0">&quot;Not found in Cache&lt;br /&gt;&quot;</span>;</p>
<p>&nbsp; &nbsp; <a href="http://www.php.net/mysql_connect"><span class="kw3">mysql_connect</span></a><span class="br0">&#40;</span><span class="st0">&#8216;localhost&#8217;</span>, <span class="st0">&#8216;user&#8217;</span>, <span class="st0">&#8216;password&#8217;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <a href="http://www.php.net/mysql_select_db"><span class="kw3">mysql_select_db</span></a><span class="br0">&#40;</span><span class="st0">&#8216;test&#8217;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="re0">$query</span> = <span class="st0">&#8217;select * from users&#8217;</span>;<br />
&nbsp; &nbsp; <span class="re0">$rs</span> = <a href="http://www.php.net/mysql_query"><span class="kw3">mysql_query</span></a><span class="br0">&#40;</span><span class="re0">$query</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; <span class="re0">$data</span> = <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="kw1">while</span><span class="br0">&#40;</span><span class="re0">$row</span> = <a href="http://www.php.net/mysql_fetch_assoc"><span class="kw3">mysql_fetch_assoc</span></a><span class="br0">&#40;</span><span class="re0">$rs</span><span class="br0">&#41;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$data</span><span class="br0">&#91;</span><span class="br0">&#93;</span> = <span class="re0">$row</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; <span class="re0">$cache</span>-&gt;<span class="me1">save</span><span class="br0">&#40;</span><span class="re0">$data</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><br />
<span class="kw1">else</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="st0">&quot;Running from Cache&lt;br /&gt;&quot;</span>;<br />
<span class="br0">&#125;</span></p>
<p><a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <a href="http://www.php.net/sprintf"><span class="kw3">sprintf</span></a><span class="br0">&#40;</span><span class="st0">&#8216;%01.4f&#8217;</span>, <a href="http://www.php.net/microtime"><span class="kw3">microtime</span></a><span class="br0">&#40;</span><span class="kw2">true</span><span class="br0">&#41;</span> &#8211; <span class="re0">$start_time</span><span class="br0">&#41;</span>;<br />
<span class="kw2">?&gt;</span><br />
&lt;form method=<span class="st0">&quot;get&quot;</span>&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &lt;input name=<span class="st0">&quot;form_submit&quot;</span> type=<span class="st0">&quot;submit&quot;</span> value=<span class="st0">&quot;reload&quot;</span>&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &lt;input name=<span class="st0">&quot;form_submit&quot;</span> type=<span class="st0">&quot;submit&quot;</span> value=<span class="st0">&quot;clear&quot;</span>&gt;<br />
&lt;/form&gt;</div>
<p>The only difference between this code and the one above is I added a form at the end of the page. This form has two buttons, one we will use to reload the page without clearing the cache and the other button we will use to clear the cache after the page is submitted. If you look at the php code, you&#8217;ll notice a new if statement.</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw1">if</span><span class="br0">&#40;</span><a href="http://www.php.net/isset"><span class="kw3">isset</span></a><span class="br0">&#40;</span><span class="re0">$_GET</span><span class="br0">&#91;</span><span class="st0">&#8216;form_submit&#8217;</span><span class="br0">&#93;</span><span class="br0">&#41;</span> &amp;&amp; <span class="re0">$_GET</span><span class="br0">&#91;</span><span class="st0">&#8216;form_submit&#8217;</span><span class="br0">&#93;</span> == <span class="st0">&#8216;clear&#8217;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$cache</span>-&gt;<span class="me1">remove</span><span class="br0">&#40;</span><span class="re0">$id</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></div>
<p>This code checks to see if you selected the &#8216;clear&#8217; button. If so, it calls the remove method in $cache to clear the cached file for $id. Right above this we are still initializing $cache the same way we use it when we want to cache a variable and we are still using the same $id &#8216;rs&#8217;.</p>
<h4>Example 3 &#8211; Caching a Page</h4>
<p>This is probably the easiest of them all because you don&#8217;t need to specify an id. The page is cached based on the url as the id. All you need to do it call $cache-start() after initializing the cache variable using &#8216;Page&#8217; as the frontend. Everything else happens behind the scene. You don&#8217;t even have to call the save() method since anything outputted by the page will be cached. Here is the code, modified from example 1:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw2">&lt;?php</span><br />
<span class="kw1">include</span> <span class="st0">&#8216;library/Zend/Cache.php&#8217;</span>;</p>
<p><span class="re0">$frontendOptions</span> = <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><br />
&nbsp; &nbsp;<span class="st0">&#8216;lifetime&#8217;</span> =&gt; <span class="nu0">10</span>,<br />
&nbsp; &nbsp;<span class="st0">&#8216;automatic_serialization&#8217;</span> =&gt; <span class="kw2">true</span><br />
<span class="br0">&#41;</span>;</p>
<p><span class="re0">$backendOptions</span> = <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><br />
&nbsp; &nbsp; <span class="st0">&#8216;cache_dir&#8217;</span> =&gt; <span class="st0">&#8216;tmp/&#8217;</span><br />
<span class="br0">&#41;</span>;</p>
<p><span class="re0">$cache</span> = Zend_Cache::<span class="me2">factory</span><span class="br0">&#40;</span><span class="st0">&#8216;Page&#8217;</span>, <span class="st0">&#8216;File&#8217;</span>, <span class="re0">$frontendOptions</span>, <span class="re0">$backendOptions</span><span class="br0">&#41;</span>;<br />
<span class="re0">$cache</span>-&gt;<span class="me1">start</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</p>
<p><span class="re0">$start_time</span> = <a href="http://www.php.net/microtime"><span class="kw3">microtime</span></a><span class="br0">&#40;</span><span class="kw2">true</span><span class="br0">&#41;</span>;</p>
<p><a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="st0">&quot;Not found in Cache&lt;br /&gt;&quot;</span>;</p>
<p><span class="re0">$data</span> = <span class="st0">&#8221;</span>;<br />
<span class="kw1">for</span> <span class="br0">&#40;</span><span class="re0">$i</span> = <span class="nu0">0</span>; <span class="re0">$i</span> &lt; <span class="nu0">1000000</span>; <span class="re0">$i</span>++<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="re0">$data</span> = <span class="re0">$data</span> . <span class="re0">$i</span>;<br />
<span class="br0">&#125;</span></p>
<p><a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <a href="http://www.php.net/sprintf"><span class="kw3">sprintf</span></a><span class="br0">&#40;</span><span class="st0">&#8216;%01.4f&#8217;</span>, <a href="http://www.php.net/microtime"><span class="kw3">microtime</span></a><span class="br0">&#40;</span><span class="kw2">true</span><span class="br0">&#41;</span> &#8211; <span class="re0">$start_time</span><span class="br0">&#41;</span>;</div>
<p>The few differences here are using &#8216;Page&#8217; as the frontend instead of &#8216;Core&#8217;. Notice that I removed the if statement since we don&#8217;t have to check for a cache file, simply calling $cache-start() will do that for us. And finally I don&#8217;t save a variable to save since as mentioned above, $cache-start() takes care of this as well.</p>
<p>There should be a slight delay loading this page the first time since it&#8217;s not cached yet. If you reload the page multiple times, you&#8217;ll notice the value in the timer doesn&#8217;t change even though the page loads much quicker. This is because all the output sent to the browser is cached, including the timer. So in this scenario, the timer is pretty useless since the value will be correct only when the code runs outside of the cache every 10 seconds (the lifetime is set to 10 seconds instead of null for this example). Every 10 seconds of reloading this page, you should see the timer change value.</p>
<h4>Additional Thoughts</h4>
<p>Cache is a powerful tool. You can save resources and time which could also mean saving money (with very little extra code!). You can have a very dynamic site which requires lots of processing and heavily relies on a database queries, creating bottlenecks that could be easily alleviated by caching instead of buying a lot more expensive hardware.</p>
<p>There is a slight overhead in using cache though. This is because your system has to read and write to files where before it wouldn&#8217;t have had to. So make sure you are using cache in a way that makes sense. Don&#8217;t use it for the fun of using it, make sure you use it to solve a problem or make a process more efficient.</p>
<p>As seen from these three examples, there are many ways to cache, so you can be creative. Sometimes caching an array makes sense, sometimes caching a whole page doesn&#8217;t. You just need to think about what the need is and how to best address it.</p>
<p>Also know there are many other forms of caching that aren&#8217;t specific to php. Caching can happen at the client level just like it can happen at server level, not just at the code.</p>
<p>Feel free to leave any questions, comments, thoughts on this topic and thanks for reading.</p>
<p>*edit: Updated my math on improvement times plus added requests example from <a title="Wiseguy" href="http://www.joeyrivera.com/2009/caching-using-phpzend_cache-and-mysql/#comment-528" target="_self">Wiseguy</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.joeyrivera.com/2009/caching-using-phpzend_cache-and-mysql/feed/</wfw:commentRss>
		<slash:comments>25</slash:comments>
		</item>
		<item>
		<title>Implementing Vanity URLs in PHP w/ Zend Framework</title>
		<link>http://www.joeyrivera.com/2009/implementing-vanity-urls-in-php-w-zend-framework/</link>
		<comments>http://www.joeyrivera.com/2009/implementing-vanity-urls-in-php-w-zend-framework/#comments</comments>
		<pubDate>Mon, 31 Aug 2009 16:45:15 +0000</pubDate>
		<dc:creator>Joey</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[controller]]></category>
		<category><![CDATA[dispatcher]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[standard]]></category>
		<category><![CDATA[url]]></category>
		<category><![CDATA[vanity]]></category>
		<category><![CDATA[zend]]></category>

		<guid isPermaLink="false">http://www.joeyrivera.com/?p=426</guid>
		<description><![CDATA[One of the reasons why people like vanity url&#8217;s is because they are easy to remember. For example take the following url:
http://www.somesocialsite.com/?user_id=123456789&#38;page=somepage
If this was the url to my page in some social network site, there&#8217;s no way I could remember it nor would I be able to easily share the url with others unless I [...]]]></description>
			<content:encoded><![CDATA[<p>One of the reasons why people like vanity url&#8217;s is because they are easy to remember. For example take the following url:</p>
<p>http://www.somesocialsite.com/?user_id=123456789&amp;page=somepage</p>
<p>If this was the url to my page in some social network site, there&#8217;s no way I could remember it nor would I be able to easily share the url with others unless I sent them the link. Now, if I could instead create a vanity url that looked like the following:</p>
<p>http://www.somesocialsite.com/joeyrivera</p>
<p>it would be much easier to remember and to share with others. Not only that but now I have a much more search engine friendly url with keywords that I would like to be found under &#8211; but ignore the search engine benefits for now.</p>
<h4>Why</h4>
<p>I&#8217;m currently working on an application that can benefit from vanity urls for the reasons mentioned above so I decided to spend some time thinking of ways to implement this. The first way that came to my mind was using <a href="http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html">mod_rewrite</a>. Mod rewrite lets you manipulate urls. For example, you can write rules in your <a href="http://wiki.apache.org/httpd/Htaccess">.htaccess</a> file so when a user goes to <em>http://www.somesocialsite.com/joeyrivera</em> it really calls <em>http://www.somesocialsite.com/search.php?user=joeyrivera</em> or in zend the request would be more like <em>http://www.somesocialsite.com/search/user/name/joeyrivera<span id="more-426"></span><br />
</em></p>
<h4>Not mod_rewrite</h4>
<p>We could make mod_rewrite work but zend already routes all traffic to the index.php file and to me, having this external-to-the-app rules just seems a bit messy. Plus, it&#8217;ll be tricky to tell mod_rewrite what urls are valid and which should be handled be zend directly. In the example above, what if joeyrivera was a legitimate controller name? In that case, zend should handle the request.</p>
<h4>Zend&#8230; yes!</h4>
<p>So then I decided I wanted zend framework to handle the vanity urls. I looked at adding rules to the router to take care of this but again I didn&#8217;t feel like this would be the best approach plus I would still have the same issue of which is a valid vanity name versus a valid controller name. What I do know is that zend at some point knows if the requested url leads to a valid controller since it throws an exception otherwise. So the search began to find that piece of code.</p>
<p>I reread the basics on <a href="http://framework.zend.com/manual/en/zend.controller.basics.html">zend controller</a> to see where the code I&#8217;m looking for should be. Based on this image:</p>
<p style="text-align: center;"><img class="aligncenter" src="http://framework.zend.com/manual/en/figures/zend.controller.basics.png" alt="Zend Controller Diagram" /></p>
<p>You start with a request which is sent to the router and is then dispatched. Based on the <a href="http://framework.zend.com/manual/en/zend.controller.basics.html">framework documentation</a>, the <strong>Zend_Controller_Dispatcher_Interface</strong>:</p>
<blockquote><p>is used to define dispatchers. Dispatching is the process of pulling the controller and action from the request object and mapping them to a controller file (or class) and action method in the controller class. If the controller or action do not exist, it handles determining default controllers and actions to dispatch</p></blockquote>
<h4>Working with the controller dispatcher</h4>
<p>So Zend_Controller_Dispatcher_Interface is what handles loading the controller class and takes action if the controller is not valid. This seems like the best place to add my vanity code. My thoughts are to add code to Zend_Controller_Dispatch_Interface so if a controller is not valid, before throwing an exception, it&#8217;ll check if the vanity url is valid. If so, it&#8217;ll redirect the user to the correct page instead of displaying the contents of the new page. I&#8217;m choosing to redirect since I don&#8217;t want to have problems with duplicate content in Google or other search engines. There are ways around it, but for now this works well for me.</p>
<p>After reading the docs on the <a href="http://framework.zend.com/manual/en/zend.controller.dispatcher.html">zend controller dispatcher</a>, this part caught my eyes:</p>
<blockquote><p>in most cases, however, you should simply extend the abstract class Zend_Controller_Dispatcher_Abstract, in which each of these have already been defined, or Zend_Controller_Dispatcher_Standard to modify functionality of the standard dispatcher.</p></blockquote>
<p>Zend_Controller_Dispatcher_Standard already extends Abstract and has all the current functionality. So what I want to do is create a new class that will extend Zend_Controller_Dispatcher_Standard and rewrite the method that loads the controller file. The method is dispatch:</p>
<blockquote><p>/**<br />
* Dispatch a request to a (module/)controller/action.<br />
*<br />
* @param  Zend_Controller_Request_Abstract $request<br />
* @param  Zend_Controller_Response_Abstract $response<br />
* @return Zend_Controller_Request_Abstract|boolean<br />
*/<br />
public function dispatch(<br />
Zend_Controller_Request_Abstract $request,<br />
Zend_Controller_Response_Abstract $response<br />
);</p></blockquote>
<h4>Extending the current dispatcher</h4>
<p>I created a new file in my library under my/controller/dispatcher/standard.php that extends Zend_Controller_Dispatcher_Standard. Then I copied the <em>dispatch </em>method from Zend_Controller_Dispatcher_Standard and pasted it into my newly created class. Now I have my new class with one method &#8211; <em>dispatch</em>. You can open the file in your zend library folder to see everything the dispatch method does but I&#8217;m only going to be modifying the first part:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw1">if</span><span class="br0">&#40;</span>!<span class="re0">$this</span>-&gt;<span class="me1">isDispatchable</span><span class="br0">&#40;</span><span class="re0">$request</span><span class="br0">&#41;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$controller</span> = <span class="re0">$request</span>-&gt;<span class="me1">getControllerName</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>!<span class="re0">$this</span>-&gt;<span class="me1">getParam</span><span class="br0">&#40;</span><span class="st0">&#8216;useDefaultControllerAlways&#8217;</span><span class="br0">&#41;</span> &amp;&amp; !<a href="http://www.php.net/empty"><span class="kw3">empty</span></a><span class="br0">&#40;</span><span class="re0">$controller</span><span class="br0">&#41;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">require_once</span> <span class="st0">&#8216;Zend/Controller/Dispatcher/Exception.php&#8217;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw <span class="kw2">new</span> Zend_Controller_Dispatcher_Exception<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#40;</span><span class="st0">&#8216;Invalid controller specified (&#8216;</span> . <span class="re0">$request</span>-&gt;<span class="me1">getControllerName</span><span class="br0">&#40;</span><span class="br0">&#41;</span> . <span class="st0">&#8216;)&#8217;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$className</span> = <span class="re0">$this</span>-&gt;<span class="me1">getDefaultControllerClass</span><span class="br0">&#40;</span><span class="re0">$request</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><br />
<span class="kw1">else</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$className</span> = <span class="re0">$this</span>-&gt;<span class="me1">getControllerClass</span><span class="br0">&#40;</span><span class="re0">$request</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>!<span class="re0">$className</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$className</span> = <span class="re0">$this</span>-&gt;<span class="me1">getDefaultControllerClass</span><span class="br0">&#40;</span><span class="re0">$request</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
<p><em>isDispatchable</em> checks to see if the controller from the request is valid. For example, if we call: http://www.joeyrivera.com/car, isDispatchable checks to see if <em>car </em>is a valid controller. If it&#8217;s valid, the class is loaded and eventually gets called. If it&#8217;s not valid, there is a check to see if the <em>dispatch </em>method should load the default controller. If so, it loads it else it throws an exception.</p>
<h4>Implementing the vanity code</h4>
<p>There are two ways to handle the vanity code at this point: 1 &#8211; set <em>useDefaultControllerAlways </em>to true so if an invalid controller is found, the default controller is called anyways and you can then let that controller decide what to do. I can think of pros and cons on doing it that way, cleaner code maybe but more repetitive code also. It seems as if I would be rewriting what this dispatch method already does. I choose the second way which is to just check if my vanity url is valid here. If so, redirect else continue with the exception.</p>
<p>Here&#8217;s my code:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw1">if</span><span class="br0">&#40;</span>!<span class="re0">$this</span>-&gt;<span class="me1">isDispatchable</span><span class="br0">&#40;</span><span class="re0">$request</span><span class="br0">&#41;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$controller</span> = <span class="re0">$request</span>-&gt;<span class="me1">getControllerName</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>!<span class="re0">$this</span>-&gt;<span class="me1">getParam</span><span class="br0">&#40;</span><span class="st0">&#8216;useDefaultControllerAlways&#8217;</span><span class="br0">&#41;</span> &amp;&amp; !<a href="http://www.php.net/empty"><span class="kw3">empty</span></a><span class="br0">&#40;</span><span class="re0">$controller</span><span class="br0">&#41;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// before we assume it&#8217;s bad, check for a vanity name</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$user</span> = <span class="kw2">new</span> My_Model_User<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$user</span>-&gt;<span class="me1">find</span><span class="br0">&#40;</span><span class="re0">$controller</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// check if valid user and redirect</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>!<a href="http://www.php.net/is_null"><span class="kw3">is_null</span></a><span class="br0">&#40;</span><span class="re0">$user</span>-&gt;<span class="me1">user_id</span><span class="br0">&#41;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/header"><span class="kw3">header</span></a><span class="br0">&#40;</span><span class="st0">&quot;Location:&quot;</span> . parent::<span class="me2">getFrontController</span><span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -&gt;<span class="me1">getBaseUrl</span><span class="br0">&#40;</span><span class="br0">&#41;</span> .<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&quot;/controller/action/user_id/{$user-&gt;user_id}/<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; first_name/{$user-&gt;first_name}/<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; last_name/{$user-&gt;last_name}&quot;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/exit"><span class="kw3">exit</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">require_once</span> <span class="st0">&#8216;Zend/Controller/Dispatcher/Exception.php&#8217;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw <span class="kw2">new</span> Zend_Controller_Dispatcher_Exception<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#40;</span><span class="st0">&#8216;Invalid controller specified (&#8216;</span> . <span class="re0">$request</span>-&gt;<span class="me1">getControllerName</span><span class="br0">&#40;</span><span class="br0">&#41;</span> . <span class="st0">&#8216;)&#8217;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$className</span> = <span class="re0">$this</span>-&gt;<span class="me1">getDefaultControllerClass</span><span class="br0">&#40;</span><span class="re0">$request</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><br />
<span class="kw1">else</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$className</span> = <span class="re0">$this</span>-&gt;<span class="me1">getControllerClass</span><span class="br0">&#40;</span><span class="re0">$request</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>!<span class="re0">$className</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$className</span> = <span class="re0">$this</span>-&gt;<span class="me1">getDefaultControllerClass</span><span class="br0">&#40;</span><span class="re0">$request</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
<p>If the controller is not valid, and <em>useDefaultControllerAlways</em> is not true, then I create a new instance of my user model and try to find an user with the vanity name passed using the variable <em>$controller</em> which is already set to <em>$request-&gt;getControllerName()</em>. The vanity name is a field stored in the database with the rest of the user information. If the user is found, I redirect to the users profile page else I just let the method call the exception as usual. I call <em>parent::getFrontController()</em> so I can use the <em>getBaseUrl()</em> method when rewriting the redirect url.</p>
<h4>Updating the bootstrap</h4>
<p>This is all for the <em>dispatch </em>method but we aren&#8217;t done yet. Now that we have our new class we need to tell the framework to use it. To do this we need to modify the <em>bootstrap </em>class. Here is the code I added to the top of my <em>bootstrap.php</em> file:</p>
<div class="dean_ch" style="white-space: wrap;">protected <span class="kw2">function</span> _initVanityName<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$this</span>-&gt;<span class="me1">bootstrap</span><span class="br0">&#40;</span><span class="st0">&#8216;FrontController&#8217;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// Retrieve the front controller from the bootstrap registry</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$front</span> = <span class="re0">$this</span>-&gt;<span class="me1">getResource</span><span class="br0">&#40;</span><span class="st0">&#8216;FrontController&#8217;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$dispatcher</span> = <span class="kw2">new</span> My_Controller_Dispatcher_Standard<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$dispatcher</span>-&gt;<span class="me1">setControllerDirectory</span><span class="br0">&#40;</span><span class="re0">$front</span>-&gt;<span class="me1">getDispatcher</span><span class="br0">&#40;</span><span class="br0">&#41;</span>-&gt;<span class="me1">getControllerDirectory</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$front</span>-&gt;<span class="me1">setDispatcher</span><span class="br0">&#40;</span><span class="re0">$dispatcher</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="re0">$front</span>;<br />
<span class="br0">&#125;</span></div>
<p>The first line makes sure the front controller resource gets initialized. Then we retrieve it via the <em>getResource </em>method. Next we instantiate the new dispatcher My_Controller_Dispatcher_Standard we created and set the controller directory since this won&#8217;t work without it. Finally we set the new dispatcher in the front controller instance and we are done.</p>
<h4>End Result</h4>
<p>Now, our application can handle vanity urls. Instead of having to remember long, complex urls such as</p>
<p>http://www.somesocialsite.com/?user_id=1231231&amp;page=somepage</p>
<p>we can use smaller, more readable urls like</p>
<p>http://www.somesocialsite.com/joeyrivera</p>
<h4>Thoughts</h4>
<p>I&#8217;m still not completely satisfied with this implementation so I&#8217;m sure I&#8217;ll be tweaking it some but it&#8217;s doing what I need it to do. I&#8217;m trying to think of ways, probably using cache, so I don&#8217;t have to hit the database each time a vanity url or an invalid controller name is passed. My other concern of having my code in the <em>dispatch </em>method is what happens when that method gets updated in future zend framework releases? Then I&#8217;ll have to remember to go back and update the method while if this code was in a controller that wouldn&#8217;t be an issue.</p>
<p>What do you guys think? Where do you guys implement this code? Let me hear your thoughts and thanks for reading.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.joeyrivera.com/2009/implementing-vanity-urls-in-php-w-zend-framework/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Extract collections of similar objects from an array in php</title>
		<link>http://www.joeyrivera.com/2009/extra-collections-of-similar-objects-from-an-array-in-php/</link>
		<comments>http://www.joeyrivera.com/2009/extra-collections-of-similar-objects-from-an-array-in-php/#comments</comments>
		<pubDate>Tue, 25 Aug 2009 18:18:03 +0000</pubDate>
		<dc:creator>Joey</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[object]]></category>

		<guid isPermaLink="false">http://www.joeyrivera.com/?p=423</guid>
		<description><![CDATA[A co-worker was working on some stuff dealing with creating collections of objects within an array so I decided to play around with the idea. Basically, you start with a big array filled of objects and the objective is to create smaller arrays of similar objects. In this case we want collections of person objects [...]]]></description>
			<content:encoded><![CDATA[<p>A co-worker was working on some stuff dealing with creating collections of objects within an array so I decided to play around with the idea. Basically, you start with a big array filled of objects and the objective is to create smaller arrays of similar objects. In this case we want collections of person objects who names are the same. Here is what I came up with:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw2">&lt;?php</span></p>
<p><span class="kw2">class</span> person<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="re0">$id</span> = <span class="kw2">null</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="re0">$name</span> = <span class="st0">&#8221;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">function</span> __construct<span class="br0">&#40;</span><span class="re0">$id</span>, <span class="re0">$name</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$this</span>-&gt;<span class="me1">id</span> = <span class="re0">$id</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$this</span>-&gt;<span class="me1">name</span> = <span class="re0">$name</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></p>
<p><span class="re0">$persons</span> = <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="re0">$persons</span><span class="br0">&#91;</span><span class="br0">&#93;</span> = <span class="kw2">new</span> person<span class="br0">&#40;</span><span class="nu0">1</span>, <span class="st0">&#8216;bob&#8217;</span><span class="br0">&#41;</span>;<br />
<span class="re0">$persons</span><span class="br0">&#91;</span><span class="br0">&#93;</span> = <span class="kw2">new</span> person<span class="br0">&#40;</span><span class="nu0">2</span>, <span class="st0">&#8216;bob&#8217;</span><span class="br0">&#41;</span>;<br />
<span class="re0">$persons</span><span class="br0">&#91;</span><span class="br0">&#93;</span> = <span class="kw2">new</span> person<span class="br0">&#40;</span><span class="nu0">3</span>, <span class="st0">&#8216;moses&#8217;</span><span class="br0">&#41;</span>;<br />
<span class="re0">$persons</span><span class="br0">&#91;</span><span class="br0">&#93;</span> = <span class="kw2">new</span> person<span class="br0">&#40;</span><span class="nu0">4</span>, <span class="st0">&#8216;joey&#8217;</span><span class="br0">&#41;</span>;<br />
<span class="re0">$persons</span><span class="br0">&#91;</span><span class="br0">&#93;</span> = <span class="kw2">new</span> person<span class="br0">&#40;</span><span class="nu0">5</span>, <span class="st0">&#8216;bob&#8217;</span><span class="br0">&#41;</span>;<br />
<span class="re0">$persons</span><span class="br0">&#91;</span><span class="br0">&#93;</span> = <span class="kw2">new</span> person<span class="br0">&#40;</span><span class="nu0">6</span>, <span class="st0">&#8216;joey&#8217;</span><span class="br0">&#41;</span>;<br />
<span class="re0">$persons</span><span class="br0">&#91;</span><span class="br0">&#93;</span> = <span class="kw2">new</span> person<span class="br0">&#40;</span><span class="nu0">7</span>, <span class="st0">&#8216;bob&#8217;</span><span class="br0">&#41;</span>;<br />
<span class="re0">$persons</span><span class="br0">&#91;</span><span class="br0">&#93;</span> = <span class="kw2">new</span> person<span class="br0">&#40;</span><span class="nu0">8</span>, <span class="st0">&#8216;moses&#8217;</span><span class="br0">&#41;</span>;<br />
<span class="re0">$persons</span><span class="br0">&#91;</span><span class="br0">&#93;</span> = <span class="kw2">new</span> person<span class="br0">&#40;</span><span class="nu0">9</span>, <span class="st0">&#8216;joey&#8217;</span><span class="br0">&#41;</span>;<br />
<span class="re0">$persons</span><span class="br0">&#91;</span><span class="br0">&#93;</span> = <span class="kw2">new</span> person<span class="br0">&#40;</span><span class="nu0">10</span>, <span class="st0">&#8216;joey&#8217;</span><span class="br0">&#41;</span>;</p>
<p><span class="re0">$collections</span> = <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span>;</p>
<p><span class="co1">// loop until there are no person left in array</span><br />
<span class="kw1">while</span><span class="br0">&#40;</span><a href="http://www.php.net/count"><span class="kw3">count</span></a><span class="br0">&#40;</span><span class="re0">$persons</span><span class="br0">&#41;</span> &gt; <span class="nu0">0</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// new collection and insert first person from array</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$collection</span> = <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$collection</span><span class="br0">&#91;</span><span class="br0">&#93;</span> = <span class="re0">$persons</span><span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// now remove person from array since already in collection</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/array_shift"><span class="kw3">array_shift</span></a><span class="br0">&#40;</span><span class="re0">$persons</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// loop through each person in array</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">for</span><span class="br0">&#40;</span><span class="re0">$x</span> = <span class="nu0">0</span>; <span class="re0">$x</span> &lt; <a href="http://www.php.net/count"><span class="kw3">count</span></a><span class="br0">&#40;</span><span class="re0">$persons</span><span class="br0">&#41;</span>; <span class="re0">$x</span>++<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// check if there is a match</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span><span class="re0">$persons</span><span class="br0">&#91;</span><span class="re0">$x</span><span class="br0">&#93;</span>-&gt;<span class="me1">name</span> == <span class="re0">$collection</span><span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span>-&gt;<span class="me1">name</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// add person to this collection</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$collection</span><span class="br0">&#91;</span><span class="br0">&#93;</span> = <span class="re0">$persons</span><span class="br0">&#91;</span><span class="re0">$x</span><span class="br0">&#93;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// pop if last item</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span><span class="re0">$x</span><span class="nu0">+1</span>-<a href="http://www.php.net/count"><span class="kw3">count</span></a><span class="br0">&#40;</span><span class="re0">$persons</span><span class="br0">&#41;</span> == <span class="nu0">0</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/array_splice"><span class="kw3">array_splice</span></a><span class="br0">&#40;</span><span class="re0">$persons</span>, <span class="re0">$x</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/array_splice"><span class="kw3">array_splice</span></a><span class="br0">&#40;</span><span class="re0">$persons</span>, <span class="re0">$x</span>, <span class="re0">$x</span><span class="nu0">+1</span>-<a href="http://www.php.net/count"><span class="kw3">count</span></a><span class="br0">&#40;</span><span class="re0">$persons</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// move back since array position gets deleted, else will skip over next index</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$x</span>&#8211;;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$collections</span><span class="br0">&#91;</span><span class="br0">&#93;</span> = <span class="re0">$collection</span>;<br />
<span class="br0">&#125;</span></p>
<p>debug<span class="br0">&#40;</span><span class="re0">$collections</span><span class="br0">&#41;</span>;<br />
<span class="kw2">function</span> debug<span class="br0">&#40;</span><span class="re0">$o</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="st0">&#8216;&lt;pre&gt;&#8217;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/print_r"><span class="kw3">print_r</span></a><span class="br0">&#40;</span><span class="re0">$o</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="st0">&#8216;&lt;/pre&gt;&#8217;</span>;<br />
<span class="br0">&#125;</span></div>
<p>And the result is:</p>
<div class="dean_ch" style="white-space: wrap;"><a href="http://www.php.net/array"><span class="kw3">Array</span></a><br />
<span class="br0">&#40;</span><br />
&nbsp; &nbsp; <span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span> =&gt; <a href="http://www.php.net/array"><span class="kw3">Array</span></a><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span> =&gt; person Object<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span>id<span class="br0">&#93;</span> =&gt; <span class="nu0">1</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span>name<span class="br0">&#93;</span> =&gt; bob<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span> =&gt; person Object<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span>id<span class="br0">&#93;</span> =&gt; <span class="nu0">2</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span>name<span class="br0">&#93;</span> =&gt; bob<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span><span class="nu0">2</span><span class="br0">&#93;</span> =&gt; person Object<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span>id<span class="br0">&#93;</span> =&gt; <span class="nu0">5</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span>name<span class="br0">&#93;</span> =&gt; bob<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span><span class="nu0">3</span><span class="br0">&#93;</span> =&gt; person Object<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span>id<span class="br0">&#93;</span> =&gt; <span class="nu0">7</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span>name<span class="br0">&#93;</span> =&gt; bob<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span></p>
<p>&nbsp; &nbsp; <span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span> =&gt; <a href="http://www.php.net/array"><span class="kw3">Array</span></a><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span> =&gt; person Object<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span>id<span class="br0">&#93;</span> =&gt; <span class="nu0">3</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span>name<span class="br0">&#93;</span> =&gt; moses<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span> =&gt; person Object<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span>id<span class="br0">&#93;</span> =&gt; <span class="nu0">8</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span>name<span class="br0">&#93;</span> =&gt; moses<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span></p>
<p>&nbsp; &nbsp; <span class="br0">&#91;</span><span class="nu0">2</span><span class="br0">&#93;</span> =&gt; <a href="http://www.php.net/array"><span class="kw3">Array</span></a><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span> =&gt; person Object<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span>id<span class="br0">&#93;</span> =&gt; <span class="nu0">4</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span>name<span class="br0">&#93;</span> =&gt; joey<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span> =&gt; person Object<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span>id<span class="br0">&#93;</span> =&gt; <span class="nu0">6</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span>name<span class="br0">&#93;</span> =&gt; joey<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span><span class="nu0">2</span><span class="br0">&#93;</span> =&gt; person Object<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span>id<span class="br0">&#93;</span> =&gt; <span class="nu0">9</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span>name<span class="br0">&#93;</span> =&gt; joey<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span><span class="nu0">3</span><span class="br0">&#93;</span> =&gt; person Object<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span>id<span class="br0">&#93;</span> =&gt; <span class="nu0">10</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span>name<span class="br0">&#93;</span> =&gt; joey<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span></p>
<p><span class="br0">&#41;</span></div>
<p>You start with 1 big array and end up with, in this case, 3 arrays of person whos names are &#8216;bob&#8217;, then &#8216;moses&#8217;, and finally &#8216;joey&#8217;.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.joeyrivera.com/2009/extra-collections-of-similar-objects-from-an-array-in-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SQLSTATE[HY000]: General error &#8211; using php/pdo mysql stored procedures (sp)</title>
		<link>http://www.joeyrivera.com/2009/sqlstatehy000-general-error-using-phppdo-mysql-stored-procedures-sp/</link>
		<comments>http://www.joeyrivera.com/2009/sqlstatehy000-general-error-using-phppdo-mysql-stored-procedures-sp/#comments</comments>
		<pubDate>Fri, 21 Aug 2009 15:15:39 +0000</pubDate>
		<dc:creator>Joey</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[general error]]></category>
		<category><![CDATA[hy000]]></category>
		<category><![CDATA[pdo]]></category>
		<category><![CDATA[sp]]></category>
		<category><![CDATA[sqlstate]]></category>
		<category><![CDATA[stored procedures]]></category>
		<category><![CDATA[zend]]></category>
		<category><![CDATA[zend framework]]></category>

		<guid isPermaLink="false">http://www.joeyrivera.com/?p=414</guid>
		<description><![CDATA[I spent many hours last night trying to figure out why I was getting a fatal error: &#8217;SQLSTATE[HY000]: General error 0&#8242; in my code and finally figured it out but first my environment. I&#8217;m using Zend Server CE running php 5.3.0, zend framework 1.9.0 and mysql 5.1.32. I should have tested this bug without the zend [...]]]></description>
			<content:encoded><![CDATA[<p>I spent many hours last night trying to figure out why I was getting a fatal error: &#8217;SQLSTATE[HY000]: General error 0&#8242; in my code and finally figured it out but first my environment. I&#8217;m using Zend Server CE running php 5.3.0, zend framework 1.9.0 and mysql 5.1.32. I should have tested this bug without the zend framework to make sure it&#8217;s not specific to zf (I don&#8217;t think it is) but I&#8217;m feeling lazy so I&#8217;ll let someone else try it out.</p>
<p>My code works as follows. I have a php class that calls a stored procedure which will take in an id, return a record set (if found) and will also return 2 out variables. While it was returning a record set everything was working perfectly fine. When I tried passing an invalid id, nothing was being returned and my code would keep giving me the &#8216;SQLSTATE[HY000]: General error 0&#8242; (very helpful error indeed&#8230;).</p>
<p>The issue ended up being the way I had my stored procedure coded. I would first check to see if the id passed was valid, if so I would select the data else I would set my out vars to some value and do nothing else. For some reason, because I wasn&#8217;t returning a select, my code would blow up. In the mysql query browser, my stored procedure worked fine and my second select to get the out vars was working correctly. But php didn&#8217;t like it one bit. I tried forcing a select in my stored procedure in the invalid id section and then everything worked fine again. This sounds a bit confusing so here is the way I can replicate this.<span id="more-414"></span></p>
<p>Here is a sample stored procedure:</p>
<div class="dean_ch" style="white-space: wrap;">DELIMITER $$</p>
<p><span class="kw1">DROP PROCEDURE</span> IF <span class="kw1">EXISTS</span> `test` $$<br />
<span class="kw1">CREATE PROCEDURE</span> `test`<br />
<span class="br0">&#40;</span><br />
&nbsp; OUT counter <span class="kw2">INT</span><br />
<span class="br0">&#41;</span><br />
<span class="kw1">BEGIN</span><br />
&nbsp; <span class="kw1">SET</span> counter = <span class="nu0">5</span>;<br />
<span class="kw1">END</span> $$</p>
<p>DELIMITER ;</p></div>
<p>In mysql query browser this works just fine if I call test(@out); and then select @out I get 5. But in my php code this blows up when I run:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="re0">$query</span> = <span class="st0">&quot;CALL test(@out);&quot;</span>;<br />
<span class="re0">$data</span> = Zend_Registry::<span class="me2">get</span><span class="br0">&#40;</span><span class="st0">&#8216;db&#8217;</span><span class="br0">&#41;</span>-&gt;<span class="me1">fetchAll</span><span class="br0">&#40;</span><span class="re0">$query</span><span class="br0">&#41;</span>;</div>
<p>To fix this problem I just need to add a select statement to the stored procedure even if I don&#8217;t need that data. I add select &#8216;hi&#8217;; to the sp and then my code works:</p>
<div class="dean_ch" style="white-space: wrap;">DELIMITER $$</p>
<p><span class="kw1">DROP PROCEDURE</span> IF <span class="kw1">EXISTS</span> `test` $$<br />
<span class="kw1">CREATE PROCEDURE</span> `test`<br />
<span class="br0">&#40;</span><br />
&nbsp; OUT counter <span class="kw2">INT</span><br />
<span class="br0">&#41;</span><br />
<span class="kw1">BEGIN</span><br />
&nbsp; <span class="kw1">SELECT</span> <span class="st0">&#8216;hi&#8217;</span>;<br />
&nbsp; <span class="kw1">SET</span> counter = <span class="nu0">5</span>;<br />
<span class="kw1">END</span> $$</p>
<p>DELIMITER ;</p></div>
<p>The quick testing php code:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="re0">$query</span> = <span class="st0">&quot;CALL test(@out);&quot;</span>;<br />
<span class="re0">$data</span> = Zend_Registry::<span class="me2">get</span><span class="br0">&#40;</span><span class="st0">&#8216;db&#8217;</span><span class="br0">&#41;</span>-&gt;<span class="me1">fetchAll</span><span class="br0">&#40;</span><span class="re0">$query</span><span class="br0">&#41;</span>;<br />
<span class="re0">$data</span> = Zend_Registry::<span class="me2">get</span><span class="br0">&#40;</span><span class="st0">&#8216;db&#8217;</span><span class="br0">&#41;</span>-&gt;<span class="me1">fetchRow</span><span class="br0">&#40;</span><span class="st0">&#8217;select @out;&#8217;</span><span class="br0">&#41;</span>;<br />
<a href="http://www.php.net/print_r"><span class="kw3">print_r</span></a><span class="br0">&#40;</span><span class="re0">$data</span><span class="br0">&#41;</span>;</div>
<p>Instead of the general error now I get Array ( [@out] =&gt; 5 ) which is what was excepted. Hope this is helpful to you guys. Now I know I have to modify my stored procedures to always have a select even if it&#8217;s empty. What I don&#8217;t know if it&#8217;s specific to my environment or not which I may do more tests later to find out. If you try this and don&#8217;t get my same error please post so we have a better idea of what causing this issue or if you know why do share.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.joeyrivera.com/2009/sqlstatehy000-general-error-using-phppdo-mysql-stored-procedures-sp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Creating RSVP in PHP/MySQL w/ Zend Framework</title>
		<link>http://www.joeyrivera.com/2009/creating-rsvp-in-phpmysql-w-zend-framework/</link>
		<comments>http://www.joeyrivera.com/2009/creating-rsvp-in-phpmysql-w-zend-framework/#comments</comments>
		<pubDate>Wed, 05 Aug 2009 20:16:03 +0000</pubDate>
		<dc:creator>Joey</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[rsvp]]></category>
		<category><![CDATA[zend]]></category>
		<category><![CDATA[zend framework]]></category>
		<category><![CDATA[zf]]></category>

		<guid isPermaLink="false">http://www.joeyrivera.com/?p=402</guid>
		<description><![CDATA[This post is to share the php/zend framework code I used to create an rsvp for my wedding site. I&#8217;m not going into all the details since that would take too long ;p but all the code is available if you want to use it. This was created using the zend framework version 1.7.
So I&#8217;m [...]]]></description>
			<content:encoded><![CDATA[<p>This post is to share the php/zend framework code I used to create an rsvp for my wedding site. I&#8217;m not going into all the details since that would take too long ;p but all the code is available if you want to use it. This was created using the zend framework version 1.7.</p>
<p>So I&#8217;m getting married in two month and for our wedding my fiancee and I decided to create a website for our guests. The site includes information such as location, time, links to registries, maps, and a section to rsvp. The site was made by my fiancee in html and css. When she was done, I ported it over to zend framework and started creating the rsvp section which I&#8217;ll describe next. You can view the finished wedding site here:</p>
<div style="text-align: center;"><img title="RSVP Page" src="http://www.joeyrivera.com/blog_files/402/images/rsvp-big.jpg" alt="RSVP Page" width="397" height="328" /></div>
<p style="text-align: center;"><a href="http://www.joeyrivera.com/wedding">http://www.joeyrivera.com/wedding</a></p>
<p><span id="more-402"></span>The objectives of the rsvp are the following:</p>
<ul>
<li>Allow guest to enter their code
<ul>
<li>each invitation that goes out has a code in it specific to the &#8216;family&#8217;.</li>
</ul>
</li>
<li>Validate code from database and load information about that family
<ul>
<li>return the family name and how many seats they have available. Because of seating limitations, we only allow x amounts of seats per family. Ex: A friend could have 2 seats available in case they are coming with a guest.</li>
</ul>
</li>
<li>Let guest select if they attending, if so, select how many guests are coming with an optional comments field</li>
<li>Submit and save their rsvp in the database and email us with the details</li>
</ul>
<p>The database structure is very simple, only one table that looks like:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw1">DROP TABLE</span> IF <span class="kw1">EXISTS</span> `family`;<br />
<span class="kw1">CREATE TABLE</span>  `family` <span class="br0">&#40;</span><br />
`family_id` <span class="kw2">INT</span><span class="br0">&#40;</span><span class="nu0">10</span><span class="br0">&#41;</span> <span class="kw3">UNSIGNED</span> <span class="kw3">NOT NULL</span> <span class="kw3">AUTO_INCREMENT</span>,<br />
`alias` <span class="kw2">VARCHAR</span><span class="br0">&#40;</span><span class="nu0">100</span><span class="br0">&#41;</span> <span class="kw3">NOT NULL</span>,<br />
`code` <span class="kw2">SMALLINT</span><span class="br0">&#40;</span><span class="nu0">5</span><span class="br0">&#41;</span> <span class="kw3">UNSIGNED</span> <span class="kw3">NOT NULL</span> <span class="kw3">DEFAULT</span> <span class="st0">&#8216;0&#8242;</span>,<br />
`attending` <span class="kw2">TINYINT</span><span class="br0">&#40;</span><span class="nu0">1</span><span class="br0">&#41;</span> <span class="kw3">DEFAULT</span> <span class="kw3">NULL</span>,<br />
`available_seats` <span class="kw2">TINYINT</span><span class="br0">&#40;</span><span class="nu0">3</span><span class="br0">&#41;</span> <span class="kw3">UNSIGNED</span> <span class="kw3">NOT NULL</span>,<br />
`confirmed_seats` <span class="kw2">TINYINT</span><span class="br0">&#40;</span><span class="nu0">3</span><span class="br0">&#41;</span> <span class="kw3">UNSIGNED</span> <span class="kw3">DEFAULT</span> <span class="kw3">NULL</span>,<br />
`comments` <span class="kw2">VARCHAR</span><span class="br0">&#40;</span><span class="nu0">255</span><span class="br0">&#41;</span> <span class="kw3">DEFAULT</span> <span class="kw3">NULL</span>,<br />
`last_access` <span class="kw2">DATETIME</span> <span class="kw3">DEFAULT</span> <span class="kw3">NULL</span>,<br />
<span class="kw1">PRIMARY KEY</span>  <span class="br0">&#40;</span>`family_id`<span class="br0">&#41;</span>,<br />
<span class="kw3">UNIQUE</span> KEY `Index_2` <span class="kw1">USING</span> <span class="kw1">BTREE</span> <span class="br0">&#40;</span>`code`<span class="br0">&#41;</span><br />
<span class="br0">&#41;</span> ENGINE=<span class="kw1">INNODB</span> <span class="kw3">AUTO_INCREMENT</span>=<span class="nu0">80</span> <span class="kw3">DEFAULT</span> <span class="kw3">CHARSET</span>=latin1;</div>
<p>The fields are:</p>
<ul>
<li><strong>Alias </strong>is the &#8216;description or name&#8217; for the family such as Ashley and Joey Rivera or The Rivera&#8217;s.</li>
<li><strong>Code </strong>is the unique code we send out with each invitation.</li>
<li><strong>Attending </strong>is a boolean that is updated when the guest rsvps.</li>
<li><strong>Available seats</strong> is the number we set to limit how many people can come from that family.</li>
<li><strong>Confirmed seats</strong> is the number the guest sets when rsvping.</li>
<li><strong>Comments </strong>is optional</li>
<li><strong>Last access</strong> tracks the last time that row was updated</li>
</ul>
<p>I made code unique because it should be, we don&#8217;t want to accidentally assign two codes to the same family. We don&#8217;t want to use the family id as the code because we want the code a bit harder to guess if someone feels like fooling around with the system &#8211; we are hoping our family members and friends won&#8217;t purposely try to break the system though :p.</p>
<p>Here is the code:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw2">&lt;?php</span></p>
<p><span class="kw2">class</span> RsvpController <span class="kw2">extends</span> Zend_Controller_Action<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; protected <span class="re0">$_redirector</span> = <span class="kw2">null</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">function</span> init<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$this</span>-&gt;_redirector = <span class="re0">$this</span>-&gt;_helper-&gt;<span class="me1">getHelper</span><span class="br0">&#40;</span><span class="st0">&#8216;Redirector&#8217;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">function</span> indexAction<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$this</span>-&gt;<span class="me1">view</span>-&gt;<span class="me1">form</span> = <span class="re0">$this</span>-&gt;<span class="me1">rsvpForm</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">function</span> submit1Action<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// get and filter code</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$filters</span> = <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;*&#8217;</span> =&gt; <span class="st0">&#8216;StringTrim&#8217;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$validators</span> = <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;Digits&#8217;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$input</span> = <span class="kw2">new</span> Zend_Filter_Input<span class="br0">&#40;</span><span class="re0">$filters</span>, <span class="re0">$validators</span>, <span class="re0">$this</span>-&gt;<span class="me1">getRequest</span><span class="br0">&#40;</span><span class="br0">&#41;</span>-&gt;<span class="me1">getParams</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// verify code was passed</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>!<span class="re0">$input</span>-&gt;<span class="me1">isValid</span><span class="br0">&#40;</span><span class="st0">&#8216;code&#8217;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// go back with error message</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$this</span>-&gt;_redirect<span class="br0">&#40;</span><span class="st0">&#8216;/rsvp&#8217;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$code</span> = <span class="re0">$input</span>-&gt;<span class="me1">code</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// get family from code</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$family</span> = <span class="kw2">new</span> Family<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$family_row</span> = <span class="re0">$family</span>-&gt;<span class="me1">fetchRow</span><span class="br0">&#40;</span><span class="st0">&#8216;code = &#8216;</span> . <span class="re0">$code</span> . <span class="st0">&#8221;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// if family doesn&#8217;t exist, leave</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span><a href="http://www.php.net/empty"><span class="kw3">empty</span></a><span class="br0">&#40;</span><span class="re0">$family_row</span><span class="br0">&#41;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$this</span>-&gt;_redirect<span class="br0">&#40;</span><span class="st0">&#8216;/rsvp&#8217;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// track row in session</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$session</span> = <span class="kw2">new</span> Zend_Session_Namespace<span class="br0">&#40;</span><span class="st0">&#8216;family&#8217;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$session</span>-&gt;<span class="me1">code</span> = <span class="re0">$code</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$session</span>-&gt;<span class="me1">row</span> = <span class="re0">$family_row</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// all is good, give view data</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$this</span>-&gt;<span class="me1">view</span>-&gt;<span class="me1">family</span> = <span class="re0">$family_row</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$this</span>-&gt;<span class="me1">view</span>-&gt;<span class="me1">form</span> = <span class="re0">$this</span>-&gt;<span class="me1">familyForm</span><span class="br0">&#40;</span><span class="re0">$family_row</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">function</span> submit2Action<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// validate session</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$session</span> = <span class="kw2">new</span> Zend_Session_Namespace<span class="br0">&#40;</span><span class="st0">&#8216;family&#8217;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>!<a href="http://www.php.net/isset"><span class="kw3">isset</span></a><span class="br0">&#40;</span><span class="re0">$session</span>-&gt;<span class="me1">row</span><span class="br0">&#41;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$this</span>-&gt;_redirect<span class="br0">&#40;</span><span class="st0">&#8216;/&#8217;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$form</span> = <span class="re0">$this</span>-&gt;<span class="me1">familyForm</span><span class="br0">&#40;</span><span class="re0">$session</span>-&gt;<span class="me1">row</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// get vars to check</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$attending</span> = <span class="re0">$this</span>-&gt;<span class="me1">getRequest</span><span class="br0">&#40;</span><span class="br0">&#41;</span>-&gt;<span class="me1">getParam</span><span class="br0">&#40;</span><span class="st0">&#8216;attending&#8217;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$seats</span> = <span class="re0">$this</span>-&gt;<span class="me1">getRequest</span><span class="br0">&#40;</span><span class="br0">&#41;</span>-&gt;<span class="me1">getParam</span><span class="br0">&#40;</span><span class="st0">&#8217;seats&#8217;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// make sure data is good</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// if attending but seats is 0 then ask them to select how many seats</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// if the seats is greater than available seats error out</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span><span class="br0">&#40;</span><span class="re0">$attending</span> &amp;amp;&amp;amp; !<span class="re0">$seats</span><span class="br0">&#41;</span> || <span class="br0">&#40;</span><span class="re0">$attending</span> &gt; <span class="re0">$session</span>-&gt;<span class="me1">row</span>-&gt;<span class="me1">available_seats</span><span class="br0">&#41;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// assumed by passing error on element would trigger this but doesn&#8217;t seem too</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$form</span>-&gt;<span class="me1">markAsError</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$seats_element</span> = <span class="re0">$form</span>-&gt;<span class="me1">getElement</span><span class="br0">&#40;</span><span class="st0">&#8217;seats&#8217;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$seats_element</span>-&gt;<span class="me1">addError</span><span class="br0">&#40;</span><span class="st0">&#8216;Please select how many people are coming.&#8217;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//else if</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// if not attending but selected seats, ask them about it</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>!<span class="re0">$attending</span> &amp;amp;&amp;amp; <span class="re0">$seats</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$form</span>-&gt;<span class="me1">markAsError</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$attending_element</span> = <span class="re0">$form</span>-&gt;<span class="me1">getElement</span><span class="br0">&#40;</span><span class="st0">&#8216;attending&#8217;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$attending_element</span>-&gt;<span class="me1">addError</span><span class="br0">&#40;</span><span class="st0">&#8216;You selected you are not coming but set the number<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; of seat to a number greater than 0. Did you mean to say you are attending?&#8217;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// if the form has an error, it can still be valid so need to check for both</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span><span class="re0">$form</span>-&gt;<span class="me1">isErrors</span><span class="br0">&#40;</span><span class="br0">&#41;</span> || !<span class="re0">$form</span>-&gt;<span class="me1">isValid</span><span class="br0">&#40;</span><span class="re0">$_POST</span><span class="br0">&#41;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$form</span>-&gt;<span class="me1">populate</span><span class="br0">&#40;</span><span class="re0">$_POST</span><span class="br0">&#41;</span>; <span class="co1">// for some reason have to manually call this or values don&#8217;t show</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$this</span>-&gt;<span class="me1">view</span>-&gt;<span class="me1">form</span> = <span class="re0">$form</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$this</span>-&gt;<span class="me1">view</span>-&gt;<span class="me1">family</span> = <span class="re0">$session</span>-&gt;<span class="me1">row</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$this</span>-&gt;<span class="me1">render</span><span class="br0">&#40;</span><span class="st0">&#8216;index&#8217;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// update family</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$family</span> = <span class="kw2">new</span> Family<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$family</span>-&gt;<span class="me1">update</span><span class="br0">&#40;</span><a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;attending&#8217;</span> =&gt; <span class="re0">$form</span>-&gt;<span class="me1">getValue</span><span class="br0">&#40;</span><span class="st0">&#8216;attending&#8217;</span><span class="br0">&#41;</span> ,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;confirmed_seats&#8217;</span> =&gt; <span class="re0">$form</span>-&gt;<span class="me1">getValue</span><span class="br0">&#40;</span><span class="st0">&#8217;seats&#8217;</span><span class="br0">&#41;</span> ,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;comments&#8217;</span> =&gt; <span class="re0">$form</span>-&gt;<span class="me1">getValue</span><span class="br0">&#40;</span><span class="st0">&#8216;comments&#8217;</span><span class="br0">&#41;</span> ,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;last_access&#8217;</span> =&gt; <a href="http://www.php.net/date"><span class="kw3">date</span></a><span class="br0">&#40;</span><span class="st0">&#8216;Y-m-d H:i:s&#8217;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span>, <span class="st0">&#8216;code = &#8216;</span> . <span class="re0">$session</span>-&gt;<span class="me1">code</span> . <span class="st0">&#8221;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// email</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$mail</span> = <span class="kw2">new</span> Zend_Mail<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$mail</span>-&gt;<span class="me1">setBodyText</span><span class="br0">&#40;</span><span class="st0">&#8216;New RSVP submitted.&#8217;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$mail</span>-&gt;<span class="me1">setBodyHtml</span><span class="br0">&#40;</span><span class="st0">&#8216;&lt;p&gt;New RSVP submitted:&lt;/p&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;p&gt;&#8217;</span> . <span class="re0">$session</span>-&gt;<span class="me1">row</span>-&gt;<span class="me1">alias</span> . <span class="st0">&#8216;&lt;/p&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;p&gt;Attending: &#8216;</span> . <span class="re0">$form</span>-&gt;<span class="me1">getValue</span><span class="br0">&#40;</span><span class="st0">&#8216;attending&#8217;</span><span class="br0">&#41;</span> . <span class="st0">&#8216;&lt;/p&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;p&gt;Seats: &#8216;</span> . <span class="re0">$form</span>-&gt;<span class="me1">getValue</span><span class="br0">&#40;</span><span class="st0">&#8217;seats&#8217;</span><span class="br0">&#41;</span> . <span class="st0">&#8216;&lt;/p&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;p&gt;Comments: &#8216;</span> . <span class="re0">$form</span>-&gt;<span class="me1">getValue</span><span class="br0">&#40;</span><span class="st0">&#8216;comments&#8217;</span><span class="br0">&#41;</span> . <span class="st0">&#8216; &lt;/p&gt;&#8217;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$mail</span>-&gt;<span class="me1">setFrom</span><span class="br0">&#40;</span><span class="st0">&#8216;joey@joeyrivera.com&#8217;</span>, <span class="st0">&#8216;JoeyRivera.com&#8217;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$mail</span>-&gt;<span class="me1">addTo</span><span class="br0">&#40;</span><span class="st0">&#8216;joey1.rivera@gmail.com&#8217;</span>, <span class="st0">&#8216;Joey Rivera&#8217;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$mail</span>-&gt;<span class="me1">addTo</span><span class="br0">&#40;</span><span class="st0">&#8216;person@person.com&#8217;</span>, <span class="st0">&#8216;Ashley Fleishel&#8217;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$mail</span>-&gt;<span class="me1">setSubject</span><span class="br0">&#40;</span><span class="st0">&#8216;Wedding New RSVP&#8217;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$mail</span>-&gt;<span class="me1">send</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$this</span>-&gt;_redirect<span class="br0">&#40;</span><span class="st0">&#8216;/rsvp/done&#8217;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">function</span> doneAction<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// unset session</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$session</span> = <span class="kw2">new</span> Zend_Session_Namespace<span class="br0">&#40;</span><span class="st0">&#8216;family&#8217;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$session</span>-&gt;<span class="me1">unsetAll</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">private</span> <span class="kw2">function</span> rsvpForm<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$form</span> = <span class="kw2">new</span> Zend_Form<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$form</span>-&gt;<span class="me1">setAction</span><span class="br0">&#40;</span><span class="st0">&#8216;/rsvp/submit1&#8242;</span><span class="br0">&#41;</span>-&gt;<span class="me1">setMethod</span><span class="br0">&#40;</span><span class="st0">&#8216;post&#8217;</span><span class="br0">&#41;</span>-&gt;<span class="me1">setAttrib</span><span class="br0">&#40;</span><span class="st0">&#8216;id&#8217;</span>, <span class="st0">&#8216;rsvp&#8217;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$form</span>-&gt;<span class="me1">addElement</span><span class="br0">&#40;</span><span class="st0">&#8216;text&#8217;</span>, <span class="st0">&#8216;code&#8217;</span>, <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;label&#8217;</span> =&gt; <span class="st0">&#8216;RSVP Code (found on your invitation)&#8217;</span> ,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;validators&#8217;</span> =&gt; <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;digits&#8217;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span> , <span class="st0">&#8216;required&#8217;</span> =&gt; <span class="kw2">true</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$form</span>-&gt;<span class="me1">addElement</span><span class="br0">&#40;</span><span class="st0">&#8217;submit&#8217;</span>, <span class="st0">&#8217;submit&#8217;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="re0">$form</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">private</span> <span class="kw2">function</span> familyForm<span class="br0">&#40;</span><span class="re0">$family</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$form</span> = <span class="kw2">new</span> Zend_Form<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$form</span>-&gt;<span class="me1">setAction</span><span class="br0">&#40;</span><span class="st0">&#8216;/rsvp/submit2&#8242;</span><span class="br0">&#41;</span>-&gt;<span class="me1">setMethod</span><span class="br0">&#40;</span><span class="st0">&#8216;post&#8217;</span><span class="br0">&#41;</span>-&gt;<span class="me1">setAttrib</span><span class="br0">&#40;</span><span class="st0">&#8216;id&#8217;</span>, <span class="st0">&#8216;rsvp&#8217;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$form</span>-&gt;<span class="me1">addElement</span><span class="br0">&#40;</span><span class="st0">&#8216;radio&#8217;</span>, <span class="st0">&#8216;attending&#8217;</span>, <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;label&#8217;</span> =&gt; <span class="st0">&#8216;Are you attending?&#8217;</span> ,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;multiOptions&#8217;</span> =&gt; <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><span class="nu0">1</span> =&gt; <span class="st0">&#8216;Yes&#8217;</span> , <span class="nu0">0</span> =&gt; <span class="st0">&#8216;No&#8217;</span><span class="br0">&#41;</span> ,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;validators&#8217;</span> =&gt; <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;Digits&#8217;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span> , <span class="st0">&#8216;required&#8217;</span> =&gt; <span class="kw2">true</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// create array of seats</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$seats</span> = <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">for</span><span class="br0">&#40;</span><span class="re0">$x</span> = <span class="nu0">0</span>, <span class="re0">$max</span> = <span class="re0">$family</span>-&gt;<span class="me1">available_seats</span>; <span class="re0">$x</span> &lt;= <span class="re0">$max</span>; <span class="re0">$x</span>++<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$seats</span><span class="br0">&#91;</span><span class="re0">$x</span><span class="br0">&#93;</span> = <span class="re0">$x</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// limit number to available seats from row session</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$form</span>-&gt;<span class="me1">addElement</span><span class="br0">&#40;</span><span class="st0">&#8217;select&#8217;</span>, <span class="st0">&#8217;seats&#8217;</span>, <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;label&#8217;</span> =&gt; <span class="st0">&#8216;How many will be attending?&#8217;</span> ,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;multiOptions&#8217;</span> =&gt; <span class="re0">$seats</span> ,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;required&#8217;</span> =&gt; <span class="kw2">true</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$form</span>-&gt;<span class="me1">addElement</span><span class="br0">&#40;</span><span class="st0">&#8216;textarea&#8217;</span>, <span class="st0">&#8216;comments&#8217;</span>, <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;label&#8217;</span> =&gt; <span class="st0">&#8216;Comments&#8217;</span> , <span class="co1">//&#8217;validators&#8217; =&gt; array(</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//&nbsp; &nbsp; &nbsp; &#8216;Alnum&#8217;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//),</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;required&#8217;</span> =&gt; <span class="kw2">false</span> , <span class="st0">&#8216;width&#8217;</span> =&gt; <span class="nu0">250</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$form</span>-&gt;<span class="me1">addElement</span><span class="br0">&#40;</span><span class="st0">&#8217;submit&#8217;</span>, <span class="st0">&#8217;submit&#8217;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="re0">$form</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
<p>I&#8217;ll summarize what I&#8217;m doing with the code. I have 4 actions in my RsvpController:</p>
<h4>index</h4>
<ul>
<li>This action display the rsvpForm in the view which is a input box for the guests to type in their family code.</li>
</ul>
<h4>submit1 (I know, name sucks but I was going for something quick ;p)</h4>
<ul>
<li>This action will validate the code submitted by the index action.</li>
<li>It then creates an instance of family using a zend db table model and loads the data from the database for the family that matches that code.</li>
<li>If it&#8217;s a real family, a session var is created with the family data and the form familyForm is displayed. Else, the user is taken back to the index action.</li>
</ul>
<h4>submit2</h4>
<ul>
<li>When the familyForm is submitted in submit1, this action validates that there is a family session var.</li>
<li>Then it does a few validations more such as making sure that the family seats is a number greater than 0 if they are attending. Or didn&#8217;t choose a number of seats greater than 0 if they are not attending (in case they accidentally hit not coming but did want to).</li>
<li>Then it validates the submitted form &#8211; I put it at the end because it seemed as if it would be less code that way. You can read the comments to see why I did what I did, I had some issues with the way I was expecting the form zend framework class to behave.</li>
<li>If all is well, the family row in the database is updated and an email is sent to myself and fiancee.</li>
</ul>
<h4>done</h4>
<ul>
<li>Displays a done message and the rsvp process is complete!</li>
</ul>
<p>That&#8217;s about it. A couple quick thoughts or comments about this. The form could be made more secure but like I said before, for this project I think it&#8217;s fine. Also, you&#8217;ll see a bunch of css in the views, it&#8217;s because I was trying to port the site over quickly and that was the quickest way. If you have any specific questions feel free to ask. Hope you find this useful. Below are some screen shots of the final product and a zip file of the code including the model, the rsvp controller, and the views in the application folder:</p>
<p><a href="http://www.joeyrivera.com/blog_files/402/application.zip">RSVP Files</a></p>
<p>index:</p>
<p><img class="alignnone" title="Index image" src="http://www.joeyrivera.com/blog_files/402/images/rsvp-index.jpg" alt="" width="289" height="178" /></p>
<p>submit1:</p>
<p><img class="alignnone" title="Options Image" src="http://www.joeyrivera.com/blog_files/402/images/rsvp-options.jpg" alt="" width="293" height="368" /></p>
<p>done:</p>
<p><img class="alignnone" title="Done Image" src="http://www.joeyrivera.com/blog_files/402/images/rsvp-done.jpg" alt="" width="285" height="122" /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.joeyrivera.com/2009/creating-rsvp-in-phpmysql-w-zend-framework/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Using MySQL Stored Procedure IN/OUT and Recordset w/ PHP</title>
		<link>http://www.joeyrivera.com/2009/using-mysql-stored-procedure-inout-and-recordset-w-php/</link>
		<comments>http://www.joeyrivera.com/2009/using-mysql-stored-procedure-inout-and-recordset-w-php/#comments</comments>
		<pubDate>Fri, 31 Jul 2009 03:21:29 +0000</pubDate>
		<dc:creator>Joey</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[db]]></category>
		<category><![CDATA[pdo]]></category>
		<category><![CDATA[sp]]></category>
		<category><![CDATA[stored procedures]]></category>

		<guid isPermaLink="false">http://www.joeyrivera.com/?p=395</guid>
		<description><![CDATA[Note: The code below will not work on all environments. I&#8217;m using php 5.2.6 with mysql driver 5.0.18.
In a previous post:
http://www.joeyrivera.com/2009/using-mysql-stored-procedures-with-php-mysqlmysqlipdo/
I explained how to use MySQL stored procedures with different db adapters such as mysql, mysqli, and pdo in PHP. In those examples, I demonstrated calling a sp with in/out variables and calling a stored [...]]]></description>
			<content:encoded><![CDATA[<p>Note: The code below will not work on all environments. I&#8217;m using php 5.2.6 with mysql driver 5.0.18.</p>
<p>In a previous post:<br />
<a href="http://www.joeyrivera.com/2009/using-mysql-stored-procedures-with-php-mysqlmysqlipdo/">http://www.joeyrivera.com/2009/using-mysql-stored-procedures-with-php-mysqlmysqlipdo/</a></p>
<p>I explained how to use MySQL stored procedures with different db adapters such as mysql, mysqli, and pdo in PHP. In those examples, I demonstrated calling a sp with in/out variables and calling a stored procedure that returns a recordset. One of the comments I received was a person asking how to call a stored procedure that uses in/out parameters as well as returns a recordset. It&#8217;s not much different and here&#8217;s how.</p>
<p>The trick is to combine both methods in one. Here&#8217;s an example of what the stored procedure looks like:</p>
<div class="dean_ch" style="white-space: wrap;">DELIMITER $$<br />
<span class="kw1">DROP PROCEDURE</span> IF <span class="kw1">EXISTS</span> `test`.`get_users` $$<br />
<span class="kw1">CREATE PROCEDURE</span> `get_users`<span class="br0">&#40;</span><br />
<span class="kw5">IN</span> firstName <span class="kw2">VARCHAR</span><span class="br0">&#40;</span><span class="nu0">100</span><span class="br0">&#41;</span>,<br />
OUT totalUsers <span class="kw2">INT</span><br />
<span class="br0">&#41;</span><br />
<span class="kw1">BEGIN</span><br />
<span class="kw1">SELECT</span> COUNT<span class="br0">&#40;</span>*<span class="br0">&#41;</span><br />
<span class="kw1">INTO</span> totalUsers<br />
<span class="kw1">FROM</span> users<br />
<span class="kw1">WHERE</span> first_name = firstName;<br />
<span class="kw1">SELECT</span> *<br />
<span class="kw1">FROM</span> users<br />
<span class="kw1">WHERE</span> first_name = firstName;<br />
<span class="kw1">END</span> $$<br />
DELIMITER ;</div>
<p><span id="more-395"></span>Notice there are two statements in the body of this stored procedure. The first select count(*) statement counts the total number of people who&#8217;s first name is equal to the in variable firstName. Once it&#8217;s gets the count, it sets the out variable totalUsers to that value.</p>
<p>The next statement is a simple select. This will select all fields for users who&#8217;s first name is equal to the in variable firstName and return the recordset. So by calling this stored procedure and passing in two parameters (first name, total), a recordset will be returned and an out variable will be set &#8211; that can then be queried.</p>
<p>I&#8217;m using the same users table from my previous post on stored procedures. Click on the link above to access that post and copy the create statement. I&#8217;m using a bit more data on the users table this time for this example to work. Here&#8217;s the insert statement:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw1">INSERT</span> <span class="kw1">INTO</span> users<br />
<span class="kw1">VALUES</span> <span class="br0">&#40;</span><span class="kw3">NULL</span>, <span class="st0">&#8216;Joey&#8217;</span>, <span class="st0">&#8216;Rivera&#8217;</span><span class="br0">&#41;</span>, <span class="br0">&#40;</span><span class="kw3">NULL</span>, <span class="st0">&#8216;John&#8217;</span>, <span class="st0">&#8216;Doe&#8217;</span><span class="br0">&#41;</span>, <span class="br0">&#40;</span><span class="kw3">NULL</span>, <span class="st0">&#8216;Joey&#8217;</span>, <span class="st0">&#8216;Tester&#8217;</span><span class="br0">&#41;</span>,<br />
<span class="br0">&#40;</span><span class="kw3">NULL</span>, <span class="st0">&#8216;Joey&#8217;</span>, <span class="st0">&#8216;Test&#8217;</span><span class="br0">&#41;</span>, <span class="br0">&#40;</span><span class="kw3">NULL</span>, <span class="st0">&#8216;Billy&#8217;</span>, <span class="st0">&#8216;Bob&#8217;</span><span class="br0">&#41;</span>;</div>
<p>The code is pretty simple, pretty much you want to do two queries. The first calls the stored procedure and passes in both variables. This first query will return a recordset (the second select statement in the body of the stored procedure. The next query will select totalUsers to get that variable and display it. Here&#8217;s the php code to do this in both mysql and pdo_mysql:</p>
<h4>MySQL</h4>
<div class="dean_ch" style="white-space: wrap;"><span class="re0">$mysql</span> = <a href="http://www.php.net/mysql_connect"><span class="kw3">mysql_connect</span></a><span class="br0">&#40;</span><span class="st0">&#8216;localhost&#8217;</span>, <span class="st0">&#8216;test&#8217;</span>, <span class="st0">&#8216;test&#8217;</span>, <span class="kw2">false</span>, <span class="nu0">65536</span><span class="br0">&#41;</span>;<br />
<a href="http://www.php.net/mysql_select_db"><span class="kw3">mysql_select_db</span></a><span class="br0">&#40;</span><span class="st0">&#8216;test&#8217;</span>, <span class="re0">$mysql</span><span class="br0">&#41;</span>;<br />
<span class="re0">$rs</span> = <a href="http://www.php.net/mysql_query"><span class="kw3">mysql_query</span></a><span class="br0">&#40;</span><span class="st0">&quot;call get_users(&#8216;joey&#8217;, @total);&quot;</span><span class="br0">&#41;</span>;<br />
<span class="kw1">while</span><span class="br0">&#40;</span><span class="re0">$row</span> = <span class="br0">&#40;</span><a href="http://www.php.net/mysql_fetch_assoc"><span class="kw3">mysql_fetch_assoc</span></a><span class="br0">&#40;</span><span class="re0">$rs</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
<a href="http://www.php.net/print_r"><span class="kw3">print_r</span></a><span class="br0">&#40;</span><span class="re0">$row</span><span class="br0">&#41;</span>;<br />
<a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="st0">&#8216;&lt;br /&gt;&#8217;</span>;<br />
<span class="br0">&#125;</span><br />
<span class="co1">// now get the @total var</span><br />
<span class="re0">$rs2</span> = <a href="http://www.php.net/mysql_query"><span class="kw3">mysql_query</span></a><span class="br0">&#40;</span><span class="st0">&quot;select @total;&quot;</span><span class="br0">&#41;</span>;<br />
<span class="re0">$total</span> = <a href="http://www.php.net/mysql_fetch_assoc"><span class="kw3">mysql_fetch_assoc</span></a><span class="br0">&#40;</span><span class="re0">$rs2</span><span class="br0">&#41;</span>;<br />
<a href="http://www.php.net/print_r"><span class="kw3">print_r</span></a><span class="br0">&#40;</span><span class="re0">$total</span><span class="br0">&#41;</span>;</div>
<h4>PDO</h4>
<div class="dean_ch" style="white-space: wrap;"><span class="re0">$pdo</span> = <span class="kw2">new</span> PDO<span class="br0">&#40;</span><span class="st0">&#8216;mysql:dbname=test;host=127.0.0.1&#8242;</span>, <span class="st0">&#8216;test&#8217;</span>, <span class="st0">&#8216;test&#8217;</span><span class="br0">&#41;</span>;<br />
<span class="re0">$data</span> = <span class="re0">$pdo</span>-&gt;<span class="me1">query</span><span class="br0">&#40;</span><span class="st0">&quot;call get_users(&#8216;joey&#8217;, @totalUsers);&quot;</span><span class="br0">&#41;</span>-&gt;<span class="me1">fetchAll</span><span class="br0">&#40;</span>PDO::<span class="me2">FETCH_ASSOC</span><span class="br0">&#41;</span>;<br />
<span class="re0">$total_count</span> = <span class="re0">$pdo</span>-&gt;<span class="me1">query</span><span class="br0">&#40;</span><span class="st0">&quot;select @totalUsers;&quot;</span><span class="br0">&#41;</span>-&gt;<span class="me1">fetchAll</span><span class="br0">&#40;</span>PDO::<span class="me2">FETCH_ASSOC</span><span class="br0">&#41;</span>;<br />
<a href="http://www.php.net/print_r"><span class="kw3">print_r</span></a><span class="br0">&#40;</span><span class="re0">$data</span><span class="br0">&#41;</span>;<br />
<a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="st0">&#8216;&lt;br /&gt;&#8217;</span>;<br />
<a href="http://www.php.net/print_r"><span class="kw3">print_r</span></a><span class="br0">&#40;</span><span class="re0">$total_count</span><span class="br0">&#41;</span>;</div>
<p>This code should output:</p>
<h4>MySQL</h4>
<div class="dean_ch" style="white-space: wrap;"><a href="http://www.php.net/array"><span class="kw3">Array</span></a> <span class="br0">&#40;</span> <span class="br0">&#91;</span>users_id<span class="br0">&#93;</span> =&gt; <span class="nu0">6</span> <span class="br0">&#91;</span>first_name<span class="br0">&#93;</span> =&gt; Joey <span class="br0">&#91;</span>last_name<span class="br0">&#93;</span> =&gt; Rivera <span class="br0">&#41;</span><br />
<a href="http://www.php.net/array"><span class="kw3">Array</span></a> <span class="br0">&#40;</span> <span class="br0">&#91;</span>users_id<span class="br0">&#93;</span> =&gt; <span class="nu0">8</span> <span class="br0">&#91;</span>first_name<span class="br0">&#93;</span> =&gt; Joey <span class="br0">&#91;</span>last_name<span class="br0">&#93;</span> =&gt; Tester <span class="br0">&#41;</span><br />
<a href="http://www.php.net/array"><span class="kw3">Array</span></a> <span class="br0">&#40;</span> <span class="br0">&#91;</span>users_id<span class="br0">&#93;</span> =&gt; <span class="nu0">9</span> <span class="br0">&#91;</span>first_name<span class="br0">&#93;</span> =&gt; Joey <span class="br0">&#91;</span>last_name<span class="br0">&#93;</span> =&gt; Test <span class="br0">&#41;</span><br />
<a href="http://www.php.net/array"><span class="kw3">Array</span></a> <span class="br0">&#40;</span> <span class="br0">&#91;</span>@total<span class="br0">&#93;</span> =&gt; <span class="nu0">3</span> <span class="br0">&#41;</span></div>
<h4>PDO</h4>
<div class="dean_ch" style="white-space: wrap;"><a href="http://www.php.net/array"><span class="kw3">Array</span></a> <span class="br0">&#40;</span> <span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span> =&gt; <a href="http://www.php.net/array"><span class="kw3">Array</span></a> <span class="br0">&#40;</span> <span class="br0">&#91;</span>users_id<span class="br0">&#93;</span> =&gt; <span class="nu0">6</span> <span class="br0">&#91;</span>first_name<span class="br0">&#93;</span> =&gt; Joey <span class="br0">&#91;</span>last_name<span class="br0">&#93;</span> =&gt; Rivera <span class="br0">&#41;</span> <span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span> =&gt; <a href="http://www.php.net/array"><span class="kw3">Array</span></a> <span class="br0">&#40;</span> <span class="br0">&#91;</span>users_id<span class="br0">&#93;</span> =&gt; <span class="nu0">8</span> <span class="br0">&#91;</span>first_name<span class="br0">&#93;</span> =&gt; Joey <span class="br0">&#91;</span>last_name<span class="br0">&#93;</span> =&gt; Tester <span class="br0">&#41;</span> <span class="br0">&#91;</span><span class="nu0">2</span><span class="br0">&#93;</span> =&gt; <a href="http://www.php.net/array"><span class="kw3">Array</span></a> <span class="br0">&#40;</span> <span class="br0">&#91;</span>users_id<span class="br0">&#93;</span> =&gt; <span class="nu0">9</span> <span class="br0">&#91;</span>first_name<span class="br0">&#93;</span> =&gt; Joey <span class="br0">&#91;</span>last_name<span class="br0">&#93;</span> =&gt; Test <span class="br0">&#41;</span> <span class="br0">&#41;</span><br />
<a href="http://www.php.net/array"><span class="kw3">Array</span></a> <span class="br0">&#40;</span> <span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span> =&gt; <a href="http://www.php.net/array"><span class="kw3">Array</span></a> <span class="br0">&#40;</span> <span class="br0">&#91;</span>@totalUsers<span class="br0">&#93;</span> =&gt; <span class="nu0">3</span> <span class="br0">&#41;</span> <span class="br0">&#41;</span></div>
<p>Like I mentioned before, both sets of code are doing the same thing, calling two queries. First the stored procedure (passing in joey for firstname and @total as the out var) then query for the out variable @total. I took a few shortcuts with pdo so it seems like less code. I personally like pdo better and it&#8217;s what I use on my projects. I hope you all find this helpful and feel free to ask any question or post any comments.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.joeyrivera.com/2009/using-mysql-stored-procedure-inout-and-recordset-w-php/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
		<item>
		<title>AMF,XMLRPC,JSON,REST Zend Web Services Tutorial</title>
		<link>http://www.joeyrivera.com/2009/amfxmlrpcjsonrest-zend-web-services-tutorial/</link>
		<comments>http://www.joeyrivera.com/2009/amfxmlrpcjsonrest-zend-web-services-tutorial/#comments</comments>
		<pubDate>Thu, 26 Mar 2009 20:17:07 +0000</pubDate>
		<dc:creator>Joey</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[amf]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[rest]]></category>
		<category><![CDATA[web services]]></category>
		<category><![CDATA[xmlrpc]]></category>
		<category><![CDATA[zend]]></category>

		<guid isPermaLink="false">http://www.joeyrivera.com/?p=356</guid>
		<description><![CDATA[I&#8217;ve been using the Zend for the last few months and I&#8217;m loving it. Here at work we were having a discussion about implementing some web services in the future so I decided to see what it takes to create some web services in PHP using Zend. I was pleasantly surprise (well not really surprised) [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been using the Zend for the last few months and I&#8217;m loving it. Here at work we were having a discussion about implementing some web services in the future so I decided to see what it takes to create some web services in PHP using Zend. I was pleasantly surprise (well not really surprised) that it was extremely quick and easy to get things up and running.</p>
<p>In this post, I&#8217;m going to explain how to create web services that can be accessed via AMF, XMLRPC, JSON, and REST using the Zend Framework. Hopefully these should cover most uses out there. I left SOAP out since I don&#8217;t really see myself using it any time soon. I will be communicating with a database as well to make the tutorial more informative. The db will consist of one table with information about courses such as mathematics courses.</p>
<p>I&#8217;m going to show you the final product first, hopefully to catch your attention so you&#8217;ll read the rest <img src='http://www.joeyrivera.com/blog_new/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> . This is what the final product will look like. If you look at the following links in Chrome, do a view source to see the formatted response.</p>
<h5>XMLRPC</h5>
<p>One course:  <a href="http://www.joeyrivera.com/ZendWebServices/xml-rpc/course-info/abbr/math101/">http://www.joeyrivera.com/ZendWebServices/xml-rpc/course-info/abbr/math101/</a><br />
All courses:  <a href="http://www.joeyrivera.com/ZendWebServices/xml-rpc/courses-info/">http://www.joeyrivera.com/ZendWebServices/xml-rpc/courses-info/</a></p>
<h5>JSON</h5>
<p>One course:  <a href="http://www.joeyrivera.com/ZendWebServices/json/course-info/abbr/math101/">http://www.joeyrivera.com/ZendWebServices/json/course-info/abbr/math101/</a><br />
All courses: <a href="http://www.joeyrivera.com/ZendWebServices/json/courses-info/">http://www.joeyrivera.com/ZendWebServices/json/courses-info/</a></p>
<h5>REST</h5>
<p>One course:  <a href="http://www.joeyrivera.com/ZendWebServices/rest/course-info/?method=getCourseInfo&amp;abbr=math102">http://www.joeyrivera.com/ZendWebServices/rest/course-info/?method=getCourseInfo&amp;abbr=math102</a><br />
All courses:  <a href="http://www.joeyrivera.com/ZendWebServices/rest/courses-info/?method=getCoursesInfo">http://www.joeyrivera.com/ZendWebServices/rest/courses-info/?method=getCoursesInfo</a></p>
<h5>AMF</h5>
<p>One course:  <a href="http://www.joeyrivera.com/ZendWebServices/amf/course-info/abbr/math101">http://www.joeyrivera.com/ZendWebServices/amf/course-info/abbr/math101</a><br />
All courses:  <a href="http://www.joeyrivera.com/ZendWebServices/amf/courses-info/">http://www.joeyrivera.com/ZendWebServices/amf/courses-info/</a><span id="more-356"></span></p>
<p>Now let me show you how to make this work. First thing is setting up the database. If you already have a database you can communicate with just use that, there&#8217;s nothing specific to the database I&#8217;ll be using that requires you to create it. But if you want to follow along here are the details. I&#8217;ll be creating a database called &#8217;sample&#8217; with a table called &#8216;course&#8217;.</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw1">DROP TABLE</span> IF <span class="kw1">EXISTS</span> `sample`.`course`;<br />
<span class="kw1">CREATE TABLE</span>  `sample`.`course` <span class="br0">&#40;</span><br />
`course_id` <span class="kw2">INT</span><span class="br0">&#40;</span><span class="nu0">10</span><span class="br0">&#41;</span> <span class="kw3">UNSIGNED</span> <span class="kw3">NOT NULL</span> <span class="kw3">AUTO_INCREMENT</span>,<br />
`abbr` <span class="kw2">VARCHAR</span><span class="br0">&#40;</span><span class="nu0">10</span><span class="br0">&#41;</span> <span class="kw3">NOT NULL</span>,<br />
`name` <span class="kw2">VARCHAR</span><span class="br0">&#40;</span><span class="nu0">45</span><span class="br0">&#41;</span> <span class="kw3">NOT NULL</span>,<br />
`title` <span class="kw2">VARCHAR</span><span class="br0">&#40;</span><span class="nu0">45</span><span class="br0">&#41;</span> <span class="kw3">NOT NULL</span>,<br />
`credits` <span class="kw2">SMALLINT</span><span class="br0">&#40;</span><span class="nu0">5</span><span class="br0">&#41;</span> <span class="kw3">UNSIGNED</span> <span class="kw3">NOT NULL</span>,<br />
<span class="kw1">PRIMARY KEY</span>  <span class="br0">&#40;</span>`course_id`<span class="br0">&#41;</span><br />
<span class="br0">&#41;</span> ENGINE=<span class="kw1">INNODB</span> <span class="kw3">AUTO_INCREMENT</span>=<span class="nu0">3</span> <span class="kw3">DEFAULT</span> <span class="kw3">CHARSET</span>=utf8;</div>
<p>Now that you have the table created, add some sample data:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw1">INSERT</span> <span class="kw1">INTO</span> `course` <span class="kw1">VALUES</span><br />
<span class="br0">&#40;</span><span class="kw3">NULL</span>,<span class="st0">&#8216;math101&#8242;</span>,<span class="st0">&#8216;Math 101&#8242;</span>,<span class="st0">&#8216;Beginner Mathematics&#8217;</span>,<span class="st0">&#8216;3&#8242;</span><span class="br0">&#41;</span>,<br />
<span class="br0">&#40;</span><span class="kw3">NULL</span>,<span class="st0">&#8216;math102&#8242;</span>,<span class="st0">&#8216;Math 102&#8242;</span>,<span class="st0">&#8216;Advanced Mathematics&#8217;</span>,<span class="st0">&#8216;4&#8242;</span><span class="br0">&#41;</span>;</div>
<p>Now the code. I have one Model class called Service. This is the only class that interacts with the database. For each format such as XMLRPC, JSON, etc. I created a Controller to handle that formats needs. All these controllers initialize a Zend server class if needed, call the method needed from the Service class, and return the formatted response to the browser.</p>
<p>This is what the Service class looks like:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw2">&lt;?php</span><br />
<span class="coMULTI">/**<br />
* Class with all the calls to the db<br />
* IMPORTANT: The methods needs comments with params and return for XMLRPC to work!!!<br />
*<br />
* @author Joey Rivera<br />
*/</span><br />
<span class="kw2">class</span> Service<br />
<span class="br0">&#123;</span><br />
<span class="coMULTI">/**<br />
* Get all courses<br />
*<br />
* @return array<br />
*/</span><br />
<span class="kw2">public</span> <span class="kw2">function</span> getCoursesInfo<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
<span class="kw1">return</span> Zend_Registry::<span class="me2">get</span><span class="br0">&#40;</span><span class="st0">&#8216;db&#8217;</span><span class="br0">&#41;</span>-&gt;<span class="me1">fetchAll</span><span class="br0">&#40;</span><span class="st0">&#8217;select * from course&#8217;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><br />
<span class="coMULTI">/**<br />
* Gets info for a course<br />
*<br />
* @param string $abbr<br />
* @return array<br />
*/</span><br />
<span class="kw2">public</span> <span class="kw2">function</span> getCourseInfo<span class="br0">&#40;</span><span class="re0">$abbr</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
<span class="kw1">return</span> Zend_Registry::<span class="me2">get</span><span class="br0">&#40;</span><span class="st0">&#8216;db&#8217;</span><span class="br0">&#41;</span>-&gt;<span class="me1">fetchRow</span><span class="br0">&#40;</span><span class="st0">&quot;select * from course where abbr = &#8216;$abbr&#8217;&quot;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
<p>Two methods make up this class. &#8216;getCoursesInfo&#8217; (plural) gets the information from all the courses in the &#8216;course&#8217; table. &#8216;getCourseInfo&#8217; takes in one parameter &#8216;abbr&#8217; and returns the information for the course who&#8217;s abbreviation matches &#8216;abbr&#8217;. You can create as many methods as you like here and these are the methods your controllers will call. One thing to note, you MUST add comments to the methods for XMLRPC calls to work. It uses the variable declaration and return declaration in your comments to know what data type to expect in and out.</p>
<p>Now lets move on to the first format: <strong>XMLRPC</strong>.</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw2">&lt;?php</span><br />
<span class="coMULTI">/**<br />
* XmlRpcController<br />
*<br />
* @author Joey Rivera<br />
*/</span><br />
<span class="kw2">class</span> XmlRpcController <span class="kw2">extends</span> Zend_Controller_Action<br />
<span class="br0">&#123;</span><br />
protected <span class="re0">$_server</span> = <span class="kw2">null</span>;<br />
protected <span class="re0">$_request</span> = <span class="kw2">null</span>;<br />
<span class="kw2">public</span> <span class="kw2">function</span> init<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
<span class="re0">$this</span>-&gt;_helper-&gt;<span class="me1">layout</span>-&gt;<span class="me1">disableLayout</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="re0">$this</span>-&gt;_helper-&gt;<span class="me1">viewRenderer</span>-&gt;<span class="me1">setNoRender</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="re0">$this</span>-&gt;_server = <span class="kw2">new</span> Zend_XmlRpc_Server<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="re0">$this</span>-&gt;_server-&gt;<span class="me1">setClass</span><span class="br0">&#40;</span><span class="st0">&#8216;Service&#8217;</span>, <span class="st0">&#8216;Service&#8217;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><br />
<span class="kw2">public</span> <span class="kw2">function</span> postDispatch<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
<a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="re0">$this</span>-&gt;_server-&gt;<span class="me1">handle</span><span class="br0">&#40;</span><span class="re0">$this</span>-&gt;_request<span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><br />
<span class="kw2">public</span> <span class="kw2">function</span> coursesInfoAction<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
<span class="re0">$this</span>-&gt;_request = <span class="kw2">new</span> Zend_XmlRpc_Request<span class="br0">&#40;</span><span class="st0">&#8216;Service.getCoursesInfo&#8217;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><br />
<span class="kw2">public</span> <span class="kw2">function</span> courseInfoAction<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
<span class="re0">$abbr</span> = Zend_Filter::<span class="me2">get</span><span class="br0">&#40;</span><span class="re0">$this</span>-&gt;_request-&gt;<span class="me1">getParam</span><span class="br0">&#40;</span><span class="st0">&#8216;abbr&#8217;</span><span class="br0">&#41;</span>, <span class="st0">&#8216;StripTags&#8217;</span><span class="br0">&#41;</span>;<br />
<span class="kw1">if</span><span class="br0">&#40;</span>!<a href="http://www.php.net/isset"><span class="kw3">isset</span></a><span class="br0">&#40;</span><span class="re0">$abbr</span><span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <a href="http://www.php.net/exit"><span class="kw3">exit</span></a>;<br />
<span class="re0">$this</span>-&gt;_request = <span class="kw2">new</span> Zend_XmlRpc_Request<span class="br0">&#40;</span><span class="st0">&#8216;Service.getCourseInfo&#8217;</span><span class="br0">&#41;</span>;<br />
<span class="re0">$this</span>-&gt;_request-&gt;<span class="me1">setParams</span><span class="br0">&#40;</span><a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><span class="re0">$abbr</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
<p>The init function gets called for any action in the controller. I am turning off the layout and view since I won&#8217;t be using them for these examples. Then I initialize the proper zend server class, in this case the &#8216;Zend_XmlRpc_Server&#8217; class. The next line &#8217;setClass&#8217; tells the server what classes will be handling incoming requests. You can add several classes and/or functions. For this sample, I&#8217;m simply attaching one class &#8216;Service&#8217; which I described earlier. &#8216;postDispatch&#8217; gets called after the action for the controller is done executing. In my &#8216;postDispatch&#8217; I tell the server instance to handle the incoming request.</p>
<p>&#8216;coursesInfoAction&#8217; creates a request object that calls the Service class &#8216;getCoursesInfo&#8217; (plural) method. Likewise, &#8216;courseInfoAction&#8217; does the same calling &#8216;getCourseInfo&#8217; (singular) but makes sure the abbr variable is set. Which ever of these two is called, a server instance gets initialized, the request variable is populated, then the server handle method is called so the request is returned to the browser with the correct format. I&#8217;m doing request objects here so I can easily test and make sure these methods are working correctly by just loading the url.</p>
<p>All the other controller classes look very similar, each initializing a server instance if necessary and then telling the server instance to display the results correctly.</p>
<p>Here&#8217;s the <strong>JSON</strong> controller:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw2">&lt;?php</span><br />
<span class="coMULTI">/**<br />
* JsonController<br />
*<br />
* @author Joey Rivera<br />
*/</span><br />
<span class="kw2">class</span> JsonController <span class="kw2">extends</span> Zend_Controller_Action<br />
<span class="br0">&#123;</span><br />
protected <span class="re0">$_return</span> = <span class="kw2">null</span>;<br />
<span class="kw2">public</span> <span class="kw2">function</span> init<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
<span class="re0">$this</span>-&gt;_helper-&gt;<span class="me1">layout</span>-&gt;<span class="me1">disableLayout</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="re0">$this</span>-&gt;_helper-&gt;<span class="me1">viewRenderer</span>-&gt;<span class="me1">setNoRender</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><br />
<span class="kw2">public</span> <span class="kw2">function</span> postDispatch<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
<a href="http://www.php.net/echo"><span class="kw3">echo</span></a> Zend_Json::<span class="me2">encode</span><span class="br0">&#40;</span><span class="re0">$this</span>-&gt;_return<span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><br />
<span class="kw2">public</span> <span class="kw2">function</span> coursesInfoAction<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
<span class="re0">$request</span> = <span class="kw2">new</span> Service<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="re0">$this</span>-&gt;_return = <span class="re0">$request</span>-&gt;<span class="me1">getCoursesInfo</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><br />
<span class="kw2">public</span> <span class="kw2">function</span> courseInfoAction<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
<span class="re0">$abbr</span> = Zend_Filter::<span class="me2">get</span><span class="br0">&#40;</span><span class="re0">$this</span>-&gt;_request-&gt;<span class="me1">getParam</span><span class="br0">&#40;</span><span class="st0">&#8216;abbr&#8217;</span><span class="br0">&#41;</span>, <span class="st0">&#8216;StripTags&#8217;</span><span class="br0">&#41;</span>;<br />
<span class="kw1">if</span><span class="br0">&#40;</span>!<a href="http://www.php.net/isset"><span class="kw3">isset</span></a><span class="br0">&#40;</span><span class="re0">$abbr</span><span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <a href="http://www.php.net/exit"><span class="kw3">exit</span></a>;<br />
<span class="re0">$request</span> = <span class="kw2">new</span> Service<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="re0">$this</span>-&gt;_return = <span class="re0">$request</span>-&gt;<span class="me1">getCourseInfo</span><span class="br0">&#40;</span><span class="re0">$abbr</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
<p>You&#8217;ll notice there is no JSON server class. All we are doing here is grabbing the data set from the Service class and calling Zend&#8217;s Zend_Json::encode to convert the data into a properly formatted JSON string and echoing it out.</p>
<p>Here you can see the implementation for <strong>REST</strong>:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw2">&lt;?php</span><br />
<span class="coMULTI">/**<br />
* RestController<br />
*<br />
* @author Joey Rivera<br />
*/</span><br />
<span class="kw2">class</span> RestController <span class="kw2">extends</span> Zend_Controller_Action<br />
<span class="br0">&#123;</span><br />
protected <span class="re0">$_server</span> = <span class="kw2">null</span>;<br />
protected <span class="re0">$_request</span> = <span class="kw2">null</span>;<br />
<span class="kw2">public</span> <span class="kw2">function</span> init<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
<span class="re0">$this</span>-&gt;_helper-&gt;<span class="me1">layout</span>-&gt;<span class="me1">disableLayout</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="re0">$this</span>-&gt;_helper-&gt;<span class="me1">viewRenderer</span>-&gt;<span class="me1">setNoRender</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="re0">$this</span>-&gt;_server = <span class="kw2">new</span> Zend_Rest_Server<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="re0">$this</span>-&gt;_server-&gt;<span class="me1">setClass</span><span class="br0">&#40;</span><span class="st0">&#8216;Service&#8217;</span>, <span class="st0">&#8216;Service&#8217;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><br />
<span class="kw2">public</span> <span class="kw2">function</span> postDispatch<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
<span class="re0">$this</span>-&gt;_server-&gt;<span class="me1">handle</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><br />
<span class="kw2">public</span> <span class="kw2">function</span> coursesInfoAction<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
<span class="br0">&#125;</span><br />
<span class="kw2">public</span> <span class="kw2">function</span> courseInfoAction<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
<span class="re0">$abbr</span> = Zend_Filter::<span class="me2">get</span><span class="br0">&#40;</span><span class="re0">$this</span>-&gt;_request-&gt;<span class="me1">getParam</span><span class="br0">&#40;</span><span class="st0">&#8216;abbr&#8217;</span><span class="br0">&#41;</span>, <span class="st0">&#8216;StripTags&#8217;</span><span class="br0">&#41;</span>;<br />
<span class="kw1">if</span><span class="br0">&#40;</span>!<a href="http://www.php.net/isset"><span class="kw3">isset</span></a><span class="br0">&#40;</span><span class="re0">$abbr</span><span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <a href="http://www.php.net/exit"><span class="kw3">exit</span></a>;<br />
<span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
<p>Starting to look repetitive? Once you figure out how to make one format work, the others are very similar. Part of using REST is passing a method parameter in the query string. The method you pass is the method of the service that will be called. This is why coursesInfo and courseInfo don&#8217;t do anything but validate if necessary.</p>
<p>Finally if you work with Flash or Flex like I do some times, you&#8217;ll want to use AMF if you are working with complex or large data sets. Here&#8217;s the <strong>AMF</strong> example:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw2">&lt;?php</span><br />
<span class="coMULTI">/**<br />
* AmfController<br />
*<br />
* @author Joey Rivera<br />
*/</span><br />
<span class="kw2">class</span> AmfController <span class="kw2">extends</span> Zend_Controller_Action<br />
<span class="br0">&#123;</span><br />
protected <span class="re0">$_server</span> = <span class="kw2">null</span>;<br />
protected <span class="re0">$_request</span> = <span class="kw2">null</span>;<br />
<span class="kw2">public</span> <span class="kw2">function</span> init<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
<span class="re0">$this</span>-&gt;_helper-&gt;<span class="me1">layout</span>-&gt;<span class="me1">disableLayout</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="re0">$this</span>-&gt;_helper-&gt;<span class="me1">viewRenderer</span>-&gt;<span class="me1">setNoRender</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="re0">$this</span>-&gt;_server = <span class="kw2">new</span> Zend_Amf_Server<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="re0">$this</span>-&gt;_server-&gt;<span class="me1">setClass</span><span class="br0">&#40;</span><span class="st0">&#8216;Service&#8217;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><br />
<span class="kw2">public</span> <span class="kw2">function</span> postDispatch<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
<a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="re0">$this</span>-&gt;_server-&gt;<span class="me1">handle</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><br />
<span class="kw2">public</span> <span class="kw2">function</span> coursesInfoAction<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
<span class="br0">&#125;</span><br />
<span class="kw2">public</span> <span class="kw2">function</span> courseInfoAction<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
<span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
<p>If you try to access the url for amf in your browser you&#8217;ll be prompted to download a file because AMF is not a text format but a binary format that Flash and Flex read. You&#8217;ll just have to take my word on that it&#8217;s working :p or create a quick Flash/Flex app that calls this service.</p>
<p>This is pretty much all I have for you all today. My next post will be on creating the different format clients to call these services. Thanks for reading. If you have any questions or comments feel free to post.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.joeyrivera.com/2009/amfxmlrpcjsonrest-zend-web-services-tutorial/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Calling a DLL with PHP</title>
		<link>http://www.joeyrivera.com/2009/calling-a-dll-with-php/</link>
		<comments>http://www.joeyrivera.com/2009/calling-a-dll-with-php/#comments</comments>
		<pubDate>Fri, 27 Feb 2009 22:10:25 +0000</pubDate>
		<dc:creator>Joey</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[com]]></category>
		<category><![CDATA[dll]]></category>
		<category><![CDATA[regsvr32]]></category>

		<guid isPermaLink="false">http://www.joeyrivera.com/?p=293</guid>
		<description><![CDATA[So here&#8217;s a quick post on calling dll&#8217;s in Windows using php. I have a dll that encrypts data in a certain format that we need for another process. So I need to pass the dll a string and it returns the encrypted string back.
I tried calling the dll using the COM class in code [...]]]></description>
			<content:encoded><![CDATA[<p>So here&#8217;s a quick post on calling dll&#8217;s in Windows using php. I have a dll that encrypts data in a certain format that we need for another process. So I need to pass the dll a string and it returns the encrypted string back.</p>
<p>I tried calling the dll using the <a href="http://us.php.net/manual/en/class.com.php" target="_blank">COM class</a> in code and was having issues until I realized I have to register the dll in windows first before I can call it using the COM class. To <a href="http://www.ss64.com/nt/regsvr32.html" target="_blank">register a dll in windows</a> you do the following in your command line:</p>
<div class="dean_ch" style="white-space: wrap;">REGSVR32 MyStuff.dll</div>
<p>Now that the dll is registered you can do the following to start accessing the dll:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="re0">$my_dll</span> = <span class="kw2">new</span> COM<span class="br0">&#40;</span><span class="st0">&#8216;MyStuff.Functions&#8217;</span><span class="br0">&#41;</span>;</div>
<p>MyStuff is the dll name an/or id and Functions is the object inside the dll that we want to use. Now I call the method I need and pass the parameters:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="re0">$encrypted_text</span> = <span class="kw2">null</span>;<br />
<span class="re0">$input</span> = <span class="st0">&#8216;This needs to be encrypted.&#8217;</span>;<br />
<span class="re0">$my_dll</span>-&gt;<span class="me1">EncryptString</span><span class="br0">&#40;</span><span class="re0">$input</span>, <span class="re0">$encrypted_text</span> <span class="br0">&#41;</span>;</div>
<p>This is pretty much it. We instantiate the COM class with the dll and function I want. Then I call the method in the dll passing my text and it returns into my $encrypted_text var the encrypted text. I can now do my next process with the encrypted text like:</p>
<div class="dean_ch" style="white-space: wrap;"><a href="http://www.php.net/print"><span class="kw3">print</span></a> <span class="re0">$encrypted_text</span>;</div>
]]></content:encoded>
			<wfw:commentRss>http://www.joeyrivera.com/2009/calling-a-dll-with-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Dynamic PHP Images Tutorial</title>
		<link>http://www.joeyrivera.com/2009/dynamic-php-images-tutorial/</link>
		<comments>http://www.joeyrivera.com/2009/dynamic-php-images-tutorial/#comments</comments>
		<pubDate>Tue, 17 Feb 2009 20:26:04 +0000</pubDate>
		<dc:creator>Joey</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[gd]]></category>
		<category><![CDATA[imagecolorallocate]]></category>
		<category><![CDATA[imagecreate]]></category>
		<category><![CDATA[imagepng]]></category>

		<guid isPermaLink="false">http://www.joeyrivera.com/?p=339</guid>
		<description><![CDATA[So I decided to play around with creating a dynamic php image to see how much work was involved and it&#8217;s actually pretty easy to do. In my case I wanted to solve the following problem.
I have a trip planned for a weekend coming up and I keep wanting to see what the weather is [...]]]></description>
			<content:encoded><![CDATA[<p>So I decided to play around with creating a dynamic php image to see how much work was involved and it&#8217;s actually pretty easy to do. In my case I wanted to solve the following problem.</p>
<p>I have a trip planned for a weekend coming up and I keep wanting to see what the weather is going to be like for that weekend. It seems everyday the weather.com forecast changes so I keep checking it on a daily basis. My options are</p>
<ol>
<li>go to the weather.com website, search for the zip code, then select view 10 day forecast or </li>
<li>create a quick and easy automated way of looking for this information as a graphic. </li>
</ol>
<p>Of course, I picked the latter. Here&#8217;s what we are about to create:</p>
<p><img src="http://www.joeyrivera.com/projects/weather_widget/newyork.png" alt="" /><br />
<span id="more-339"></span></p>
<p>Here&#8217;s the plan. I need to call the weather.com page that lists a 10 day forecast for the zip code I need (ex: <a href="http://www.weather.com/weather/tenday/30339?from=36hr_topnav_undeclared">http://www.weather.com/weather/tenday/30339?from=36hr_topnav_undeclared)</a>. Then I need to search through the HTML of that request and pull out each individual day (there should be 10 in total). Then I need to grab the values for each day such as the date, high and low temperatures, chance of rain, etc. Now I need to construct an image with all that information and save it as a png that I can check anytime I want a 10 day forecast. Every time I request this image, the entire process should take place and the image should return the latest weather forecast data.</p>
<p><strong>Grabbing the HTML request</strong></p>
<p>I am using <a href="http://us.php.net/curl" target="_blank">cURL </a>for this although <a href="http://us.php.net/manual/en/function.file-get-contents.php">file_get_contents</a> should work as well. This is what this part of the code looks like:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="re0">$zip</span> = <a href="http://www.php.net/isset"><span class="kw3">isset</span></a><span class="br0">&#40;</span><span class="re0">$_GET</span><span class="br0">&#91;</span><span class="st0">&#8216;zip&#8217;</span><span class="br0">&#93;</span><span class="br0">&#41;</span> ? &nbsp;<span class="re0">$_GET</span><span class="br0">&#91;</span><span class="st0">&#8216;zip&#8217;</span><span class="br0">&#93;</span> : <span class="nu0">30339</span>;<br />
<span class="re0">$ch</span> = curl_init<span class="br0">&#40;</span><span class="st0">&#8216;http://www.weather.com/weather/tenday/&#8217;</span>.<span class="re0">$zip</span>.<span class="st0">&#8216;?from=36hr_topnav_undeclared&#8217;</span><span class="br0">&#41;</span>;<br />
curl_setopt<span class="br0">&#40;</span><span class="re0">$ch</span>, CURLOPT_HEADER, <span class="nu0">0</span><span class="br0">&#41;</span>;<br />
curl_setopt<span class="br0">&#40;</span><span class="re0">$ch</span>, CURLOPT_RETURNTRANSFER, <span class="nu0">1</span><span class="br0">&#41;</span>;<br />
<span class="re0">$data</span> = curl_exec<span class="br0">&#40;</span><span class="re0">$ch</span><span class="br0">&#41;</span>;<br />
curl_close<span class="br0">&#40;</span><span class="re0">$ch</span><span class="br0">&#41;</span>;</div>
<p>First I check for the zip code I want then I init curl with the proper params and get the HTML request. If you have questions about the options I&#8217;m using here&#8217;s the <a href="http://us.php.net/manual/en/function.curl-setopt.php" target="_blank">info</a>.</p>
<p>Now I grab the data I need from the HTML using a nasty substr. I was having a &#8220;regex just isn&#8217;t cooperating with me day&#8221; so I quit wasting time and did it the easier way. I haven&#8217;t gone back to optimize this code but I probably will later.</p>
<div class="dean_ch" style="white-space: wrap;"><span class="co1">// get only 10 day divs</span><br />
<span class="re0">$start_pos</span> = <a href="http://www.php.net/strpos"><span class="kw3">strpos</span></a><span class="br0">&#40;</span><span class="re0">$data</span>, <span class="st0">&#8216;&lt;div id=&quot;tenDay&quot;&gt;&#8217;</span><span class="br0">&#41;</span>;<br />
<span class="re0">$end_pos</span> = <a href="http://www.php.net/strpos"><span class="kw3">strpos</span></a><span class="br0">&#40;</span><span class="re0">$data</span>, <span class="st0">&#8216;&lt;!&#8211; tenDayWrap &#8211;&gt;&#8217;</span><span class="br0">&#41;</span>;<br />
<span class="re0">$data</span> = <a href="http://www.php.net/substr"><span class="kw3">substr</span></a><span class="br0">&#40;</span><span class="re0">$data</span>, <span class="re0">$start_pos</span>, <span class="re0">$end_pos</span> &#8211; <span class="re0">$start_pos</span><span class="br0">&#41;</span>;<br />
<span class="re0">$items</span> = <a href="http://www.php.net/preg_split"><span class="kw3">preg_split</span></a><span class="br0">&#40;</span><span class="st0">&#8216;/&lt;!&#8211; day [0-9]+ &#8211;&gt;/&#8217;</span>, <span class="re0">$data</span><span class="br0">&#41;</span>;<br />
<a href="http://www.php.net/array_shift"><span class="kw3">array_shift</span></a><span class="br0">&#40;</span><span class="re0">$items</span><span class="br0">&#41;</span>; <span class="co1">// get rid of first empty node</span></div>
<p>Now that I have all the days in an array I can start creating my image object.</p>
<div class="dean_ch" style="white-space: wrap;"><span class="re0">$img</span> = @imagecreate<span class="br0">&#40;</span><span class="nu0">550</span>,<span class="nu0">140</span><span class="br0">&#41;</span>;<br />
<span class="re0">$bg</span> = imagecolorallocate<span class="br0">&#40;</span><span class="re0">$img</span>, <span class="nu0">0</span>, <span class="nu0">0</span>, <span class="nu0">0</span><span class="br0">&#41;</span>;<br />
<span class="re0">$tc</span> = imagecolorallocate<span class="br0">&#40;</span><span class="re0">$img</span>, <span class="nu0">233</span>, <span class="nu0">14</span>, <span class="nu0">91</span><span class="br0">&#41;</span>;<br />
&nbsp;</div>
<p>imagecreate is creating an empty image with the assigned width and height. The next two lines are the background color and the text color that will be used for my image in RGB. Now comes the meat of the process of creating my image:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="re0">$x</span> = <span class="nu0">5</span>;<br />
<span class="re0">$y</span> = <span class="nu0">0</span>;<br />
<span class="re0">$counter</span> = <span class="nu0">0</span>;<br />
<span class="kw1">foreach</span><span class="br0">&#40;</span><span class="re0">$items</span> <span class="kw1">as</span> <span class="re0">$item</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
<a href="http://www.php.net/preg_match_all"><span class="kw3">preg_match_all</span></a><span class="br0">&#40;</span><span class="st0">&#8216;|&lt;[^&gt;]+&gt;(.*)]+&gt;|U&#8217;</span>, <span class="re0">$item</span>, <span class="re0">$values</span><span class="br0">&#41;</span>;<br />
<span class="re0">$day</span><span class="br0">&#91;</span><span class="br0">&#93;</span> = <a href="http://www.php.net/substr"><span class="kw3">substr</span></a><span class="br0">&#40;</span><span class="re0">$values</span><span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span><span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span>, <a href="http://www.php.net/strrpos"><span class="kw3">strrpos</span></a><span class="br0">&#40;</span><span class="re0">$values</span><span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span><span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span>, <span class="st0">&#8216;&gt;&#8217;</span><span class="br0">&#41;</span> + <span class="nu0">1</span><span class="br0">&#41;</span>;<br />
<span class="re0">$date</span><span class="br0">&#91;</span><span class="br0">&#93;</span> = <span class="re0">$values</span><span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span><span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span>;<br />
<span class="re0">$type</span><span class="br0">&#91;</span><span class="br0">&#93;</span> = <a href="http://www.php.net/substr"><span class="kw3">substr</span></a><span class="br0">&#40;</span><span class="re0">$values</span><span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span><span class="br0">&#91;</span><span class="nu0">3</span><span class="br0">&#93;</span>, <a href="http://www.php.net/strrpos"><span class="kw3">strrpos</span></a><span class="br0">&#40;</span><span class="re0">$values</span><span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span><span class="br0">&#91;</span><span class="nu0">3</span><span class="br0">&#93;</span>, <span class="st0">&#8216;&gt;&#8217;</span><span class="br0">&#41;</span> + <span class="nu0">1</span><span class="br0">&#41;</span>;<br />
<span class="re0">$high</span><span class="br0">&#91;</span><span class="br0">&#93;</span> = <a href="http://www.php.net/html_entity_decode"><span class="kw3">html_entity_decode</span></a><span class="br0">&#40;</span><a href="http://www.php.net/substr"><span class="kw3">substr</span></a><span class="br0">&#40;</span><span class="re0">$values</span><span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span><span class="br0">&#91;</span><span class="nu0">5</span><span class="br0">&#93;</span>, <a href="http://www.php.net/strrpos"><span class="kw3">strrpos</span></a><span class="br0">&#40;</span><span class="re0">$values</span><span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span><span class="br0">&#91;</span><span class="nu0">5</span><span class="br0">&#93;</span>, <span class="st0">&#8216;&gt;&#8217;</span><span class="br0">&#41;</span> + <span class="nu0">1</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
<span class="re0">$low</span><span class="br0">&#91;</span><span class="br0">&#93;</span> = <a href="http://www.php.net/html_entity_decode"><span class="kw3">html_entity_decode</span></a><span class="br0">&#40;</span><a href="http://www.php.net/substr"><span class="kw3">substr</span></a><span class="br0">&#40;</span><span class="re0">$values</span><span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span><span class="br0">&#91;</span><span class="nu0">6</span><span class="br0">&#93;</span>, <a href="http://www.php.net/strrpos"><span class="kw3">strrpos</span></a><span class="br0">&#40;</span><span class="re0">$values</span><span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span><span class="br0">&#91;</span><span class="nu0">6</span><span class="br0">&#93;</span>, <span class="st0">&#8216;&gt;&#8217;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
<span class="re0">$rain</span><span class="br0">&#91;</span><span class="br0">&#93;</span> = <a href="http://www.php.net/html_entity_decode"><span class="kw3">html_entity_decode</span></a><span class="br0">&#40;</span><a href="http://www.php.net/substr"><span class="kw3">substr</span></a><span class="br0">&#40;</span><span class="re0">$values</span><span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span><span class="br0">&#91;</span><span class="nu0">8</span><span class="br0">&#93;</span>, <a href="http://www.php.net/strrpos"><span class="kw3">strrpos</span></a><span class="br0">&#40;</span><span class="re0">$values</span><span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span><span class="br0">&#91;</span><span class="nu0">8</span><span class="br0">&#93;</span>, <span class="st0">&#8216;&gt;&#8217;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
imagestring<span class="br0">&#40;</span><span class="re0">$img</span>, <span class="nu0">1</span>, <span class="re0">$x</span>, <span class="re0">$y</span><span class="nu0">+5</span>, <span class="re0">$day</span><span class="br0">&#91;</span><span class="re0">$counter</span><span class="br0">&#93;</span>, <span class="re0">$tc</span><span class="br0">&#41;</span>;<br />
imagestring<span class="br0">&#40;</span><span class="re0">$img</span>, <span class="nu0">1</span>, <span class="re0">$x</span>, <span class="re0">$y</span><span class="nu0">+15</span>, <span class="re0">$date</span><span class="br0">&#91;</span><span class="re0">$counter</span><span class="br0">&#93;</span>, <span class="re0">$tc</span><span class="br0">&#41;</span>;<br />
imagestring<span class="br0">&#40;</span><span class="re0">$img</span>, <span class="nu0">1</span>, <span class="re0">$x</span>, <span class="re0">$y</span><span class="nu0">+25</span>, <span class="re0">$type</span><span class="br0">&#91;</span><span class="re0">$counter</span><span class="br0">&#93;</span>, <span class="re0">$tc</span><span class="br0">&#41;</span>;<br />
imagestring<span class="br0">&#40;</span><span class="re0">$img</span>, <span class="nu0">1</span>, <span class="re0">$x</span>, <span class="re0">$y</span><span class="nu0">+35</span>, <span class="st0">&#8216;High: &#8216;</span>.<span class="re0">$high</span><span class="br0">&#91;</span><span class="re0">$counter</span><span class="br0">&#93;</span>, <span class="re0">$tc</span><span class="br0">&#41;</span>;<br />
imagestring<span class="br0">&#40;</span><span class="re0">$img</span>, <span class="nu0">1</span>, <span class="re0">$x</span>, <span class="re0">$y</span><span class="nu0">+45</span>, <span class="st0">&#8216;Low: &#8216;</span>.<span class="re0">$low</span><span class="br0">&#91;</span><span class="re0">$counter</span><span class="br0">&#93;</span>, <span class="re0">$tc</span><span class="br0">&#41;</span>;<br />
imagestring<span class="br0">&#40;</span><span class="re0">$img</span>, <span class="nu0">1</span>, <span class="re0">$x</span>, <span class="re0">$y</span><span class="nu0">+55</span>, <span class="st0">&#8216;Percip.: &#8216;</span>.<span class="re0">$rain</span><span class="br0">&#91;</span><span class="re0">$counter</span><span class="br0">&#93;</span>, <span class="re0">$tc</span><span class="br0">&#41;</span>;<br />
<span class="re0">$x</span> += <span class="nu0">110</span>;<br />
<span class="re0">$counter</span>++;<br />
<span class="kw1">if</span><span class="br0">&#40;</span><span class="re0">$counter</span> == <span class="nu0">5</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
<span class="re0">$x</span> = <span class="nu0">5</span>;<br />
<span class="re0">$y</span> = <span class="nu0">70</span>;<br />
<span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
<p>I am looping through all my days and setting up variables that I am then creating text objects for on my image. For each item, I create an imagestring passing in the image object, x and y pos, the string and the color. Each item is spaced out 10 pixels from each other in height and 110 px in width. I&#8217;m displaying the data as two rows of five items. Now that the image has all the data I need all that is left to do is display it by passing a image/png header and returning the image itself as a png.</p>
<div class="dean_ch" style="white-space: wrap;"><a href="http://www.php.net/header"><span class="kw3">header</span></a><span class="br0">&#40;</span><span class="st0">&quot;Content-type: image/png&quot;</span><span class="br0">&#41;</span>;<br />
imagepng<span class="br0">&#40;</span><span class="re0">$img</span><span class="br0">&#41;</span>;<br />
imagedestroy<span class="br0">&#40;</span><span class="re0">$img</span><span class="br0">&#41;</span>;<br />
&nbsp;</div>
<p>And there you go, that&#8217;s all needed to get the following result:</p>
<p><img src="http://www.joeyrivera.com/projects/weather_widget/atlanta.png" alt="" /></p>
<p>This is the 10 day weather forecast for the Atlanta Area. I&#8217;m using my .htaccess to call atlanta.png and forward that to my php file with a 30339 zip code. Here&#8217;s another example calling a zip code in new york:</p>
<p><img src="http://www.joeyrivera.com/projects/weather_widget/newyork.png" alt="" /></p>
<p>As the day progresses, you&#8217;ll notice the numbers change since every time these images are requested the php page gets the latest data from weather.com before rendering the new image. As always, questions or comments are welcomed!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.joeyrivera.com/2009/dynamic-php-images-tutorial/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
