<?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>Inside DealTakertutorial » Inside DealTaker</title>
	<atom:link href="http://www.dealtaker.com/our-blog/tag/tutorial/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dealtaker.com/our-blog</link>
	<description>Just another DealTaker.com site</description>
	<lastBuildDate>Fri, 10 Feb 2012 14:56:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Looking Forward to a Fast Tax Return? Start by Getting Organized!</title>
		<link>http://www.dealtaker.com/our-blog/2012/01/13/looking-forward-to-a-fast-tax-return-start-by-getting-organized/</link>
		<comments>http://www.dealtaker.com/our-blog/2012/01/13/looking-forward-to-a-fast-tax-return-start-by-getting-organized/#comments</comments>
		<pubDate>Fri, 13 Jan 2012 01:34:53 +0000</pubDate>
		<dc:creator>siddeley</dc:creator>
				<category><![CDATA[Headlines and Topics]]></category>
		<category><![CDATA[2011]]></category>
		<category><![CDATA[2012]]></category>
		<category><![CDATA[2012 Tax Filing]]></category>
		<category><![CDATA[Filing Federal Taxes]]></category>
		<category><![CDATA[Organization]]></category>
		<category><![CDATA[tax]]></category>
		<category><![CDATA[Tax Deductions]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.dealtaker.com/our-blog/?p=4833</guid>
		<description><![CDATA[<img width="150" height="150" src="http://s1.dtkcdn.com/dtwp/media/10/2012/01/22-150x150.jpg" class="attachment-index-categories wp-post-image" alt="Getting Organized for Tax Season" title="Getting Organized for Tax Season" />Are you looking forward to a Fast Tax Return this year? A key place to start is organizing your information. If you break theÂ  process down into many small steps you will be able to get everything done faster and may even come away feeling like you have accomplished something! I hate to admit this [...]]]></description>
			<content:encoded><![CDATA[<img width="150" height="150" src="http://s1.dtkcdn.com/dtwp/media/10/2012/01/22-150x150.jpg" class="attachment-index-categories wp-post-image" alt="Getting Organized for Tax Season" title="Getting Organized for Tax Season" /><p>Are you looking forward to a Fast Tax Return this year? A key place to start is organizing your information. If you break theÂ  process down into many small steps you will be able to get everything done faster and may even come away feeling like you have accomplished something!</p>
<p><span id="more-4833"></span><img title="More..." src="http://www.dealtaker.com/our-blog/wp-includes/js/tinymce/plugins/wordpress/img/trans.gif" alt="" /></p>
<p>I hate to admit this &#8211; and I&#8217;m sure this has never happened to anyone but me! &#8211; but I have had to take a dive on a few tax credits because I couldn&#8217;t find my receipts, even electronic receipts.Â Â  For Example, When I am &#8220;filing&#8221; emails and paperwork I always think that I can easily remember where I deposited them. Eight months later when I go to my email &#8220;business&#8221; folder to find all of the online receipts and confirmations, I don&#8217;t see the big donation I made to the SPCA! After sifting through all of the extraneous emails that wound up in my business folder because I needed to quickly clear out my exploding inbox, I remember that for the first few months of the year I was using the previous year&#8217;s tax folder to file business items! Sounds confusing I know, but you get the picture; a lot of info is processed over the course of a year and a little organization will keep us from pulling our hair out trying to find it! Below is my suggestion for gathering 2011 tax info and setting yourself up for an easier time preparing for 2012 taxes.</p>
<p><span style="color: #000000;"><strong>Info gathered online<br />
</strong></span></p>
<p>~ Break out a new sheet on &#8220;notepad&#8221; or one of your other favorite place to write lists on your computer.<br />
~ Start by taking a quick review of every folder for every email address you have. The task at hand is to establish a base from which you can get the info you need.Â  This list is just for you so don&#8217;t worry about whether they do or don&#8217;t have tax info in them at this point.<br />
~ Jot down the email addresses and the folders you will need to have a look at.<br />
~ Now create a folder on your main email address that is labeled &#8217;2011 Taxes&#8217;<br />
~ Over the next few days check the folders for Tax info.<br />
~ Move anything into the &#8217;2011 Taxes&#8217; folder that could possibly relate to taxes.<br />
~ Now you can tackle the items in your &#8217;2011 Taxes&#8217; folder and drill down even further.<br />
~ Once the sorting is finished you should have a &#8220;pure&#8221; &#8217;2011 Taxes&#8217; folder that can be used to prepare your own tax return or send it off to a tax professional.<br />
~ Continue to add to the &#8217;2011 Taxes&#8217; folder any correspondence or documents that are sent your way. This is a great thing to have if you are ever audited!</p>
<p><span style="color: #000000;"><strong>Do the same sorting with Hard-copy information<br />
</strong></span></p>
<p>Gather up anything that looks like it might be tax related and deposit it into a folder or basket, grocery bag or anything that can hold it all temporarily.Â  Once you have scoured the house and your basket is full, sit down and go through everything in the basket until you have a good set of paperwork to either take to your tax professional or process yourself. (Tax professionals are very good at sifting through paperwork so if you have included something that cant be used it won&#8217;t hurt anything)</p>
<p><strong>2012 tax year<br />
</strong></p>
<p>Start now by creating a &#8217;2012 Tax&#8217; email folder.Â  Throughout the year move anything into it that could possibly be used on your tax return. Do the same for hard copy information: Get a folder or bin going right away and you will be glad that most of the above steps wont be needed when January 2013 rolls around.</p>
<p><span style="color: #000000;"><strong>Looking for more information?Â  Check out these articles:</strong></span></p>
<p>~ Looking for additional information on organizing your paper piles? Check out this article by Home Goes Strong &#8211; &#8220;<a title="Learning How To Deal With Paper Piles" href="http://home.lifegoesstrong.com/getting-organizedlearning-how-deal-paper-piles" target="_blank">Getting Organized: Learning How to Deal with Paper Piles</a>&#8221;<br />
~ There are people out there who will take your piles and sort through them for you &#8211; <a title="Organizing Help" href="http://pilesofpaper.com/what-i-do/organizing-for-taxes/%20" target="_blank">PilesofPaper.com</a> advertises that for a fee they will process your documents and return them to you organized!<br />
~ Maybe there is a Tax Deduction you didn&#8217;t know about?Â  Check out Kiplinger&#8217;s article &#8220;<a title="The Most Overlooked Tax Deductions!" href="http://www.kiplinger.com/features/archives/the-mostoverlooked-tax-deductions2.html?kipad_id=x" target="_blank">The Most-Overlooked Tax Deductions</a>&#8221; or MSN Money&#8217;s &#8220;<a title="The 19 Most Overlooked Tax Deductions" href="http://articles.moneycentral.msn.com/Taxes/CutYourTaxes/the-19-most-overlooked-tax-deductions.aspx" target="_blank">The 19 most-overlooked tax deductions</a>&#8220;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dealtaker.com/our-blog/2012/01/13/looking-forward-to-a-fast-tax-return-start-by-getting-organized/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Kohana PHP 3.0 (KO3) Tutorial Part 9</title>
		<link>http://www.dealtaker.com/our-blog/2010/06/02/kohana-php-3-0-ko3-tutorial-part-9/</link>
		<comments>http://www.dealtaker.com/our-blog/2010/06/02/kohana-php-3-0-ko3-tutorial-part-9/#comments</comments>
		<pubDate>Wed, 02 Jun 2010 13:23:25 +0000</pubDate>
		<dc:creator>ellisgl</dc:creator>
				<category><![CDATA[DealTaker.com Updates]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[KO3]]></category>
		<category><![CDATA[kohana]]></category>
		<category><![CDATA[library]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[Zend]]></category>

		<guid isPermaLink="false">http://www.dealtaker.com/blog/?p=2340</guid>
		<description><![CDATA[Welcome to the ninth part in this series on how to develop with Kohana PHP V3 (KO3). If you havenâ€™t read any of previous parts yet, I would search for and read them before going on. In this tutorial we will be going over using other frameworks&#8217; libraries. Before we get into loading external libraries, [...]]]></description>
			<content:encoded><![CDATA[<p>Welcome to the ninth part in this series on how to develop with Kohana PHP V3 (KO3). If you havenâ€™t read any of previous parts yet, I would search for and read them before going on. In this tutorial we will be going over using other frameworks&#8217; libraries.<br />
<span id="more-2340"></span><br />
Before we get into loading external libraries, weâ€™ll want to update the KO3 code base. Go in to &#8220;application&#8221; directory and rename your &#8220;bootstrap.php&#8221; to &#8220;bootstrap.bak.php&#8221;. Now point your browser to http://kohanaframework.org/download and download the latest stable (3.0.5), open it and extract everything inside the &#8220;kohana&#8221; folder to &#8220;mykohana3&#8243;. Once you have done that, delete or rename the &#8220;install.php&#8221; in your &#8220;mykohana3&#8243; folder. Next, delete the &#8220;bootstrap.php&#8221; file in the &#8220;application&#8221; folder and rename the &#8220;bootstrap.bak.php&#8221; to &#8220;bootstrap.php&#8221;.</p>
<p>The first thing we need to do is create a folder named &#8220;vendors&#8221; under &#8220;application&#8221;. In the &#8220;vendors&#8221; folder, create another folder named &#8220;Zend&#8221; (yes, with a capital &#8220;Z&#8221;). Next we&#8217;ll need to download the latest version of Zend Framework, so click here to download 1.10.5. Open the archive up and extract everything from &#8220;ZendFramework-1.10.5-minimal/library/Zend/&#8221; into &#8220;application/vendors/Zend&#8221;.</p>
<p>Open the  &#8220;application/bootstrap.php&#8221; file and add the following above the line which reads &#8220;echo Request::getInstance()&#8221;:</p>
<p>[php]<br />
$path = Kohana::find_file(&#8216;vendors&#8217;, &#8216;Zend/Loader&#8217;);</p>
<p>if($path)<br />
 {<br />
    ini_set(&#8216;include_path&#8217;,<br />
    ini_get(&#8216;include_path&#8217;) . PATH_SEPARATOR . dirname(dirname($path)));</p>
<p>    require_once &#8216;Zend/Loader/Autoloader.php&#8217;;<br />
    Zend_Loader_Autoloader::getInstance();<br />
 }<br />
[/php]</p>
<p>Open &#8220;application/views/pages/ko3.php&#8221; and put the following at the bottom:</p>
<p>[php]<br />
  &lt;?php $validator = new Zend_Validate_Isbn();?&gt;<br />
  &lt;br /&gt;978-3-16-148410-0 &lt;?php echo ($validator-&gt;isValid(&#8217;978-3-16-148410-0&#8242;)) ? &#8216;is&#8217; : &#8216;is not&#8217;;?&gt; valid.<br />
  &lt;br /&gt;9783161484100 &lt;?php echo ($validator-&gt;isValid(&#8217;9783161484100&#8242;)) ? &#8216;is&#8217; : &#8216;is not&#8217;;?&gt; valid.<br />
[/php]</p>
<p>Now if you load &#8220;http://yourserver/mykohana3/ko3&#8243; in your browser, you should see the two tests at the bottom.</p>
<p>Until next time, when I go over forms, happy coding.</p>
<p>If you enjoyed this tutorial, please check out our <a href="http://www.dealtaker.com/technology/" target="_blank">tech blog</a> and <a href="http://www.dealtaker.com/category/computers-office/computers/" target="_blank">computer related coupons</a>.</p>
<p><a href="http://www.dealtaker.com/blog/2010/04/30/kohana-php-3-0-ko3-tutorial-part-8/">Part 8</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.dealtaker.com/our-blog/2010/06/02/kohana-php-3-0-ko3-tutorial-part-9/feed/</wfw:commentRss>
		<slash:comments>40</slash:comments>
		</item>
		<item>
		<title>PHP &amp; MongoDB Sitting in a Tree: Part 1</title>
		<link>http://www.dealtaker.com/our-blog/2010/05/12/php-mongodb-sitting-in-a-tree-part-1/</link>
		<comments>http://www.dealtaker.com/our-blog/2010/05/12/php-mongodb-sitting-in-a-tree-part-1/#comments</comments>
		<pubDate>Wed, 12 May 2010 21:08:30 +0000</pubDate>
		<dc:creator>ellisgl</dc:creator>
				<category><![CDATA[DealTaker.com Updates]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[db]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[mongo]]></category>
		<category><![CDATA[mongodb]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[nosql]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[scale]]></category>
		<category><![CDATA[Schema-less]]></category>
		<category><![CDATA[schemaless]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.dealtaker.com/blog/?p=2187</guid>
		<description><![CDATA[Yet another MongoDB and PHP tutorial, say it ain&#8217;t so! Well yes, but this tutorial aims to be more complete than the others out there. OK I&#8217;m going to assume you know what MongoDB is, so I&#8217;m not going to go over &#8220;what mongoDB is&#8221; or &#8220;what NoSQL is&#8221;. I&#8217;m going to do this series [...]]]></description>
			<content:encoded><![CDATA[<p>Yet another MongoDB and PHP tutorial, say it ain&#8217;t so! Well yes, but this tutorial aims to be more complete than the others out there. OK I&#8217;m going to assume you know what MongoDB is, so I&#8217;m not going to go over &#8220;what mongoDB is&#8221; or &#8220;what NoSQL is&#8221;. I&#8217;m going to do this series in a little different styling than my other tutorial series have been, so let just jump right in.<br />
<span id="more-2187"></span></p>
<p>Here&#8217;s a couple links to get you up to speed:<br />
<a href="http://www.mongodb.org/display/DOCS/Quickstart">Installing Mongo</a><br />
<a href="http://www.mongodb.org/display/DOCS/PHP+Language+Center">MongoDB PHP Extension</a></p>
<p>If you have issues with MongoD not starting &#8211; something about &#8220;/data/db&#8221; missing, then you should create the directory. In *Nix: &#8220;mkdir /data/db&#8221;, in Win &#8220;mkdir c:data&#8221; then &#8220;mkdir c:datadb&#8221;.</p>
<p><strong>Connecting</strong><br />
<em>MySQL</em></p>
<p>[php]<br />
&lt;?php<br />
$host = &#8216;localhost&#8217;;<br />
$user = &#8216;root&#8217;;<br />
$pass = &#8220;&#8221;;<br />
$link = mysql_connect($host, $user, $pass, 1) or die(&#8220;Could not connect to: {$host}. Check your settings and try again.&#8221;);<br />
[/php]</p>
<p><em>MongoDB</em></p>
<p>[php]<br />
&lt;?php<br />
try<br />
{<br />
$link = new Mongo();<br />
}<br />
catch(MongoConnectionException $e)<br />
{<br />
die(&#8216;Could not connect. Check to make sure MongoDB is running.&#8217;);<br />
}<br />
[/php]</p>
<p>As you can see, Mongo DB automatically will connect to the local host and default port. Yes, MySQL will do the same if you have the directives in your &#8220;php.ini&#8221; setup to do this. IE. &#8220;mysql.default_host&#8221;, &#8220;mysql.default_user&#8221; and &#8220;mysql.default_password&#8221;. You might notice that by default MongoDB doesn&#8217;t have a user name or password setup. I will go over how to set that up in part 2.</p>
<p><strong>Creating and using a DB</strong><br />
<em>MySQL</em></p>
<p>[php]$db  = &#8216;testdb&#8217;;<br />
$sql = &#8220;CREATE DATABASE `$db`&#8221;;</p>
<p>mysql_query($sql, $link);<br />
mysql_select_db($db, $link);<br />
[/php]</p>
<p><em>MongoDB</em></p>
<p>[php]$db   = $link-&gt;testdb;<br />
[/php]</p>
<p>In one line of code, MongoDB will create a DB automatically if it doesn&#8217;t already exists and select (use) it.</p>
<p><strong>Create a Table / Collection</strong><br />
<em>MySQL</em></p>
<p>[php]<br />
// Create the Table<br />
$tbl = &#8216;user&#8217;;<br />
$sql = &#8220;CREATE TABLE `$tbl` (`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY, `login` VARCHAR (24)NOT NULL, `password` CHAR(32) NOT NULL, `email` VARCHAR(255)) TYPE=innodb;&#8221;;<br />
mysql_query($sql, $link);<br />
[/php]</p>
<p><em>MongoDB</em></p>
<p>[php]<br />
// Create the collection (AKA Table)<br />
$col  = $db-&gt;user;<br />
[/php]</p>
<p>Once again, MongoDB will automatically create something for us if it doesn&#8217;t exist. In this example, it create our &#8220;user&#8221; table. You might notice we didn&#8217;t define how the table is formatted, this is because MongoDB is schema-less and doesn&#8217;t need column definitions. &#8220;Tables&#8221; in MongoDB are called &#8220;collections&#8221;.</p>
<p><strong>Insert Data</strong><br />
<em>MySQL</em></p>
<p>[php]<br />
// Insert a row into the table<br />
$sql = &#8220;INSERT INTO `$tbl` SET `login` = &#8216;jsmith&#8217;, `password` = &#8217;5f4dcc3b5aa765d61d8327deb882cf99&#8242;, `email` = &#8216;jsmith@example.com&#8217;&#8221;;<br />
mysql_query($sql);</p>
<p>$sql = &#8220;INSERT INTO `$tbl` SET `login` = &#8216;psmith&#8217;, `password` = &#8217;5f4dcc3b5aa765d61d8327deb882cf99&#8242;, `email` = &#8216;psmith@example.com&#8217;&#8221;;<br />
mysql_query($sql);</p>
<p>// Get the ID of last insert<br />
$id = mysql_insert_id($link);<br />
[/php]</p>
<p><em>MongoDB</em></p>
<p>[php]<br />
// Insert a document (row) into the collection (table)<br />
$doc  = array(&#8216;login&#8217; =&gt; &#8216;jsmith&#8217;, &#8216;password&#8217; =&gt; &#8216; 5f4dcc3b5aa765d61d8327deb882cf99&#8242;, &#8216;email&#8217; =&gt; &#8216;jsmith@example.com&#8217;);<br />
$col-&gt;insert($doc, true);</p>
<p>$doc  = array(&#8216;login&#8217; =&gt; &#8216;psmith&#8217;, &#8216;password&#8217; =&gt; &#8216; 5f4dcc3b5aa765d61d8327deb882cf99&#8242;, &#8216;email&#8217; =&gt; &#8216;psmith@example.com&#8217;);<br />
$col-&gt;insert($doc, true);</p>
<p>// Get the id of last insert<br />
$id   = $doc['_id'];<br />
[/php]</p>
<p>You might have noticed that we used an array to define our &#8220;row&#8221; of data, which are called &#8220;documents&#8221;. On the insert method, the second argument will set the query to do a &#8220;safe insert&#8221;. This will allow us to find out if the query executed properly. If not set, it will not get that information. So I would set it to true if you want to be able to debug your queries. If you are wondering about the password, it&#8217;s the MD5 hash for &#8220;password&#8221;.</p>
<p><strong>Querying Data</strong><br />
<em>MySQL</em></p>
<p>[php]<br />
// Get all rows<br />
$sql = &#8220;SELECT * FROM `$tbl`&#8221;;<br />
$qry = mysql_query($sql, $link);<br />
$cnt = mysql_num_rows($qry);</p>
<p>echo &#8216;All rows:&lt;br/&gt;&#8217;;</p>
<p>if($cnt &gt; 0)<br />
{<br />
do<br />
{<br />
$row = mysql_fetch_assoc($qry);</p>
<p>echo &#8216;&lt;pre&gt;&#8217;;<br />
print_r($row);<br />
echo &#8216;&lt;/pre&gt;&#8217;<br />
}<br />
}</p>
<p>// Query for the row matching the last insert ID<br />
$sql = &#8220;SELECT * FROM `$tbl` WHERE `id` = {$id}&#8221;;<br />
$qry = mysql_query($sql, $link);<br />
$row = mysql_fetch_assoc($qry);</p>
<p>echo &#8216;Single row (id = $id):&amp;lt;br/&amp;gt;&amp;lt;pre&amp;gt;&#8217;;<br />
print_r($row);<br />
echo &#8216;&lt;/pre&gt;<br />
[/php]</p>
<p><em>MongoDB</em></p>
<p>[php]<br />
// Get all documents<br />
$res = $col-&gt;find();</p>
<p>echo &#8216;All documents:&lt;br/&gt;&#8217;;</p>
<p>foreach($res as $doc)<br />
{<br />
echo &#8216;&lt;pre&gt;&#8217;;<br />
print_r($doc);<br />
echo &#8216;&lt;/pre&gt;&#8217;;<br />
}</p>
<p>// Query for the document matching the last insert ID<br />
$doc  = $col-&gt;findone(array(&#8216;_id&#8217; =&gt; $id));</p>
<p>echo &#8216;Single document (_id = $id):&lt;br/&gt;&lt;pre&gt;&#8217;;<br />
print_r($doc);<br />
[/php]</p>
<p>The MongoDB support in PHP has function to pull a single document, or all the documents. Once again, we use arrays to define something, in this example, we define our constraint.</p>
<p><strong>Upading Data</strong><br />
<em>MySQL</em></p>
<p>[php]<br />
$sql = &#8220;UPDATE `$tbl` SET `password` = &#8216;b497dd1a701a33026f7211533620780d&#8217; WHERE `id` = {$id}&#8221;;<br />
$qry = mysql_query($sql, $link);</p>
<p>$sql = &#8220;SELECT * FROM `$tbl` WHERE `id` = {$id}&#8221;;<br />
$qry = mysql_query($sql, $link);<br />
$row = mysql_fetch_assoc($qry);</p>
<p>echo &#8216;Updated row (id = $id):&lt;br/&gt;&lt;pre&gt;&#8217;;<br />
print_r($row);<br />
echo &#8216;&lt;/pre&gt;<br />
[/php]</p>
<p><em>MogoDB</em></p>
<p>[php]<br />
// Update a document<br />
$col-&gt;update(array(&#8216;_id&#8217; =&gt; $id), array(&#8216;$set&#8217; =&gt; array(&#8216;password&#8217; =&gt; &#8216;b497dd1a701a33026f7211533620780d&#8217;)));</p>
<p>// Query the updated docuemnt<br />
$doc = $col-&gt;findone(array(&#8216;_id&#8217; =&gt; $id));</p>
<p>echo &#8216;Updated docuement:&lt;br/&gt;&lt;pre&gt;&#8217;;<br />
print_r($doc);<br />
echo &#8216;&lt;/pre&gt;&#8217;;<br />
[/php]</p>
<p>The MongoDB extension has a function to perform updates. The first argument is an array of constraints, IE &#8220;WHERE {expression}&#8221;. The second is the data we want to update and what we want to update it with. Pretty simple, right?</p>
<p><strong>Indexing data</strong><br />
<em>MySQL</em></p>
<p>[php]<br />
// Create a unique index<br />
$sql = &#8220;ALTER TABLE `$db`.`$tbl` ADD UNIQUE `login` (`login`)&#8221;;<br />
$qry = mysql_query($sql, $link);<br />
[/php]</p>
<p><em>MongoDB</em></p>
<p>[php]<br />
// Create a unique index<br />
$col-&gt;ensureIndex(array(&#8220;login&#8221; =&gt; 1), array(&#8220;unique&#8221; =&gt; true, &#8220;dropDups&#8221; =&gt; true));<br />
[/php]</p>
<p>MongoDB has method for creating index name &#8220;ensureIndex&#8221;. The first parameter is an array of what we want to index and the value is ether 1 or -1. 1 means that the index will sort the data for this in an ascending manor, while -1 will sort it in a descending manor. The second parameter are extra options. In this example I tell it to make what ever I&#8217;m indexing as a unique index, that way I don&#8217;t end up with multiple &#8220;jsmith&#8221; entries. Also, I&#8217;ve told it to drop any documents that have duplicates of the data in the &#8220;login&#8221; &#8220;column&#8221;. Another note is that &#8220;columns&#8221; in MongoDB should be called &#8220;elements&#8221;, since we are really dealing with arrays.</p>
<p>Lets make sure our index is working shall we?<br />
<em>MongoDb</em></p>
<p>[php]<br />
// Test our unique index<br />
try<br />
{<br />
$doc  = array(&#8216;login&#8217; =&gt; &#8216;jsmith&#8217;, &#8216;password&#8217; =&gt; &#8216; 5f4dcc3b5aa765d61d8327deb882cf99&#8242;, &#8216;email&#8217; =&gt; &#8216;jsmith@example.com&#8217;);<br />
$col-&gt;insert($doc, true);<br />
}<br />
catch(MongoCursorException $e)<br />
{<br />
echo &#8216;Could not insert document. Double check values.&lt;br /&gt;&#8217;;<br />
}<br />
[/php]</p>
<p>If all works, we should get an error message.</p>
<p><strong>Limiting Results</strong><br />
<em>MySQL</em></p>
<p>[php]<br />
// Limits<br />
$sql = &#8220;SELECT * FROM `$tbl` LIMIT 1&#8243;;<br />
$qry = mysql_query($sql, $link);<br />
$cnt = mysql_num_rows($qry);</p>
<p>echo &#8216;All rows &#8211; Limit by 1:&lt;br/&gt;&#8217;;</p>
<p>if($cnt &gt; 0)<br />
{<br />
do<br />
{<br />
$row = mysql_fetch_assoc($qry);</p>
<p>echo &#8216;&lt;pre&gt;&#8217;;<br />
print_r($row);<br />
echo &#8216;&lt;/pre&gt;&#8217;<br />
}<br />
}<br />
[/php]</p>
<p><em>MongoDB</em></p>
<p>[php]<br />
// Limits<br />
$res = $col-&gt;find()-&gt;limit(1);</p>
<p>echo &#8216;All documents &#8211; Limited to 1:&lt;br/&gt;&#8217;;</p>
<p>foreach($res as $doc)<br />
{<br />
echo &#8216;&lt;pre&gt;&#8217;;<br />
print_r($doc);<br />
echo &#8216;&lt;/pre&gt;&#8217;;<br />
}<br />
[/php]</p>
<p><strong>Offsetting</strong><br />
<em>MySQL</em></p>
<p>[php]<br />
// Limits<br />
$sql = &#8220;SELECT * FROM `$tbl` OFFSET 1 LIMIT 1&#8243;;<br />
$qry = mysql_query($sql, $link);<br />
$cnt = mysql_num_rows($qry);</p>
<p>echo &#8216;All rows &#8211; Limit by 1 &#8211; Offset by 1:&lt;br/&gt;&#8217;;</p>
<p>if($cnt &gt; 0)<br />
{<br />
do<br />
{<br />
$row = mysql_fetch_assoc($qry);</p>
<p>echo &#8216;&lt;pre&gt;&#8217;;<br />
print_r($row);<br />
echo &#8216;&lt;/pre&gt;&#8217;;<br />
}<br />
}<br />
[/php]</p>
<p><em>MongoDB</em></p>
<p>[php]<br />
// Offsets<br />
$res = $col-&amp;gt;find()-&amp;gt;skip(1)-&amp;gt;limit(1);</p>
<p>echo &#8216;All documents &#8211; Limited to 1, Offset by 1:&lt;br/ &gt;&#8217;;</p>
<p>foreach($res as $doc)<br />
{<br />
echo &#8216;&lt;pre&gt;&#8217;;<br />
print_r($doc);<br />
echo &#8216;&lt;/pre&gt;&#8217;;<br />
}<br />
&lt;/pre&gt;<br />
[/php]</p>
<p>To offset a your query start point, we used the method &#8220;skip&#8221; with an integer value to create the offset.</p>
<p><strong>Sorting</strong><br />
<em>MySQL</em></p>
<p>[php]<br />
// Sorting<br />
$sql = &#8220;SELECT * FROM `$tbl` ORDERBY `login` DESC&#8221;;<br />
$qry = mysql_query($sql, $link);<br />
$cnt = mysql_num_rows($qry);</p>
<p>echo &#8216;All rows &#8211;  Sorted by login descending:&lt;br/&gt;&#8217;;</p>
<p>if($cnt &gt; 0)<br />
{<br />
do<br />
{<br />
$row = mysql_fetch_assoc($qry);</p>
<p>echo &#8216;&lt;pre&gt;&#8217;;<br />
print_r($row);<br />
echo &#8216;&lt;/pre&gt;&#8217;<br />
}while(&#8211;$cnt &gt; 0);<br />
}<br />
[/php]</p>
<p><em>MongoDB</em></p>
<p>[php]<br />
// Sorting<br />
$res = $col-&gt;find()-&gt;sort(array(&#8216;login&#8217; =&gt; -1));</p>
<p>echo &#8216;All documents &#8211; Sorted by login descending:&lt;br/&gt;&#8217;;</p>
<p>foreach($res as $doc)<br />
{<br />
echo &#8216;&lt;pre&gt;&#8217;;<br />
print_r($doc);<br />
echo &#8216;&lt;/pre&gt;&#8217;;<br />
}<br />
[/php]</p>
<p>Here we used the sort method, told it what element to sort on and which direction, -1 being descending. The record for &#8220;psmith&#8221; should show up first. A word of caution here, there is a 4MB limit if sorting on non-indexed element.</p>
<p><strong>Advanced Queries</strong><br />
<em>MySQL</em></p>
<p>[php]<br />
// Advanced queries<br />
// Add new column<br />
$sql = &#8220;ALTER `$db`.`$tbl` ADD COLUMN `age` INT UNSIGNED&#8221;;<br />
mysql_query($sql, $link);</p>
<p>// Add values<br />
$sql = &#8220;UPDATE `$tbl` SET `age` = 40 WHERE `login` = &#8216;jsmith&#8217;&#8221;;<br />
mysql_query($sql, $link);</p>
<p>$sql = &#8220;UPDATE `$tbl` SET `age` = 24 WHERE `login` = &#8216;psmith&#8217;&#8221;;<br />
mysql_query($sql, $link);</p>
<p>// Query for anyone under 30<br />
$sql = &#8220;SELECT * FROM `$tbl` WHERE `age` &lt; 30&#8243;;<br />
$qry = mysql_query($sql, $link);<br />
$cnt = mysql_num_rows($qry);</p>
<p>echo &#8216;All documents &#8211; Anyone under 30:&lt;br/&gt;&#8217;;</p>
<p>if($cnt &gt; 0)<br />
{<br />
do<br />
{<br />
$row = mysql_fetch_assoc($qry);</p>
<p>echo &#8216;&lt;pre&gt;&#8217;;<br />
print_r($row);<br />
echo &#8216;&lt;/pre&gt;&#8217;;<br />
}<br />
}</p>
<p>// Query for anyone over 30<br />
$sql = &#8220;SELECT * FROM `$tbl` WHERE `age` &gt; 30&#8243;;<br />
$qry = mysql_query($sql, $link);<br />
$cnt = mysql_num_rows($qry);</p>
<p>echo &#8216;All documents &#8211; Anyone over 30:&lt;br/&gt;&#8217;;</p>
<p>if($cnt &gt; 0)<br />
{<br />
do<br />
{<br />
$row = mysql_fetch_assoc($qry);</p>
<p>echo &#8216;&lt;pre&gt;&#8217;;<br />
print_r($row);<br />
echo &#8216;&lt;/pre&gt;&#8217;;<br />
}<br />
}</p>
<p>// Query from anyone 25 and over<br />
$sql = &#8220;SELECT * FROM `$tbl` WHERE `age` &gt;= 25&#8243;;<br />
$qry = mysql_query($sql, $link);<br />
$cnt = mysql_num_rows($qry);</p>
<p>echo &#8216;All documents &#8211; Anyone 25 and over:&lt;br/&gt;&#8217;;</p>
<p>if($cnt &gt; 0)<br />
{<br />
do<br />
{<br />
$row = mysql_fetch_assoc($qry);</p>
<p>echo &#8216;&lt;pre&gt;&#8217;;<br />
print_r($row);<br />
echo &#8216;&lt;/pre&gt;&#8217;;<br />
}<br />
}<br />
// Query for anyone 25 and under<br />
$sql = &#8220;SELECT * FROM `$tbl` WHERE `age` &lt;= 25&#8243;;<br />
$qry = mysql_query($sql, $link);<br />
$cnt = mysql_num_rows($qry);</p>
<p>echo &#8216;All documents &#8211; Anyone 25 and under:&lt;br/&gt;&#8217;;</p>
<p>if($cnt &gt; 0)<br />
{<br />
do<br />
{<br />
$row = mysql_fetch_assoc($qry);</p>
<p>echo &#8216;&lt;pre&gt;&#8217;;<br />
print_r($row);<br />
echo &#8216;&lt;/pre&gt;&#8217;;<br />
}<br />
}</p>
<p>// Query for anyone not 40<br />
$sql = &#8220;SELECT * FROM `$tbl` WHERE `age` != 40&#8243;;<br />
$qry = mysql_query($sql, $link);<br />
$cnt = mysql_num_rows($qry);</p>
<p>echo &#8216;All documents &#8211; Anyone not 30:&lt;br/&gt;&#8217;;</p>
<p>if($cnt &gt; 0)<br />
{<br />
do<br />
{<br />
$row = mysql_fetch_assoc($qry);</p>
<p>echo &#8216;&lt;pre&gt;&#8217;;<br />
print_r($row);<br />
echo &#8216;&lt;/pre&gt;&#8217;;<br />
}<br />
}</p>
<p>// Query for anyone 20-50<br />
$sql = &#8220;SELECT * FROM `$tbl` WHERE `age` &gt;= 20 AND `age` &lt;= 50&#8243;;<br />
$qry = mysql_query($sql, $link);<br />
$cnt = mysql_num_rows($qry);</p>
<p>echo &#8216;All documents &#8211; Anyone 20-50:&lt;br/&gt;&#8217;;</p>
<p>if($cnt &gt; 0)<br />
{<br />
do<br />
{<br />
$row = mysql_fetch_assoc($qry);</p>
<p>echo &#8216;&lt;pre&gt;&#8217;;<br />
print_r($row);<br />
echo &#8216;&lt;/pre&gt;&#8217;;<br />
}<br />
}<br />
[/php]</p>
<p><em>MongoDB</em></p>
<p>[php]<br />
// Advanced queries<br />
// Add a new element and value<br />
$col-&gt;update(array(&#8216;login&#8217; =&gt; &#8216;jsmith&#8217;), array(&#8216;$set&#8217; =&gt; array(&#8216;age&#8217; =&gt; 40)));<br />
$col-&gt;update(array(&#8216;login&#8217; =&gt; &#8216;psmith&#8217;), array(&#8216;$set&#8217; =&gt; array(&#8216;age&#8217; =&gt; 24)));</p>
<p>// Confirm<br />
$res = $col-&gt;find();</p>
<p>echo &#8216;All documents &#8211; Confirming age element:&lt;br/&gt;&#8217;;</p>
<p>foreach($res as $doc)<br />
{<br />
echo &#8216;&lt;pre&gt;&#8217;;<br />
print_r($doc);<br />
echo &#8216;&lt;/pre&gt;&#8217;;<br />
}</p>
<p>// Query for anyone under 30<br />
$res = $col-&gt;find(array(&#8216;age&#8217; =&gt; array(&#8216;$lt&#8217; =&gt; 30)));</p>
<p>echo &#8216;All documents &#8211; Anyone under 30:&lt;br/&gt;&#8217;;</p>
<p>foreach($res as $doc)<br />
{<br />
echo &#8216;&lt;pre&gt;&#8217;;<br />
print_r($doc);<br />
echo &#8216;&lt;/pre&gt;&#8217;;<br />
}</p>
<p>// Query for anyone over 30<br />
$res = $col-&gt;find(array(&#8216;age&#8217; =&gt; array(&#8216;$gt&#8217; =&gt; 30)));</p>
<p>echo &#8216;All documents &#8211; Anyone over 30:&lt;br/&gt;&#8217;;</p>
<p>foreach($res as $doc)<br />
{<br />
echo &#8216;&lt;pre&gt;&#8217;;<br />
print_r($doc);<br />
echo &#8216;&lt;/pre&gt;&#8217;;<br />
}</p>
<p>// Query from anyone 25 or over<br />
$res = $col-&gt;find(array(&#8216;age&#8217; =&gt; array(&#8216;$gte&#8217; =&gt; 25)));</p>
<p>echo &#8216;All documents &#8211; Anyone 25 or older:&lt;br/&gt;&#8217;;</p>
<p>foreach($res as $doc)<br />
{<br />
echo &#8216;&lt;pre&gt;&#8217;;<br />
print_r($doc);<br />
echo &#8216;&lt;/pre&gt;&#8217;;<br />
}</p>
<p>// Query for anyone 25 and under<br />
$res = $col-&gt;find(array(&#8216;age&#8217; =&gt; array(&#8216;$lte&#8217; =&gt; 25)));</p>
<p>echo &#8216;All documents &#8211; Anyone 25 or younger:&lt;br/&gt;&#8217;;</p>
<p>foreach($res as $doc)<br />
{<br />
echo &#8216;&lt;pre&gt;&#8217;;<br />
print_r($doc);<br />
echo &#8216;&lt;/pre&gt;&#8217;;<br />
}</p>
<p>// Query for anyone not 40<br />
$res = $col-&gt;find(array(&#8216;age&#8217; =&gt; array(&#8216;$ne&#8217; =&gt; 40)));</p>
<p>echo &#8216;All documents &#8211; Anyone not 40:&lt;br/&gt;&#8217;;</p>
<p>foreach($res as $doc)<br />
{<br />
echo &#8216;&lt;pre&gt;&#8217;;<br />
print_r($doc);<br />
echo &#8216;&lt;/pre&gt;&#8217;;<br />
}</p>
<p>// Query for anyone 20-50<br />
$res = $col-&gt;find(array(&#8216;age&#8217; =&gt; array(&#8216;$gte&#8217; =&gt; 20, &#8216;$lte&#8217; =&gt; 50)));</p>
<p>echo &#8216;All documents &#8211; Anyone 20-50:&lt;br/&gt;&#8217;;</p>
<p>foreach($res as $doc)<br />
{<br />
echo &#8216;&lt;pre&gt;&#8217;;<br />
print_r($doc);<br />
echo &#8216;&lt;/pre&gt;&#8217;;<br />
}<br />
[/php]</p>
<p>With MongoDB you can do &#8220;&lt;,&lt;=, &gt;, &gt;=&#8221; conditional operator based queries. Only thing is that you can&#8217;t just use &#8220;&lt;,&lt;=, &gt;, &gt;=&#8221; in your constraints, you have to use MongoDB&#8217;s version. Those are &#8220;$lt&#8221; (less than), &#8220;$lte&#8221; (less than or equal), &#8220;$gt&#8221; (greater than), &#8220;$gte&#8221; (greater than or equal). Yes, you need the dollar sign. Other conditional operators are &#8220;$ne&#8221; (not equal to), &#8220;$in&#8221; (like MySQL&#8217;s IN statement), &#8220;$nin&#8221; (NOT IN), &#8220;$mod&#8221; (Modulo operation), &#8220;$all&#8221; (like in, but all values much be matched to a document),  &#8220;$size&#8221; (Match of number of elements), &#8220;$exists&#8221; (element exists), &#8220;$type&#8221; (matches element type &#8211; int, string, etc) and there are couple more.</p>
<p><strong>Deleting a Row / Docuement</strong><br />
<em>MySQL</em></p>
<p>[php]<br />
// Delete a row<br />
$sql = &#8220;DELETE FROM `$tbl` WHERE `login` = &#8216;psmith&#8217;&#8221;<br />
mysql_query($sql, $link);<br />
[/php]</p>
<p><em>MongoDB</em></p>
<p>[php]<br />
// Delete a document<br />
$col-&gt;remove(array(&#8216;age&#8217; =&gt; 24), array(&#8216;justOne&#8217; =&gt; true, &#8216;safe&#8217; =&gt; true));</p>
<p>// Confirm delete<br />
$res = $col-&gt;find();</p>
<p>echo &#8216;All documents:&lt;br/&gt;&#8217;;</p>
<p>foreach($res as $doc)<br />
{<br />
echo &#8216;&lt;pre&gt;&#8217;;<br />
print_r($doc);<br />
echo &#8216;&lt;/pre&gt;&#8217;;<br />
}<br />
[/php]</p>
<p>Here we use the &#8220;remove&#8221; method. First argument has the constraints and the second tell it to only remove one item and also to run a &#8220;safe&#8221; query.</p>
<p><strong>Dropping a Table / Collection</strong><br />
<em>MySQL</em></p>
<p>[php]<br />
// Drop table<br />
$sql = &#8220;DROP TABLE `$db`.`$tbl`&#8221;;<br />
mysql_query($sql, $link);<br />
[/php]</p>
<p><em>MongoDB</em></p>
<p>[php]<br />
// Drop collection<br />
$col-&gt;drop();<br />
[/php]</p>
<p><strong>Dropping a database</strong><br />
<em>MySQL</em></p>
<p>[php]<br />
// Drop DB<br />
$sql = &#8220;DROP DATABASE `$db`&#8221;;<br />
mysql_query($sql, $link);<br />
[/php]</p>
<p><em>MongoDB</em></p>
<p>[php]<br />
// Drop DB<br />
$db-&gt;drop();<br />
[/php]</p>
<p><strong>Disconnect</strong><br />
<em>MySQL</em></p>
<p>[php]<br />
// Disconnect<br />
mysql_close($link);<br />
[/php]</p>
<p><em>MongoDB</em></p>
<p>[php]<br />
// Disconnect<br />
$link-&gt;close();<br />
[/php]</p>
<p>Until next time, when I will go over more stuff, happy coding.</p>
<p>Sources:<br />
<a href="http://www.php.net/manual/en/book.mongo.php" target="_blank">PHP.net</a>, <a href="http://www.mongodb.org/display/DOCS/Home" target="_blank">MongoDB</a></p>
<p><a href="http://www.dealtaker.com/blog/2010/06/22/php-mongodb-sitting-in-a-tree-part-2/">Part 2</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.dealtaker.com/our-blog/2010/05/12/php-mongodb-sitting-in-a-tree-part-1/feed/</wfw:commentRss>
		<slash:comments>31</slash:comments>
		</item>
		<item>
		<title>Kohana PHP 3.0 (KO3) Tutorial Part 8</title>
		<link>http://www.dealtaker.com/our-blog/2010/04/30/kohana-php-3-0-ko3-tutorial-part-8/</link>
		<comments>http://www.dealtaker.com/our-blog/2010/04/30/kohana-php-3-0-ko3-tutorial-part-8/#comments</comments>
		<pubDate>Fri, 30 Apr 2010 15:55:25 +0000</pubDate>
		<dc:creator>ellisgl</dc:creator>
				<category><![CDATA[DealTaker.com Updates]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[KO3]]></category>
		<category><![CDATA[kohana]]></category>
		<category><![CDATA[Modules]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.dealtaker.com/blog/?p=2169</guid>
		<description><![CDATA[Welcome to the eighth part in this series on how to develop with Kohana PHP V3 (KO3). If you haven&#8217;t read any of previous parts yet, I would search for and read them before going on. In this tutorial we will be going over modules. Before we get into views, we&#8217;ll want to update the [...]]]></description>
			<content:encoded><![CDATA[<p>Welcome to the eighth part in this series on how to develop with Kohana PHP V3 (KO3). If you haven&#8217;t read any of previous parts yet, I would search for and read them before going on</a>. In this tutorial we will be going over modules.</p>
<p><span id="more-2169"></span></p>
<p>Before we get into views, we&#8217;ll want to update the KO3 code base. Go in to &#8220;application&#8221; directory and rename your &#8220;bootstrap.php&#8221; to &#8220;bootstrap.bak.php&#8221;. Now point your browser to </a><a href="http://kohanaframework.org/download">http://kohanaframework.org/download</a> (Yes there is a new site), download the latest stable (3.0.4.2), open it and extract everything inside the &#8220;kohana&#8221; folder to &#8220;mykohana3&#8243;. Once you have done that, delete or rename the &#8220;install.php&#8221; in your &#8220;mykohana3&#8243; folder. Next, delete the  &#8220;bootstrap.php&#8221; file in the &#8220;application&#8221; folder and rename the &#8220;bootstrap.bak.php&#8221; to &#8220;bootstrap.php&#8221;.</p>
<p>So what is a module? A module is pretty much just a plug in. It can extend the core, give you whole new section to your project (IE. blogs and forums), or just give you a set of classes that are available globally. So lets jump right in!</p>
<p>Add a new directory to the &#8220;modules&#8221; directory and name it &#8220;fortune&#8221;. In the &#8220;module/fortune&#8221; directory add the following directories &#8220;classes&#8221; and &#8220;views&#8221;. In &#8220;/module/fortune/classes&#8221; create a folder named &#8220;kohana&#8221;. In &#8220;/module/fortune/views&#8221; create a folder named &#8220;fortune&#8221;. So you should have a directory that looks something like this:<br />
[php]<br />
modules<br />
+&#8211; fortune<br />
     |&#8211; classes<br />
     |    +&#8211; kohana<br />
     +&#8211; views<br />
          +&#8211; fortune<br />
[/php]<br />
Create a new file in &#8220;/module/fortune/classes/&#8221; named &#8220;fortune.php&#8221; and put the following into it:<br />
[php]<br />
&lt;?php<br />
defined(&#8216;SYSPATH&#8217;) or die(&#8216;No direct script access.&#8217;);<br />
class Fortune extends Kohana_Fortune{}<br />
[/php]<br />
This is pretty simple. Just extends another class. If you&#8217;ve gone through the code base for KO3, this will probably look familiar. Now lets create the main class file, so open a new file and put the following into it:<br />
[php]<br />
&lt;?php<br />
defined(&#8216;SYSPATH&#8217;) or die(&#8216;No direct script access.&#8217;);</p>
<p>/**<br />
 * Fortune Cookie Module<br />
 */<br />
class Kohana_Fortune<br />
 {<br />
    // Merged configuration settings<br />
    protected $config  = array(&#8216;view&#8217; =&gt; &#8216;fortune/default&#8217;);<br />
    protected $fortune = &quot;&quot;;</p>
<p>    /**<br />
     * Creates a new Fortune object.<br />
     *<br />
     * @return Forune<br />
     */<br />
    public static function factory(array $config = array())<br />
     {<br />
        return new Fortune($config);<br />
     }</p>
<p>    public function __construct(array $config = array())<br />
     {<br />
        // Overwrite system defaults with application defaults<br />
        $this-&gt;config = $this-&gt;config_group() + $this-&gt;config;<br />
     }</p>
<p>    /**<br />
     * Retrieves a fortune config group from the config file. One config group can<br />
     * refer to another as its parent, which will be recursively loaded.<br />
     *<br />
     * @param  STRING fortune config group; &quot;default&quot; if none given<br />
     * @return ARRAY  config settings<br />
     */<br />
    public function config_group($group = &#8216;default&#8217;)<br />
     {<br />
        // Load the fortune config file<br />
        $config_file     = Kohana::config(&#8216;fortune&#8217;);</p>
<p>        // Initialize the $config array<br />
        $config          = array(&#8216;group&#8217; =&gt; (string)$group);</p>
<p>        // Recursively load requested config groups<br />
        while(isset($config['group']) &amp;&amp; isset($config_file-&gt;$config['group']))<br />
         {<br />
           // Temporarily store config group name<br />
           $group   = $config['group'];<br />
           unset($config['group']);</p>
<p>           // Add config group values, not overwriting existing keys<br />
           $config += $config_file-&gt;$group;<br />
         }</p>
<p>        // Get rid of possible stray config group names<br />
        unset($config['group']);</p>
<p>        // Return the merged config group settings<br />
        return $config;<br />
     }</p>
<p>    /**<br />
     * Randomly pick a fortune<br />
     *<br />
     * @return STRING fortune text<br />
     */<br />
     public function getFortune()<br />
      {<br />
         // Stolen from: http://fortunes.cat-v.org/plan_9/<br />
         $fortunes      = array(&#8217;2 is always smaller than 3, even for large values of 2.&#8217;,<br />
                                &#8216;A penny saved is a political breakthrough.&#8217;,<br />
                                &#8216;Avoid reality at all costs.&#8217;,<br />
                                &#8216;Bad taste is timeless.&#8217;);</p>
<p>         $this-&gt;fortune = $fortunes[array_rand($fortunes, 1)];<br />
      }</p>
<p>    /**<br />
     * Renders the fortune.<br />
     *<br />
     * @param  MIXED   string of the view to use, or a Kohana_View object<br />
     * @return STRING  fortune output (HTML)<br />
     */<br />
    public function render($view = NULL)<br />
     {<br />
        if($view === NULL)<br />
         {<br />
           // Use the view from config<br />
           $view = $this-&gt;config['view'];<br />
         }</p>
<p>        if(!$view instanceof Kohana_View)<br />
         {<br />
            // Load the view file<br />
            $view = View::factory($view);<br />
         }</p>
<p>        $this-&gt;getFortune();</p>
<p>        // Pass on the whole Fortune object<br />
        return $view-&gt;set(get_object_vars($this))-&gt;set(&#8216;page&#8217;, $this)-&gt;render();<br />
     }</p>
<p>    /**<br />
     * Renders the fortune.<br />
     *<br />
     * @return STRING fortune output (HTML)<br />
     */<br />
    public function __toString()<br />
     {<br />
        return $this-&gt;render();<br />
     }<br />
 }<br />
[/php]<br />
Save this as &#8220;fortune.php&#8221; in &#8220;/modules/fortune/classes/kohana&#8221;. For the most part this looks like a simple Kohana core. We have 2 protected properties, config and fortune. The config property is for, well, a configuration. We only use the view configuration value here. The fortune property is hopefully the land that my house sits on&#8230; er&#8230; It is used to store a single fortune for our output.</p>
<p>Next we have our factory method so we can do the factory pattern. Then we have our constructor which pulls in a configuration from the config variable and recreates the config property with default configurations from a file, set array element and from what has been passed to it with the help of the next method called &#8220;config_group&#8221;.</p>
<p>The heart of this class is the getFortune method. I cheated here and used an array instead of creating a model and pulling stuff from the db. So I pull a random element and place it in our fortune property. Next up is the render method. This just runs the getFortune method and then renders the view. The last one, __toString calls the render method.</p>
<p>Create a new file and put the following into it:<br />
[php]<br />
&lt;div style=&quot;width: 250px; height: 52px; vertical-align: middle; display: table-cell; text-align: center; border: 1px solid #CCCCCC; border-right-color: #666666; border-bottom-color: #666666;&quot;&gt;<br />
  &lt;p&gt;&lt;?php echo $fortune;?&gt;&lt;/p&gt;<br />
&lt;/div&gt;<br />
[/php]<br />
Save this as &#8220;default.php&#8221; under &#8220;/modules/fortune/views/fortune/default.php&#8221;. Pretty simple. You might notice that I&#8217;m echoing out $fortune, which happens to the protected property in our class. In the render method, we pass all the properties to the view. Makes things simple.</p>
<p>Next up is to edit a couple files. Open up the &#8220;/application/bootstrap.php&#8221; file and locate the section with &#8220;Kohana::modules&#8221; in it. Now replace that with:<br />
[php]Kohana::modules(array(// &#8216;auth&#8217;       =&gt; MODPATH.&#8217;auth&#8217;,       // Basic authentication<br />
                      // &#8216;codebench&#8217;  =&gt; MODPATH.&#8217;codebench&#8217;,  // Benchmarking tool<br />
                      &#8216;database&#8217;   =&gt; MODPATH.&#8217;database&#8217;,   // Database access<br />
                      // &#8216;image&#8217;      =&gt; MODPATH.&#8217;image&#8217;,      // Image manipulation<br />
                      // &#8216;orm&#8217;        =&gt; MODPATH.&#8217;orm&#8217;,        // Object Relationship Mapping<br />
                      // &#8216;pagination&#8217; =&gt; MODPATH.&#8217;pagination&#8217;, // Paging of results<br />
                      // &#8216;userguide&#8217;  =&gt; MODPATH.&#8217;userguide&#8217;,  // User guide and API documentation<br />
                      &#8216;fortune&#8217;    =&gt; MODPATH.&#8217;fortune&#8217;,<br />
                     ));[/php]<br />
We&#8217;ve added the new fortune module and enabled it in our system here. Now open the the &#8220;/application/views/pages/ko3.php&#8221; file and add the following to the bottom of it.<br />
[php]<br />
&lt;?php echo Fortune::factory();?&gt;<br />
[/php]</p>
<p>Now if you point your browser to &#8220;http://yourserver/mykohana3/ko3/&#8221; you should see the fortune at the bottom!</p>
<p>Until next time, when I go over including external libraries, happy coding.</p>
<p><a href="http://www.dealtaker.com/blog/2010/03/26/kohana-php-3-0-ko3-tutorial-part-7/">Part 7</a> | <a href="http://www.dealtaker.com/blog/2010/06/02/kohana-php-3-0-ko3-tutorial-part-9/">Part 9</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.dealtaker.com/our-blog/2010/04/30/kohana-php-3-0-ko3-tutorial-part-8/feed/</wfw:commentRss>
		<slash:comments>24</slash:comments>
		</item>
		<item>
		<title>Kohana PHP 3.0 (KO3) Tutorial Part 7</title>
		<link>http://www.dealtaker.com/our-blog/2010/03/26/kohana-php-3-0-ko3-tutorial-part-7/</link>
		<comments>http://www.dealtaker.com/our-blog/2010/03/26/kohana-php-3-0-ko3-tutorial-part-7/#comments</comments>
		<pubDate>Fri, 26 Mar 2010 09:03:06 +0000</pubDate>
		<dc:creator>ellisgl</dc:creator>
				<category><![CDATA[DealTaker.com Updates]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[Helpers]]></category>
		<category><![CDATA[KO3]]></category>
		<category><![CDATA[kohana]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.dealtaker.com/blog/?p=2082</guid>
		<description><![CDATA[Welcome to the seventh part in this series on how to develop with Kohana PHP V3 (KO3). If you haven&#8217;t read any of previous parts yet, . In this tutorial we will be going over helpers. So what is a helper? The Kohana 2.0 documents defines helpers as: Helpers are simply â€śhandyâ€ť functions that help [...]]]></description>
			<content:encoded><![CDATA[<p>Welcome to the seventh part in this series on how to develop with Kohana PHP V3 (KO3). If you haven&#8217;t read any of previous parts yet, <a>. In this tutorial we will be going over helpers.<br />
<span id="more-2082"></span><br />
So what is a helper? The Kohana 2.0 documents defines helpers as:</a></p>
<p><a></p>
<blockquote cite="http://docs.kohanaphp.com/general/helpers"><p>Helpers are simply â€śhandyâ€ť functions that help you with development.</p>
<p>Helpers are similar to library methods, but there is a subtle difference. With a library, you have to create an instance of the library&#8217;s class to use its methods. Helpers are declared as static methods of a class, so there is no need to instantiate the class. You can think of them as â€śglobal functionsâ€ť.</p>
<p>As with libraries, the helper classes are automatically loaded by the framework when they are used, so there is no need to load them yourself.</p></blockquote>
<p></a></p>
<p><a>So pretty much they are static methods that &#8220;help&#8221; with doing common things, IE: working with arrays or URLs. Kohana comes with quite a few &#8220;helpers&#8221; prepackaged. </a><a href="http://kohanaframework.org/3.0/guide/api/" target="_blank">See the KO3 API Guide</a>.</p>
<p>So how do we make a &#8220;helper&#8221;? Fire up your IDE and let get coding. The first thing we should is create a new directory called &#8220;helper&#8221; under &#8220;/application/classes&#8221;. Create a new file and put the following into it:<br />
[php]&lt;?php<br />
class Helper_MyUrl<br />
{<br />
public static function SEOIt($str)<br />
{<br />
$str = preg_replace(array(&#8216;/s/&#8217;, &#8216;/[$.+!*'(),"]/&#8217;), array(&#8216;-&#8217;, &#8220;&#8221;), $str);<br />
return $str;<br />
}<br />
}[/php]<br />
Save to this as &#8220;myurl.php&#8221; under &#8220;/application/classes/helper&#8221;. Now open &#8220;/application/views/pages/ko3.php&#8221; and add the following to the bottom and save it.:<br />
[php]  &lt;br /&gt;&lt;?php echo Helper_MyUrl::SEOIt(&#8216;This Is a string!!!&#8217;);?&gt;[/php]<br />
So if you point your browser to &#8220;http://yourserver/mykohana3/ko3/&#8221;, you should see &#8220;ThisIsastring&#8221; at the bottom. Pretty simple!</p>
<p>Until next time, when I go over modules, happy coding.</p>
<p>Sources used: <a href="http://docs.kohanaphp.com/" target="_blank">Kohana PHP 2.x Docs</a>, <a href="http://forum.kohanaframework.org/" target="_blank">Kohana Forums</a></p>
<p><a href="http://www.dealtaker.com/blog/2010/03/03/kohana-php-3-0-ko3-tutorial-part-6/">Part 6</a> | <a href="http://www.dealtaker.com/blog/2010/04/30/kohana-php-3-0-ko3-tutorial-part-8/">Part 8</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.dealtaker.com/our-blog/2010/03/26/kohana-php-3-0-ko3-tutorial-part-7/feed/</wfw:commentRss>
		<slash:comments>21</slash:comments>
		</item>
		<item>
		<title>Kohana PHP 3.0 (KO3) Tutorial Part 5</title>
		<link>http://www.dealtaker.com/our-blog/2010/02/25/kohana-php-3-0-ko3-tutorial-part-5/</link>
		<comments>http://www.dealtaker.com/our-blog/2010/02/25/kohana-php-3-0-ko3-tutorial-part-5/#comments</comments>
		<pubDate>Thu, 25 Feb 2010 17:44:23 +0000</pubDate>
		<dc:creator>ellisgl</dc:creator>
				<category><![CDATA[DealTaker.com Updates]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[HMVC]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[KO3]]></category>
		<category><![CDATA[kohana]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.dealtaker.com/blog/?p=1991</guid>
		<description><![CDATA[Welcome to the fifth part in this series on how to develop with Kohana PHP V3 (KO3). If you haven&#8217;t read any of previous parts yet, I would search for and read them before going on. In this tutorial we will be going over the &#8220;H&#8221; in HMVC. So most of you know what MVC [...]]]></description>
			<content:encoded><![CDATA[<p>Welcome to the fifth part in this series on how to develop with Kohana PHP V3 (KO3). If you haven&#8217;t read any of previous parts yet, I would search for and read them before going on. In this tutorial we will be going over the &#8220;H&#8221; in HMVC.<br />
<span id="more-1991"></span><br />
So most of you know what MVC is, but might not know about HMVC. HMVC extends MVC by allowing a controller to call on other controllers on do stuff, or doing stuff in an Hierarchical manor. Think modules that can work by themselves or in a groups. If that didn&#8217;t help, <a href="http://techportal.ibuildings.com/2010/02/22/scaling-web-applications-with-hmvc/">check out this article about the Hierarchical-Model-View-Controller pattern</a>.</p>
<p>So we&#8217;ve been design pages with a single controller as a page, with some actions that pulls data in from a model and populates a parts of a view to give us a complete browser page. Well, that can get messy with all the model calls and extra snippets we bring into with views. Using HMVC, we can organize our code a lot better, make more things reusable and with the way Kohana PHP 3 does HMVC, we can even make our application scale by calling out to other servers to give us our content for a section.</p>
<p>Lets go ahead and make a new controller:<br />
[php]&lt;?php<br />
defined(&#8216;SYSPATH&#8217;) or die(&#8216;No direct script access.&#8217;);</p>
<p>class Controller_Hmvc extends Controller_DefaultTemplate<br />
{<br />
public function action_index()<br />
{<br />
// Set meta data<br />
$this-&gt;template-&gt;title            = &#8216;Kohana 3.0 HMVC Test&#8217;;<br />
$this-&gt;template-&gt;meta_keywords    = &#8216;PHP, Kohana, KO3, Framework, HMVC&#8217;;<br />
$this-&gt;template-&gt;meta_description = &#8216;A test of of the KO3 framework HMVC pattern&#8217;;</p>
<p>// Fill in content<br />
$ko3                              = array();<br />
$ko3['posts']                     = Request::factory(&#8216;posts/getposts&#8217;)-&gt;execute()-&gt;response;</p>
<p>$this-&gt;template-&gt;content          = View::factory(&#8216;pages/hmvc&#8217;, $ko3);<br />
}</p>
<p>}[/php]<br />
Save this as &#8220;hmvc.php&#8221; in &#8220;application/classes/controller&#8221;. The above should look pretty familiar. One line should stand out:<br />
[php]$ko3['posts'] = Request::factory(&#8216;posts/getposts&#8217;)-&gt;execute()-&gt;response;[/php]<br />
This line is where the HMVC magic happen. This will call the controller &#8220;posts&#8221; an invoke the action &#8220;getposts&#8221;, execute the code and get the response. We take the response and save it to the array which we fill the view with.</p>
<p>Talking about views, let make our view for this controller:<br />
[php]&lt;?php echo $posts;?&gt;[/php]<br />
Save this as &#8220;hmvc.php&#8221; in &#8220;application/views/pages/&#8221;</p>
<p>Now for the &#8220;posts&#8221; controller:<br />
[php]&lt;?php<br />
defined(&#8216;SYSPATH&#8217;) or die(&#8216;No direct script access.&#8217;);</p>
<p>class Controller_Posts extends Controller<br />
{<br />
public function action_index()<br />
{<br />
}</p>
<p>public function action_getposts()<br />
{<br />
// Load model<br />
$posts           = new Model_Post();</p>
<p>// Fill content array for view with last 10 posts.<br />
$content          = array();<br />
$content['posts'] = $posts-&gt;getLastTenPosts();</p>
<p>// Render and output.<br />
$this-&gt;request-&gt;response   = View::factory(&#8216;pages/hmvc_posts&#8217;, $content);<br />
}<br />
}[/php]<br />
Save this as &#8220;posts.php&#8221; in &#8220;application/classes/controller&#8221;. Once again, pretty straight forward. This controller extends the basic controller, so nifty template or anything and is pretty much what we did in the last tutorial.</p>
<p>Now for the view.<br />
[php]&lt;?php foreach($posts as $post):?&gt;<br />
&lt;h1&gt;&lt;?php echo $post['title'];?&gt;&lt;/h1&gt;<br />
&lt;?php echo $post['post'];?&gt;<br />
&lt;hr /&gt;<br />
&lt;?php endforeach;?&gt;[/php]<br />
Save this as &#8220;hmvc_posts.php&#8221; under &#8220;applications/views/pages&#8221;.</p>
<p>Now if you point your browser to: &#8220;http://yourserver/mykohana3/hmvc&#8221;, you should see a couple posts up on the screen.</p>
<p>We can achieve the same result by calling the controller from the view instead of the controller. This is helpful for keeping it simple. So one template used all over the place, you wouldn&#8217;t have to worry about having to putting in all the HMVC stuff into the controllers that used that template.</p>
<p>Change the &#8220;hmvc&#8221; controller to look like this:<br />
[php]&lt;?php<br />
defined(&#8216;SYSPATH&#8217;) or die(&#8216;No direct script access.&#8217;);</p>
<p>class Controller_Hmvc extends Controller_DefaultTemplate<br />
{<br />
public function action_index()<br />
{<br />
// Set meta data<br />
$this-&gt;template-&gt;title            = &#8216;Kohana 3.0 HMVC Test&#8217;;<br />
$this-&gt;template-&gt;meta_keywords    = &#8216;PHP, Kohana, KO3, Framework, HMVC&#8217;;<br />
$this-&gt;template-&gt;meta_description = &#8216;A test of of the KO3 framework HMVC pattern&#8217;;</p>
<p>// Fill in content<br />
$ko3                              = array();<br />
$ko3['content']                   = &#8216;Hello there!&#8217;;<br />
$this-&gt;template-&gt;content          = View::factory(&#8216;pages/hmvc&#8217;, $ko3);<br />
}<br />
}[/php]<br />
Now edit the &#8220;hmvc&#8221; view to look like this:<br />
[php]&lt;?php echo $content;?&gt;&lt;br/&gt;<br />
&lt;?php echo Request::factory(&#8216;posts/getposts&#8217;)-&gt;execute()-&gt;response;?&gt;[/php]<br />
This is example is pretty simple and we could expand this to use parameters to deliver the data in different ways, say, simple html, full html, xml, json, etc..</p>
<p>Sources used: <a href="http://kerkness.ca/wiki/doku.php" target="_blank">Unofficial Kohana 3 Wiki</a>, <a href="http://techportal.ibuildings.com/2010/02/22/scaling-web-applications-with-hmvc/" target="_blank">iBuildings: Scaling Web Applications with HMVC</a></p>
<p>&lt;&lt;<a href="http://www.dealtaker.com/our-blog/2010/02/01/kohana-php-3-0-ko3-tutorial-part-4/">Part 4</a> | <a href="http://www.dealtaker.com/our-blog/2010/03/03/kohana-php-3-0-ko3-tutorial-part-6/">Part 6</a>&gt;&gt;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dealtaker.com/our-blog/2010/02/25/kohana-php-3-0-ko3-tutorial-part-5/feed/</wfw:commentRss>
		<slash:comments>29</slash:comments>
		</item>
		<item>
		<title>Kohana PHP 3.0 (KO3) Tutorial Part 3</title>
		<link>http://www.dealtaker.com/our-blog/2009/12/30/kohana-php-3-0-ko3-tutorial-part-3/</link>
		<comments>http://www.dealtaker.com/our-blog/2009/12/30/kohana-php-3-0-ko3-tutorial-part-3/#comments</comments>
		<pubDate>Wed, 30 Dec 2009 10:08:30 +0000</pubDate>
		<dc:creator>ellisgl</dc:creator>
				<category><![CDATA[DealTaker.com Updates]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[how-to]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[KO3]]></category>
		<category><![CDATA[kohana]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.dealtaker.com/blog/?p=1873</guid>
		<description><![CDATA[Welcome to the third part in this series on how to develop with Kohana PHP V3 (KO3). If you haven&#8217;t read the first and/or second parts yet, I would click here and read them before going on. In this tutorial we will be going over how to create a template. In the last tutorial, we [...]]]></description>
			<content:encoded><![CDATA[<p>Welcome to the third part in this series on how to develop with Kohana PHP V3 (KO3). If you haven&#8217;t read the <a href="http://www.dealtaker.com/our-blog/2009/11/20/kohana-php-3-0-ko3-tutorial-part-1/" target="_blank">first</a> and/or <a href="http://www.dealtaker.com/our-blog/2009/12/07/kohana-php-3-0-ko3-tutorial-part-2/" target="_blank">second parts</a> yet, I would click here and read them before going on. In this tutorial we will be going over how to create a template.<br />
<span id="more-1873"></span><br />
In the last tutorial, we went over views and in this one we are going to extend the Controller classes which will allow us to create a template. A template, you might ask, is nothing more than a view that is more or less your base (X)HTML code. This will allow to keep things &#8220;DRY&#8221; in the view world. Before we get to putting code into a file, lets create a new directory under &#8220;/application/views/&#8221; named &#8220;templates&#8221;.  Now open up your favorite IDE and make a new file and put the following into it:<br />
[php]<br />
&lt;!DOCTYPE html PUBLIC &#8220;-//W3C//DTD XHTML 1.0 Transitional//EN&#8221;<br />
&#8220;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&#8221;&gt;<br />
&lt;html xmlns=&#8221;http://www.w3.org/1999/xhtml&#8221; xml:lang=&#8221;en&#8221; lang=&#8221;en&#8221;&gt;<br />
&lt;head&gt;<br />
&lt;meta http-equiv=&#8221;Content-type&#8221; content=&#8221;text/html; charset=UTF-8&#8243; /&gt;<br />
&lt;meta http-equiv=&#8221;Content-Language&#8221; content=&#8221;en-us&#8221; /&gt;<br />
&lt;title&gt;&lt;?php echo $title;?&gt;&lt;/title&gt;<br />
&lt;meta name=&#8221;keywords&#8221; content=&#8221;&lt;?php echo $meta_keywords;?&gt;&#8221; /&gt;<br />
&lt;meta name=&#8221;description&#8221; content=&#8221;&lt;?php echo $meta_description;?&gt;&#8221; /&gt;<br />
&lt;meta name=&#8221;copyright&#8221; content=&#8221;&lt;?php echo $meta_copywrite;?&gt;&#8221; /&gt;<br />
&lt;?php foreach($styles as $file =&gt; $type) { echo HTML::style($file, array(&#8216;media&#8217; =&gt; $type)), &#8220;n&#8221;; }?&gt;<br />
&lt;?php foreach($scripts as $file) { echo HTML::script($file, NULL, TRUE), &#8220;n&#8221;; }?&gt;<br />
&lt;/head&gt;<br />
&lt;body&gt;<br />
&lt;div id=&#8221;container&#8221;&gt;<br />
&lt;?php echo $header;?&gt;<br />
&lt;?php echo $content;?&gt;<br />
&lt;?php echo $footer;?&gt;<br />
&lt;/div&gt;<br />
&lt;/body&gt;<br />
&lt;/html&gt;<br />
[/php]<br />
Save the above as &#8220;default.php&#8221; in your &#8220;/application/views/templates/&#8221; folder.</p>
<p>As you can see the above pretty much looks like a view we have done before, a bit more expanded. I will go over the &#8220;foreach&#8221; a little bit later. Unlike a view, this will most likely be used by all of your project. Since we will point to this file for our template, or shell, this will reduce the amount of code you have to put in each view, keeping thing separated, easier to maintain, or basically &#8220;DRY&#8221;.</p>
<p>Now that we have a template, our system isn&#8217;t going to do anything with it until well tell it to use it. So lets go back to our IDE and create a new file and put the following into it:<br />
[php]<br />
&lt;?php<br />
defined(&#8216;SYSPATH&#8217;) or die(&#8216;No direct script access.&#8217;);</p>
<p>class Controller_DefaultTemplate extends Controller_Template<br />
{<br />
public $template = &#8216;templates/default&#8217;;</p>
<p>/**<br />
* Initialize properties before running the controller methods (actions),<br />
* so they are available to our action.<br />
*/<br />
public function before()<br />
{<br />
// Run anything that need ot run before this.<br />
parent::before();</p>
<p>if($this-&gt;auto_render)<br />
{<br />
// Initialize empty values<br />
$this-&gt;template-&gt;title            = &#8221;;<br />
$this-&gt;template-&gt;meta_keywords    = &#8221;;<br />
$this-&gt;template-&gt;meta_description = &#8221;;<br />
$this-&gt;template-&gt;meta_copywrite   = &#8221;;<br />
$this-&gt;template-&gt;header           = &#8221;;<br />
$this-&gt;template-&gt;content          = &#8221;;<br />
$this-&gt;template-&gt;footer           = &#8221;;<br />
$this-&gt;template-&gt;styles           = array();<br />
$this-&gt;template-&gt;scripts          = array();<br />
}<br />
}</p>
<p>/**<br />
* Fill in default values for our properties before rendering the output.<br />
*/<br />
public function after()<br />
{<br />
if($this-&gt;auto_render)<br />
{<br />
// Define defaults<br />
$styles                  = array(&#8216;assets/css/reset.css&#8217; =&gt; &#8216;screen&#8217;);<br />
$scripts                 = array(&#8216;http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js&#8217;);</p>
<p>// Add defaults to template variables.<br />
$this-&gt;template-&gt;styles  = array_reverse(array_merge($this-&gt;template-&gt;styles, $styles));<br />
$this-&gt;template-&gt;scripts = array_reverse(array_merge($this-&gt;template-&gt;scripts, $scripts));<br />
}</p>
<p>// Run anything that needs to run after this.<br />
parent::after();<br />
}<br />
}<br />
[/php]<br />
And save this as &#8220;defaulttemplate.php&#8221; under your &#8220;/application/classes/controller/&#8221; folder.</p>
<p>The above code extends the &#8220;Controller_Template&#8221; class and does three main things, initialize some properties (variables) to be accessable by our methods (actions) and appends default values to them and then gets associates them to our template variables before we render the final output to the screen. This is where the foreach() loop in the template comes into play. The foreach() loop uses a static method for the helper class &#8220;HTML&#8221; to <a href="http://v3.kohanaphp.com/guide/api/HTML#style" target="_blank">load CSS style sheets</a> and load JS files</a>. Both of these helper methods will iterate through an array and properly wrap it in the appropriate tag. They can take a path or a URL.</p>
<p>You might be wondering what a &#8220;Helper&#8221; is, so here is a quick definition from the Kohana PHP 2.x documents:</p>
<blockquote cite="http://docs.kohanaphp.com/general/helpers"><p>Helpers are simply &#8220;handy&#8221; functions that help you with development.</p>
<p>Helpers are similar to library methods, but there is a subtle difference. With a library, you have to create an instance of the library&#8217;s class to use its methods. Helpers are declared as static methods of a class, so there is no need to instantiate the class. You can think of them as &#8220;global functions&#8221;.</p>
<p>As with libraries, the helper classes are automatically loaded by the framework when they are used, so there is no need to load them yourself.</p></blockquote>
<p>OK, back on course. You might have noticed there is a reference to &#8220;assets/css/reset.css&#8221;, so lets go ahead and get that in place. In your root create a folder named &#8220;assets&#8221; and in that folder create one named &#8220;css&#8221;. For the actual &#8220;reset.css&#8221; file, I went to Serene Destiny and copied the code from the article titled &#8220;Create The Perfect CSS Reset&#8221; into a file and saved it as &#8220;reset.css&#8221; in the &#8220;assets/css/&#8221; folder. You might want to also set up some other folders within &#8220;/assets/&#8221;, maybe &#8220;images&#8221;, &#8220;js&#8221; and &#8220;files&#8221;. Basically the assets folder should be used to store and organize static files and such.</p>
<p>At this point, our application still doesn&#8217;t know what to do what we have done, so we need to modify our controller. So open &#8220;/application/classes/controller/ko3.php&#8221;.<br />
We want to change what class we are extending, so change the line:<br />
[php]class Controller_Ko3 extends Controller[/php]<br />
to:<br />
[php]lass Controller_Ko3 extends Controller_DefaultTemplate[/php]</p>
<p>We also need to change up our &#8220;index&#8221; action (action_index() method) to look like the following:<br />
[php]<br />
public function action_index()<br />
{<br />
$ko3_inner               = array();<br />
$ko3                     = array();<br />
$this-&gt;template-&gt;title   = &#8216;Kohana 3.0&#8242;;</p>
<p>View::set_global(&#8216;x&#8217;, &#8216;This is a global variable&#8217;);</p>
<p>$ko3_inner['content']    = &#8216;We have more data&#8217;;<br />
$ko3['content']          = &#8216;We have data&#8217;;<br />
$ko3['ko3_inner']        = View::factory(&#8216;blocks/ko3_inner&#8217;, $ko3_inner)<br />
-&gt;render();<br />
$this-&gt;template-&gt;content = View::factory(&#8216;pages/ko3&#8242;, $ko3);<br />
}<br />
[/php]<br />
Save it. Now you might noticed that we now have &#8220;$this-&gt;template-&gt;title   = &#8216;Kohana 3.0&#8242;;&#8221;, this will assign a value to our templates &#8220;title&#8221; variable. The next thing you might notice is the absence of the last &#8220;render()&#8221; method. The &#8220;factory()&#8221; method actually will &#8220;auto render&#8221; it to our template&#8217;s &#8220;content&#8221; variable. Pretty simple, yes?</p>
<p>There is something we should probably do before loading up the page, so load up the &#8220;ko3.php&#8221; view file located in &#8220;application/views/pages/&#8221;. You might noticed that we have all the &#8220;shell&#8221; code in our view, so lets remove it. What you should only have in the view file is this:<br />
[php]<br />
&lt;h1&gt;This is my first view&lt;/h1&gt;<br />
&lt;?php echo $content;?&gt;<br />
&lt;?php echo $ko3_inner; ?&gt;<br />
&lt;br/&gt;&lt;?php echo $x;?&gt;<br />
[/php]</p>
<p>Now if you load it up, you should see that page has a title of &#8220;Kohana 3.0&#8243; and the page should pretty much look the same as it did from the last tutorial. If you view the source thou, it would look much different. But you might be wondering about the other variables in the template and what to do with them. OK, let go back and edit our &#8220;index&#8221; action again. Make it look like:<br />
[php]<br />
public function action_index()<br />
{<br />
$ko3_inner                        = array();<br />
$ko3                              = array();<br />
$this-&gt;template-&gt;title            = &#8216;Kohana 3.0&#8242;;<br />
$this-&gt;template-&gt;meta_keywords    = &#8216;PHP, Kohana, KO3, Framework&#8217;;<br />
$this-&gt;template-&gt;meta_description = &#8216;A test of of the KO3 framework&#8217;;<br />
$this-&gt;template-&gt;styles           = array(&#8216;assets/css/red.css&#8217; =&gt; &#8216;screen&#8217;);<br />
$this-&gt;template-&gt;scripts          = array(&#8216;assets/js/jqtest.js&#8217;);</p>
<p>View::set_global(&#8216;x&#8217;, &#8216;This is a global variable&#8217;);</p>
<p>$ko3_inner['content']             = &#8216;We have more data&#8217;;<br />
$ko3['content']                   = &#8216;We have data&#8217;;<br />
$ko3['ko3_inner']                 = View::factory(&#8216;blocks/ko3_inner&#8217;, $ko3_inner)<br />
-&gt;render();<br />
$this-&gt;template-&gt;content          = View::factory(&#8216;pages/ko3&#8242;, $ko3);<br />
}<br />
[/php]<br />
Pretty simple. You will notice I didn&#8217;t fill in the header or footer. I&#8217;m sure you know what to do there. Hint: Render a view to that variable =). Another thing you might notice is that I have put in &#8220;assets/css/red.css&#8221; and &#8220;assets/css/jqtest.js&#8221;. Lets make those two files starting with &#8220;/assets/css/red.css&#8221;:<br />
[php]<br />
h1<br />
{<br />
color: #FF0000;<br />
}<br />
[/php]</p>
<p>Next &#8220;/assets/js/jqtest.js&#8221;:<br />
[php]<br />
$(&#8220;document&#8221;).ready(function()<br />
{<br />
alert(&#8216;Hello Kohana!&#8217;);<br />
});<br />
[/php]</p>
<p>Save them and refresh the site. You should see an alert pop up and the first line of text in red.</p>
<p>Today we have created a template file, extended a template controller and our controller use that template file. There is a lot of potential with what you could do with this already. So until next time when I will go over models, happy coding!<br />
Sources used: <a href="http://kerkness.ca/wiki/doku.php" target="_blank">Unofficial Kohana 3 Wiki</a>, <a href="http://docs.kohanaphp.com/" target="_blank">Kohana PHP 2.x Docs</a></p>
<p><a href="http://www.dealtaker.com/our-blog/2009/12/07/kohana-php-3-0-ko3-tutorial-part-2/">Part 2</a> | <a href="http://www.dealtaker.com/our-blog/2010/02/01/kohana-php-3-0-ko3-tutorial-part-4/">Part 4</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.dealtaker.com/our-blog/2009/12/30/kohana-php-3-0-ko3-tutorial-part-3/feed/</wfw:commentRss>
		<slash:comments>48</slash:comments>
		</item>
		<item>
		<title>Kohana PHP Tuturial &#8211; Part III</title>
		<link>http://www.dealtaker.com/our-blog/2009/06/19/kohana-php-tuturial-part-iii/</link>
		<comments>http://www.dealtaker.com/our-blog/2009/06/19/kohana-php-tuturial-part-iii/#comments</comments>
		<pubDate>Fri, 19 Jun 2009 08:26:09 +0000</pubDate>
		<dc:creator>ellisgl</dc:creator>
				<category><![CDATA[DealTaker.com Updates]]></category>
		<category><![CDATA[db]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[kohana]]></category>
		<category><![CDATA[models]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.dealtaker.com/blog/?p=1013</guid>
		<description><![CDATA[It&#8217;s been a couple weeks now, so lets get back on track with this series of tutorials. This tutorial will get into models and how to play with data. The first thing I have to is tell you is that Kohana PHP has been updated since the last tutorial. So I have gone ahead and [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s been a couple weeks now, so lets get back on track with this series of tutorials. This tutorial will get into models and how to play with data.<br />
<span id="more-1013"></span></p>
<p>The first thing I have to is tell you is that Kohana PHP has been updated since the last tutorial. So I have gone ahead and took the last tutorial and updated the core and made sure it worked with Kohana 2.3.4. <a href="http://images.dealtaker.com/dealtaker/blog/kohana/dealtaker-kohana-2-2.3.4.zip">You can grab that here</a>. Now onward to the glory!</p>
<p>The first thing we want to do is identify where and what the data is. Is it an XML feed, CSV, JSON, DB or something else? Well I&#8217;m going to make it easy. We are going to deal with our friend MySQL for this. The next step is to setup a MySQL DB connection.</p>
<p>Go into your system/config folder and copy the database.php file to your application/config folder. Edit the application/config/database.php to reflect your server settings. Mine looks like this:</p>
<pre>$config['default'] = array
(
	'benchmark'     =&gt; TRUE,
	'persistent'    =&gt; FALSE,
	'connection'    =&gt; array
	(
		'type'     =&gt; 'mysql',
		'user'     =&gt; 'root',
		'pass'     =&gt; 'root',
		'host'     =&gt; 'localhost',
		'port'     =&gt; FALSE,
		'socket'   =&gt; FALSE,
		'database' =&gt; 'myfirstkohana'
	),
	'character_set' =&gt; 'utf8',
	'table_prefix'  =&gt; '',
	'object'        =&gt; TRUE,
	'cache'         =&gt; FALSE,
	'escape'        =&gt; TRUE
);</pre>
<p>Now that we have a configuration a connection for our database, we should probably set up a database and a table. Here&#8217;s the SQL:</p>
<pre>/*!40101 SET NAMES utf8 */;
/*!40101 SET SQL_MODE=''*/;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
CREATE DATABASE /*!32312 IF NOT EXISTS*/`myfirstkohana` /*!40100 DEFAULT CHARACTER SET utf8 */;

USE `myfirstkohana`;

/*Table structure for table `posts` */
DROP TABLE IF EXISTS `posts`;

CREATE TABLE `posts` (
  `id` mediumint(8) unsigned NOT NULL auto_increment,
  `title` varchar(255) default NULL,
  `post` text,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 CHECKSUM=1 DELAY_KEY_WRITE=1 ROW_FORMAT=DYNAMIC;

/*Data for the table `posts` */
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;</pre>
<p>This is a pretty straight forward &#8220;Create a DB and one table&#8221; setup. You might notice that we have everything set for UTF8. This will match the DB configuration setting and also will allow us to deal with i18n (Internationalization) stuff later on.</p>
<p>Onward to creating an actual model. There are a couple things to keep in mind with naming conventions, well really one. From the docs &#8220;The model class name is capitalized, does have _Model appended to it and should be the singular form of the name.&#8221;. There are so other rules when you are dealing with ORM, but we won&#8217;t be dealing with ORM in this tutorial.</p>
<p>Create a file named post.php in the &#8216;application/models/&#8217; folder and make it look like the following:</p>
<pre>&lt;?php
defined('SYSPATH') or die('No direct script access.');

class Post_Model extends Model
 {
    public function __construct()
    	{
		      parent::__construct();
    	}

 }</pre>
<p>As you can tell the above really doesn&#8217;t do much, so let&#8217;s give it some functionality:</p>
<pre>&lt;?php
defined('SYSPATH') or die('No direct script access.');

class Post_Model extends Model
 {
    public function __construct()
    	{
		      parent::__construct();
    	}

    public function getPosts()
     {
        $sql = 'SELECT *
                FROM   `posts`
                LIMIT  0, 10';

        return $this-&gt;db-&gt;query($sql);
     }
 }</pre>
<p>So we have a model method that pulls 10 posts from the table with a pretty simple query. We use &#8220;$this-&gt;query()&#8221; to run queries, which will return object and we return that to the calling entity. Check the query method <a href="http://docs.kohanaphp.com/libraries/database/query" target="_blank">docs here</a> for more information.</p>
<p>Let&#8217;s update our &#8216;Hello&#8217; controller to use this the model.</p>
<p>In your application/controllers/hello.php update your index() method to look like this:</p>
<pre>    public function index()
     {
        // Load the models
        $post  = new Post_Model;
        $posts = $post-&gt;getPosts();
        $rpsts = "";

        // Loop thru the posts
        foreach($posts-&gt;result_array(FALSE) as $row)
         {
          // Simple output of
          $rpsts .= '
    &lt;h1&gt;'.$row['title'].'&lt;/h1&gt;
    '.$row['post'].'&lt;hr /&gt;';
         }

        // Put something useful in our variables.
        $this-&gt;template-&gt;header-&gt;pageTitle .= ' ::: I am on the top';
        $this-&gt;template-&gt;content-&gt;content   = $rpsts;
     }</pre>
<p>When we run the hello controller, we won&#8217;t get much but &#8216;This is my second view&#8217;. We need to put stuff in the table. Use your favorite method of accessing your DB and insert some rows. Now when you run it you will see stuff!</p>
<p>If you notice there is a &#8220;$post-&gt;result_array()&#8221; inside a foreach loop. This allows us to loop though the results of the query easily from within the controller.</p>
<p>You might have notice something of bad practice. I pretty much created HTML inside the controller. As we all know, we shouldn&#8217;t do this. Let&#8217;s fix this!</p>
<p>Create a new view named main_posts.php and make it look like:</p>
<pre>&lt;?php foreach($posts as $post): ?&gt;
&lt;h1&gt;&lt;?php echo $post['title'];?&gt;&lt;h1&gt;
&lt;?php echo $post['post'];?&gt;&lt;hr /&gt;
&lt;?php endforeach; ?&gt;</pre>
<p>This view does a foreach on our query results and fills in our little view.</p>
<p>In the the index method of our controller we will need to change up the controller to use our new view.</p>
<pre>   public function index()
     {
        // Load the models
        $post  = new Post_Model;
        $posts = $post-&gt;getPosts();

        // Put something useful in our variables.
        $this-&gt;template-&gt;header-&gt;pageTitle .= ' ::: I am on the top';

        // Posts view
        $this-&gt;template-&gt;content-&gt;content        = new View('main_posts');
        $this-&gt;template-&gt;content-&gt;content-&gt;posts = $posts-&gt;result_array(FALSE);
     }</pre>
<p>As you can tell, it&#8217;s pretty simple and it&#8217;s clean! Here&#8217;s a quick sample on how to do an insert. We are going to add a method to the post model called addPost.</p>
<pre>  public function addPost($title, $post)
    {
       $sql = sprintf('INSERT INTO `posts`
                       SET         `title` = %s,
                                   `post`  = %s',
                       $this-&gt;db-&gt;escape($title),
                       $this-&gt;db-&gt;escape($post));
      $this-&gt;db-&gt;query($sql);
    }</pre>
<p>We need to add a method to handle adding of posts to our hello controller:</p>
<pre>   public function addpost()
     {
        // Load the models
        $post  = new Post_Model;
        $post-&gt;addPost($_POST['title'], $_POST['post']);
        url::redirect('hello');
     }</pre>
<p>And lets add a form to the end of the main_post.php in the views:</p>
<pre>&lt;form method="POST" action="&lt;?php echo url::base();?&gt;hello/addpost/"&gt;
  &lt;table&gt;
    &lt;tr&gt;
     &lt;th&gt;
       Title
     &lt;/th&gt;
     &lt;th&gt;
       Post
     &lt;/th&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
     &lt;td&gt;
      &lt;input type="text" name="title" /&gt;
     &lt;/td&gt;
     &lt;td&gt;
      &lt;textarea cols="20" rows="5" name="post"&gt;&lt;/textarea&gt;
      &lt;input type="submit" name="submit" value="Submit"/&gt;
     &lt;/td&gt;
  &lt;/table&gt;
&lt;/form&gt;</pre>
<p>Once again, pretty simple right? I could go on and give you how to edit entries, but I&#8217;m leaving that to you for your home work. Free free to post your results! Until next time when will go over libraries and helpers, keep on coding till your fingers bleed!</p>
<p>If you enjoyed this tutorial, please check out our <a href="http://www.dealtaker.com/tech-deals.html" target="_blank">tech deals</a> and <a href="http://www.dealtaker.com/Computer-store-coupons-10.html" target="_blank">Computer related coupons</a>.</p>
<p><a href="http://images.dealtaker.com/dealtaker/blog/kohana/dealtaker-kohana-3.zip">Get the file for this tutorial here.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.dealtaker.com/our-blog/2009/06/19/kohana-php-tuturial-part-iii/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>Kohana PHP 2.3.x Tutorial Part II</title>
		<link>http://www.dealtaker.com/our-blog/2009/05/21/kohana-php-23x-tutorial-part-ii/</link>
		<comments>http://www.dealtaker.com/our-blog/2009/05/21/kohana-php-23x-tutorial-part-ii/#comments</comments>
		<pubDate>Thu, 21 May 2009 09:51:27 +0000</pubDate>
		<dc:creator>ellisgl</dc:creator>
				<category><![CDATA[DealTaker.com Updates]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[kohana]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[templates]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[views]]></category>

		<guid isPermaLink="false">http://www.dealtaker.com/blog/?p=633</guid>
		<description><![CDATA[Welcome to the second tutorial in this series on how to develop with Kohana PHP. If you haven&#8217;t read the first part, I would read Part 1 before going on. In this tutorial we will be going over views, wonderful views, glorious views. Let&#8217;s just dive straight into building our first picturesque view. Create the [...]]]></description>
			<content:encoded><![CDATA[<p>Welcome to the second tutorial in this series on how to develop with Kohana PHP. If you haven&#8217;t read the first part, I would <a href="http://www.dealtaker.com/blog/2009/04/23/kohana-php-23x-tutorial-part-1/">read Part 1</a> before going on. In this tutorial we will be going over views, wonderful views, glorious views.<br />
<span id="more-633"></span></p>
<p>Let&#8217;s just dive straight into building our first picturesque view.<br />
Create the file &#8220;myfirstkohana/application/views/hello.php&#8221; and put the following into it:</p>
<pre>&lt;html&gt;
 &lt;head&gt;
  &lt;title&gt;Hello!&lt;/title&gt;
 &lt;/head&gt;
 &lt;body&gt;
  &lt;h1&gt;This is my first view&lt;/h1&gt;
  &lt;?php echo $content;?&gt;
 &lt;/body&gt;
&lt;/html&gt;</pre>
<p>Looking at the above, it&#8217;s pretty straight forward and simple HTML file with some simple PHP code thrown in.</p>
<p>Just because we put a view with the same name as our controller, it&#8217;s not going to be shown until we say so. Let&#8217;s go back to our  wonderful controller &#8220;myfirstkohana/application/controllers/hello.php&#8221; and edit the index method to look like the following:</p>
<pre>public function index()
 {
  $view          = new View('hello');
  $view-&gt;content = 'Hello World!';
  $view-&gt;render(TRUE);
 }</pre>
<p>Let go through this line by line. The first line:</p>
<pre>$view = new View('hello');</pre>
<p>tells the framework to create a new &#8220;view&#8221; object and load the file :myfirstkohana/application/views/hello.php&#8221; into it. The next line:</p>
<pre>$view-&gt;content = 'Hello World!';</pre>
<p>creates a new property called &#8220;content&#8221; in the object that has the value &#8220;Hello World!&#8221;. The last line</p>
<pre>$view-&gt;render(TRUE);</pre>
<p>compiles the template with our assigned values and outputs it to the screen. What we should get when we load &#8220;http://youserver/myfirstkohana/hello&#8221; is &#8220;This is my first view&#8221; and below that should read &#8220;Hello World!&#8221;.</p>
<p>This might the simplist view you&#8217;ll see. Of course, I don&#8217;t think we&#8217;ll be wanting something so simple, so lets knock it up notch and create views with nested views.</p>
<p>Create the file &#8220;myfirstkohana/application/views/default_template.php&#8221; and put this in it:</p>
<pre>&lt;?php echo $header; ?&gt;
&lt;?php echo $content; ?&gt;
&lt;?php echo $footer; ?&gt;</pre>
<p>The above code is just a master template, or the template of templates if you will. Next lets create &#8220;myfirstkohana/application/views/default_header.php&#8221; and put the following into it:</p>
<pre>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" &gt;
 &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt;
  &lt;title&gt;&lt;?php echo $pageTitle;?&gt;&lt;/title&gt;
 &lt;/head&gt;</pre>
<p>and then &#8220;myfirstkohana/application/views/default_content.php&#8221;:</p>
<pre> &lt;body&gt;
  &lt;h1&gt;This is my second view&lt;/h1&gt;
  &lt;?php echo $content;?&gt;</pre>
<p>finally &#8220;myfirstkohana/application/views/default_footer.php&#8221;:</p>
<pre> &lt;/body&gt;
&lt;/html&gt;</pre>
<p>Above we taken a basic HTML page and split it up &#8220;logically&#8221; into a header, footer and a content section. Let open our controller to build out our more complex view.  Edit &#8220;myfirstkohana/application/controllers/hello.php&#8221; to make the index method look like this:</p>
<pre>  public function index()
   {
    $view                    = new View('default_template');
    $view-&gt;header            = new View('default_header');
    $view-&gt;content           = new View('default_content');
    $view-&gt;footer            = new View('default_footer');
    $view-&gt;header-&gt;pageTitle = 'I am on the top';
    $view-&gt;content-&gt;content  = 'Hello World!';

    $view-&gt;render(TRUE);
   }</pre>
<p>That wasn&#8217;t too hard was it? Basically we load our first view, which is the main template. Then we populate our properties with objects of our inner templates. It&#8217;s like working with multi-demential arrays in a sense.</p>
<p>With the ability to nest views/templates inside each other, things are a lot nicer to deal with. There is a problem thou. Each method (action) that needs to have a templated view, you need to do what we did to our index action to each one. Kohana does have an answer.</p>
<p>The Template_Controller allows to give a default view to actions, which if you have a lot of actions for a controller, it can really cut down on development and debugging time. Let go ahead and re-work out the hello controller (&#8220;myfirstkohana/application/controllers/hello.php&#8221;).</p>
<pre>&lt;?php
defined('SYSPATH') or die('No direct access allowed.');

class Hello_Controller extends Template_Controller
 {
    public $template    = 'default_template'; // Default template (view) to use
    public $auto_render = TRUE;               // Auto render template after controller is done

    public function __call($method, $arguments)
     {
        $this-&gt;name($method);
     }

    public function __construct()
     {
        parent::__construct(); // Has to be here!

        // Load the inner templates
        $this-&gt;template-&gt;header            = new View('default_header');
        $this-&gt;template-&gt;content           = new View('default_content');
        $this-&gt;template-&gt;footer            = new View('default_footer');
        $this-&gt;template-&gt;header-&gt;pageTitle = 'My first Kohana App';
     }

    public function index()
     {
        // Put something useful in our variables.
        $this-&gt;template-&gt;header-&gt;pageTitle .= ' ::: I am on the top';
        $this-&gt;template-&gt;content-&gt;content   = 'Hello World!';
     }

    public function america()
     {
        $this-&gt;template-&gt;content-&gt;content = 'Hello America!';
     }

    public function name($name)
     {
        $this-&gt;template-&gt;content-&gt;content = 'Hello ',$name,'!';
     }
 }</pre>
<p>Here we have defaulted our main template, so now we didn&#8217;t have to declare it in each method. Also in our constructor method, we loaded the sub templates in and gave a default value to one of the template variable that used in the title tag. In our index method we added on to the title tag (think SEO) and put some generic content in place.</p>
<p>Stay tuned for the third part of our series and find out about models and working with external data!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dealtaker.com/our-blog/2009/05/21/kohana-php-23x-tutorial-part-ii/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Kohana PHP 2.3.x Tutorial Part 1</title>
		<link>http://www.dealtaker.com/our-blog/2009/04/23/kohana-php-23x-tutorial-part-1/</link>
		<comments>http://www.dealtaker.com/our-blog/2009/04/23/kohana-php-23x-tutorial-part-1/#comments</comments>
		<pubDate>Thu, 23 Apr 2009 09:22:25 +0000</pubDate>
		<dc:creator>ellisgl</dc:creator>
				<category><![CDATA[DealTaker.com Updates]]></category>
		<category><![CDATA[controllers]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[install]]></category>
		<category><![CDATA[kohana]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.dealtaker.com/blog/?p=558</guid>
		<description><![CDATA[So after reading my article about frameworks, you might want to try one out. Today we are going start a series of tutorials on how to use the Kohana PHP framework beginning with how to install it and creating a controller. Before we get into this tutorial, lets go over what will be required. *AMP [...]]]></description>
			<content:encoded><![CDATA[<p>So after reading <a href="http://www.dealtaker.com/blog/2009/02/10/frameworks-frameworks-frameworks/">my article about frameworks</a>, you might want to try one out. Today we are going start a series of tutorials on how to use the Kohana PHP framework beginning with how to install it and creating a controller.<br />
<span id="more-558"></span></p>
<p>Before we get into this tutorial, lets go over what will be required.</p>
<ul>
<li>*AMP (Apache MySQL PHP) install</li>
<li>Knowledge of PHP</li>
<li>Know what a frame work is (<a href="http://www.dealtaker.com/blog/2009/02/10/frameworks-frameworks-frameworks/">Framework Article</a>)</li>
<li>Know what MVC is (<a href="http://en.wikipedia.org/wiki/Model-view-controller">Wikipedia Entry</a>)</li>
</ul>
<p>Lets get started!<br />
Download:<br />
Load <a href="http://kohanaframework.org//">http://www.kohanaphp.com</a> in your favorite browser and click the Download Kohana! button. You should be prompted to save a file. Go a head and save the file some where. At the time of this writing, the version was at 2.3.2.</p>
<p>Install:<br />
Open the file we just downloaded in your favorite archive program and extract it to a temporary location. Open that temporary location and you should have a folder that is named &#8220;Kohana_v2.3.2&#8243; or something like that. Open that folder. Open a new window and open the root directory of your *AMP install. Since I&#8217;m using WAMP Server &#8211; mine is C:wampwww. Next make a new folder in there named &#8220;myfirstkohana&#8221;. Copy the files from the &#8220;Kohana_v2.3.2&#8243; directory to the &#8220;myfirstkohana&#8221;.  Make sure your *AMP installation is up and running then point your browser to http://yourserver/myfirstkohana/ . You should have a screen stating that everything is &#8220;OK&#8221;.<br />
<img src="http://images.dealtaker.com/dealtaker/blog/kohana/kohana-1-1.png" alt="" /></p>
<p>If everything is &#8220;OK&#8221;, then remove or rename the &#8220;install.php&#8221; file in the &#8220;myfirstkohana&#8221; directory. Next we want to navigate into the application folder, then in the the config folder. Open the file named &#8220;config.php&#8221; in your favorite editor. There should be a line that reads: &#8220;$config['site_domain'] = &#8216;/kohana/&#8217;;&#8221; (Line 7 for me). We need to change the &#8220;/kohana/&#8221; part to reflect our install. So replace &#8220;/kohana/&#8221; with &#8220;/myfirstkohana/&#8221;. Also search for &#8220;$config['index_page'] = &#8216;index.php&#8217;;&#8221; and make the &#8220;index.php&#8221; part blank, so that line (Line 21 for me) should now read &#8220;$config['index_page'] = &#8221;;&#8221;.  Save that file.</p>
<p>Create a new document, cut and paste the following into it:</p>
<pre># Turn on URL rewriting
RewriteEngine On

# Put your installation directory here:
# If your URL is www.example.com/, use /
# If your URL is www.example.com/kohana/, use /kohana/
RewriteBase /myfirstkohana/

# Do not enable rewriting for files or directories that exist
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# For reuests that are not actual files or directories,
# Rewrite to index.php/URL
RewriteRule ^(.*)$ index.php/$1 [PT,L]</pre>
<p>Save this file in the &#8220;myfirstkohana&#8221; directory as &#8220;.htaccess&#8221;.</p>
<p>These changes will help with creating clean URLs that help with SEO efforts.</p>
<p>Reload the browser page and you should get something like this:<br />
<img src="http://images.dealtaker.com/dealtaker/blog/kohana/kohana-1-2.png" alt="" /></p>
<p>If everything looks good, then we have successfully installed Kohana PHP.  Now lets make it do stuff!</p>
<p>My first controller:<br />
Open a new document and put the following into it:</p>
<pre>&lt;?php
defined('SYSPATH') or die('No direct access allowed.');

class Hello_Controller extends Controller
 {
  public function index()
   {
    echo 'Hello World!';
   }
 }</pre>
<p>Save this in &#8220;myfirstkohana/application/controllers&#8221; as &#8220;hello.php&#8221;. Here&#8217;s a little explaination of what we did. The line &#8220;defined(&#8216;SYSPATH&#8217;) or die(&#8216;No direct access allowed.&#8217;);&#8221; basically prevents the file from being executed by itself. The next line &#8220;class Hello_Controller extends Controller&#8221; creates a class called &#8220;Hello_Controller&#8221; that extends the controller object. The rules to follow for creating controllers is as follows:</p>
<ul>
<li>Must reside in a controllers (sub-)directory</li>
<li>Controller filename must be lowercase, e.g. articles.php</li>
<li>Controller class must map to filename and capitalized, and must be appended with _Controller, e.g. Articles_Controller</li>
<li>Must have the Controller class as (grand)parent</li>
<li>Controller methods preceded by &#8216;_&#8217; (e.g. _do_something() ) cannot be called by the URI mapping</li>
</ul>
<p>stolen from: <a href="http://docs.kohanaphp.com/general/controllers">Kohana Docs</a></p>
<p>The first method, &#8220;index&#8221; is the default action. Pretty easy right? If you point your browser to &#8220;http://yourserver/myfirstkohana/hello/&#8221; you should see &#8220;Hello World!&#8221; on the screen now.</p>
<p>What if you wanted to exend the controller to do something else? Let add another method in the class that looks like:</p>
<pre>public function america()
 {
  echo 'Hello America!';
 }</pre>
<p>Now the hello.php should look like:</p>
<pre>&lt;?php
defined('SYSPATH') or die('No direct access allowed.');

class Hello_Controller extends Controller
 {
  public function index()
   {
    echo 'Hello World!';
   }

  public function america()
   {
    echo 'Hello America!';
   }
 }</pre>
<p>Save this and open &#8220;http://yourserver/hello/america/&#8221;. Notice that is now show &#8220;Hello America!&#8221; on the screen. That wasn&#8217;t too hard, but lets make this a little more dynamic with arguements. Make a new method that will look like this:</p>
<pre>public function name($name)
 {
  echo 'Hello ',$name,'!';
 }</pre>
<p>Load up &#8220;http://yourserver/hello/name/monkey&#8221; and you should see &#8220;Hello monkey!&#8221; on your screen. Pretty straight forward. Let make it even more &#8216;friendly&#8217;. Add this to the top of the class:</p>
<pre>public function __call($method, $arguments)
 {
  $this-&gt;name($method);
 }</pre>
<p>If we load up &#8220;http://yourserver/myfirstkohana/hello/MrGuy/&#8221; we should get the same thing as if we loaded &#8220;http://yourserver/myfirstkohana/hello/name/MrGuy/&#8221; which would be &#8220;Hello MrGuy!&#8221;.</p>
<p>That&#8217;s it for this lesson. Next time we&#8217;ll be looking over Views.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dealtaker.com/our-blog/2009/04/23/kohana-php-23x-tutorial-part-1/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
	</channel>
</rss>

