<?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>andre bluehs &#187; mysql</title>
	<atom:link href="http://andrebluehs.net/blog/tag/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://andrebluehs.net/blog</link>
	<description>nerdy, webby, smelly?</description>
	<lastBuildDate>Wed, 25 May 2011 23:35:25 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>CakePHP and Events with AJAX and MySQL (Part 1)</title>
		<link>http://andrebluehs.net/blog/2009/12/cakephp-and-events-with-ajax-and-mysql-part-1/</link>
		<comments>http://andrebluehs.net/blog/2009/12/cakephp-and-events-with-ajax-and-mysql-part-1/#comments</comments>
		<pubDate>Tue, 15 Dec 2009 23:15:31 +0000</pubDate>
		<dc:creator>Andre</dc:creator>
				<category><![CDATA[Webby]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[cakephp]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[pretty things]]></category>
		<category><![CDATA[web dev]]></category>

		<guid isPermaLink="false">http://andrebluehs.net/blog/?p=125</guid>
		<description><![CDATA[I recently had a customer that needed an event display system. (They&#8217;re not paying me, and non-profit, so I feel ok putting this tutorial here). They wanted to have a nice pretty calendar that displayed when certain events happened. I had also decided early on to use CakePHP and had a fairly fleshed out site [...]]]></description>
			<content:encoded><![CDATA[<p>I recently had a customer that needed an event display system. (They&#8217;re not paying me, and non-profit, so I feel ok putting this tutorial here). They wanted to have a nice pretty calendar that displayed when certain events happened. I had also decided early on to use CakePHP and had a fairly fleshed out site at this point, so I just wanted to add in another MVC that I could use. This is actually quite easy, thought it takes some setting up, so here is how I did it.<br clear='none'><br clear='none'></p>
<p>NOTE: This tutorial assumes you have an <a href="http://book.cakephp.org/view/641/Simple-Acl-controlled-Application">Acl controlled application</a>, and know how and when to get these things to work. I will explain when things need to be done with ARO/ACO, but not how. Also, I borrowed the design heavily from <a href="http://www.stefanoverna.com/log/create-astonishing-ical-like-calendars-with-jquery">this calendar tutorial</a><br clear='none'><br clear='none'></p>
<p><strong>Step 0:</strong> Setting up the DB<br clear='none'><br clear='none'></p>
<p>This database structure provides a very simplistic event. The event has a title, body (description), date, and ID, and if it reoccurs. You may need less or more things, this will be left to your discretion, this is just to get you started.</p>
<pre>CREATE TABLE IF NOT EXISTS `events` (
  `id` int(11) NOT NULL auto_increment,
  `title` varchar(55) NOT NULL,
  `body` text,
  `date` date NOT NULL,
  `recurring` text,
  PRIMARY KEY  (`id`),
  KEY `title` (`title`)
)</pre>
<p><br clear='none'><br />
<strong>Step 1:</strong> The Model<br clear='none'><br clear='none'></p>
<p>I didn&#8217;t need anything at all in my model, so I just stole the first few lines from another one and put it there. I&#8217;ll put mine here simply for completeness.</p>
<pre>class Event extends AppModel {

	var $name = 'Event';

}</pre>
<p><br clear='none'><br />
<strong>Step 2:</strong> Setting up the controller<br clear='none'><br clear='none'></p>
<p>This won&#8217;t get in to ACL/ACO/ARO at all, but a hand-waving explanation of how-to-make-it-work. Let&#8217;s stub out the controller with what we will eventually need: some setup and 4 methods.</p>
<pre>class EventsController extends AppController {

	var $name = 'Events';
	var $helpers = array('Html', 'Form', 'Ajax', 'Javascript');

	function beforeFilter() {
	    parent::beforeFilter();
	    $this-&gt;Auth-&gt;allowedActions = array('index', 'display');
	}

       	function index(){
        }

	function add($date = ""){
        }

	function delete($id){
        }

	function edit($id = null) {
        }</pre>
<p>This will be what we are mainly going to  work on. The way mine functions is that there is no way to view an individual event&#8217;s details by clicking on it. You could add this in by including a view() method and displaying the info. That will be left as an exercise to the reader.<br clear='none'><br clear='none'></p>
<p><strong>Step 3:</strong> Setting up the index() method<br clear='none'><br clear='none'></p>
<p>What we need to do here is to get all the events within a certain range (the current month) and to display them out AJAX style. CakePHP makes this trivially easy.</p>
<pre>	Configure::write('debug', 0);
	$month = $_POST['m'];
	if ($month &lt; 10) $month = "0".$month;
	$year = $_POST['y'];

	$max = cal_days_in_month(CAL_GREGORIAN, $month, $year);
	$min = "01";

	$this-&gt;set('events', $this-&gt;Event-&gt;find('all', array(
							'conditions' =&gt; array("Event.date BETWEEN '$year-$month-$min' AND '$year-$month-$max'"))));
	$this-&gt;set('month', $month);
	$this-&gt;set('year', $year);

	$this-&gt;layout = "ajax";</pre>
<p>Explanation:<br clear='none'></p>
<pre>Configure::write('debug', 0)</pre>
<p>- because we&#8217;re using AJAX, we don&#8217;t want any pesky error messages (or if still in debugging, db calls) to show up.</p>
<pre>if ($month &lt; 10) $month = "0".$month;</pre>
<p>- later we&#8217;ll see javascript gives us the month without a leading 0. this is to put it back.</p>
<pre>cal_days_in_month(CAL_GREGORIAN, $month, $year);</pre>
<p>- a handy function to give us&#8230; exactly what it says. alternative to date(&#8220;t&#8221;, mktime(&#8230;.));<br clear='none'><br clear='none'></p>
<p>Part 2 to happen shortly<br clear='none'></p>
<ul>
<li>Ajax-ing in the content</li>
<li>Javascript Date() vs PHP date()</li>
<li>Correctly padding days before first of the month</li>
</ul>
 <img src="http://andrebluehs.net/blog/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=125" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://andrebluehs.net/blog/2009/12/cakephp-and-events-with-ajax-and-mysql-part-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

