<?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>House of Metnetsky &#187; php</title>
	<atom:link href="http://cowarthill.com/blog/index.php/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://cowarthill.com/blog</link>
	<description>Run Tortoise Run</description>
	<lastBuildDate>Fri, 26 Feb 2010 17:15:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Programming Tutorial: Covering All Your Bases</title>
		<link>http://cowarthill.com/blog/index.php/2009/03/25/programming-tutorial-covering-all-your-bases/</link>
		<comments>http://cowarthill.com/blog/index.php/2009/03/25/programming-tutorial-covering-all-your-bases/#comments</comments>
		<pubDate>Wed, 25 Mar 2009 13:30:21 +0000</pubDate>
		<dc:creator>MET</dc:creator>
				<category><![CDATA[humor]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://cowarthill.com/blog/?p=252</guid>
		<description><![CDATA[Make sure you cover all your bases when reseting variables: if ($flag == &#34;yes&#34;) { $form-&#62;creationDate = null; $form-&#62;runCount = 0; $form-&#62;statusCode = 1; } else { $form-&#62;creationDate = null; $form-&#62;runCount = 0; $form-&#62;statusCode = 1; } Discovered by @zewillow]]></description>
			<content:encoded><![CDATA[<p>Make sure you cover all your bases when reseting variables:</p>
<pre class="brush: php;">
if ($flag == &quot;yes&quot;) {
	$form-&gt;creationDate = null;
	$form-&gt;runCount = 0;
	$form-&gt;statusCode = 1;
} else {
	$form-&gt;creationDate = null;
	$form-&gt;runCount = 0;
	$form-&gt;statusCode = 1;
}
</pre>
<p>Discovered by <a href="http://twitter.com/zewillow" target="_blank">@zewillow</a></p>
]]></content:encoded>
			<wfw:commentRss>http://cowarthill.com/blog/index.php/2009/03/25/programming-tutorial-covering-all-your-bases/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Programming Tutorial: Readability</title>
		<link>http://cowarthill.com/blog/index.php/2009/03/18/programming-tutorial-readability/</link>
		<comments>http://cowarthill.com/blog/index.php/2009/03/18/programming-tutorial-readability/#comments</comments>
		<pubDate>Wed, 18 Mar 2009 17:01:32 +0000</pubDate>
		<dc:creator>MET</dc:creator>
				<category><![CDATA[humor]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://cowarthill.com/blog/?p=213</guid>
		<description><![CDATA[Readability is always more important than simple one-liners: if ($form-&#62;minute == 0) { $minute = &#34;00&#34;; } else if ($form-&#62;minute &#60; 10) { $minute = &#34;0&#34; . $form-&#62;minute; } else { $minute = $form-&#62;minute; } Discovered by @zewillow.]]></description>
			<content:encoded><![CDATA[<p>Readability is always more important than simple one-liners:</p>
<pre class="brush: php;">
if ($form-&gt;minute == 0) {
	$minute = &quot;00&quot;;
} else if ($form-&gt;minute &lt; 10) {
	$minute = &quot;0&quot; . $form-&gt;minute;
} else {
	$minute = $form-&gt;minute;
}
</pre>
<p>Discovered by <a href="http://twitter.com/zewillow" target="_blank">@zewillow</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://cowarthill.com/blog/index.php/2009/03/18/programming-tutorial-readability/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Programming Tutorial: Variable Initialization</title>
		<link>http://cowarthill.com/blog/index.php/2009/03/17/programming-tutorial-variable-initialization/</link>
		<comments>http://cowarthill.com/blog/index.php/2009/03/17/programming-tutorial-variable-initialization/#comments</comments>
		<pubDate>Tue, 17 Mar 2009 19:25:19 +0000</pubDate>
		<dc:creator>MET</dc:creator>
				<category><![CDATA[humor]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://cowarthill.com/blog/?p=207</guid>
		<description><![CDATA[Always make sure your variables are properly initialized: $list = $_SESSION['list']; if ($list == null &#124;&#124; $list == &#34;&#34;) { $list = $_SESSION['list']; } Discovered by @zewillow]]></description>
			<content:encoded><![CDATA[<p>Always make sure your variables are properly initialized:</p>
<pre class="brush: php;">
$list = $_SESSION['list'];

if ($list == null || $list == &quot;&quot;) {
	$list = $_SESSION['list'];
}
</pre>
<p>Discovered by <a href="http://twitter.com/zewillow" target="_blank">@zewillow</a></p>
]]></content:encoded>
			<wfw:commentRss>http://cowarthill.com/blog/index.php/2009/03/17/programming-tutorial-variable-initialization/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP Annoyances: header()</title>
		<link>http://cowarthill.com/blog/index.php/2009/02/10/php-annoyances-header/</link>
		<comments>http://cowarthill.com/blog/index.php/2009/02/10/php-annoyances-header/#comments</comments>
		<pubDate>Wed, 11 Feb 2009 04:04:23 +0000</pubDate>
		<dc:creator>MET</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[flex]]></category>
		<category><![CDATA[http]]></category>
		<category><![CDATA[kmbs]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://cowarthill.com/blog/?p=138</guid>
		<description><![CDATA[Earlier today, and yesterday in fact, I was successfully downloading generated PDF&#8217;s from a FLEX application from an Apache2 server via mod_php5. Tonight however, it kept failing with an IOErrorEvent #2038. There&#8217;s lots of fun references to this issue like this one. Sadly downloading via FileReference doesn&#8217;t trigger the HTTPStatusEvent event, so I can&#8217;t lie [...]]]></description>
			<content:encoded><![CDATA[<p>Earlier today, and yesterday in fact, I was successfully downloading generated PDF&#8217;s from a FLEX application from an Apache2 server via mod_php5. Tonight however, it kept failing with an IOErrorEvent #2038. There&#8217;s lots of fun references to this issue like this <a target="_blank">one</a>. Sadly downloading via FileReference doesn&#8217;t trigger the HTTPStatusEvent event, so I can&#8217;t lie to FLEX.</p>
<p>I eventually stopped brute forcing an attempt and intelligently looked at Apache2&#8242;s logs and found this wonderful line:</p>
<p><em style="font-size:6">10.10.220.130 &#8211; - [10/Feb/2009:20:24:16 -0500] &#8220;GET /vrm/Create_Report.php?download=vrm_report_17_10-10-220-130.pdf HTTP/1.1&#8243; 1 68856 &#8220;-&#8221; &#8220;Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; &#8230;)&#8221;</em></p>
<p>Do you notice whats wrong? No? Look for the 1 after &#8220;HTTP/1.1&#8243; and before the &#8220;68856&#8243; &#8211; this is where the HTTP Status Code should be. Eh? What happened? Where&#8217;d my status go? The server-side looked like this:</p>
<pre class="brush: php;">
if (file_exists($file)) {
	header('Content-Type', 'application/octet-stream', true);
	header('Content-Disposition', 'attachment/filename=' . $filename, true);
	header('Content-Length', filesize($file), true);
	header('Content-Description', 'File Transfer');
	readfile($file);
}
</pre>
<p>Any one notice the issue &#8211; header is being use inappropriately. The first argument should be the entire response header, the second argument is the option to override a matching header, and the third is the status code. &#8220;true&#8221; evaluates &#8220;1&#8243; and thus the status of 1. You might ask as I am right now: Why is he divulging that he&#8217;s an idiot? Well folks it goes like this:</p>
<p>It&#8217;s 10:55PM and I&#8217;ve been programming since 8:30AM to meet a deadline. Things get missed and stupid mistakes happen. The above code worked on (IIS 6 + PHP5) but failed on (Apache2 + PHP5) &#8211; and this is one of the many reasons I hate PHP. The second argument should be a boolean and should SCREAM to the error log if you don&#8217;t match the type expected. Instead it says nothing, and leaves you to your madness.</p>
<p>Anyways, lets fix the code and go home!</p>
<pre class="brush: php;">
	header('Content-Type: application/octet-stream', true, 200);
	header('Content-Disposition: attachment/filename=' . $filename, true);
	header('Content-Length: ' . filesize($file), true);
	header('Content-Description: File Transfer');
</pre>
<p><em style="font-size:8">10.10.220.130 &#8211; - [10/Feb/2009:22:14:15 -0500] &#8220;GET /vrm/Create_Report.php?download=vrm_report_1_10-10-220-130.pdf HTTP/1.1&#8243; 200 68856 &#8220;-&#8221; &#8220;Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; &#8230;)&#8221;</em></p>
<p>Gah ;-0</p>
]]></content:encoded>
			<wfw:commentRss>http://cowarthill.com/blog/index.php/2009/02/10/php-annoyances-header/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Monthly Calendars in PHP for Flex</title>
		<link>http://cowarthill.com/blog/index.php/2009/01/21/php-flex-xml-calendar/</link>
		<comments>http://cowarthill.com/blog/index.php/2009/01/21/php-flex-xml-calendar/#comments</comments>
		<pubDate>Thu, 22 Jan 2009 01:00:05 +0000</pubDate>
		<dc:creator>MET</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[as3]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[flex]]></category>
		<category><![CDATA[kmbs]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://cowarthill.com/blog/?p=96</guid>
		<description><![CDATA[At work I recently had a need to create a monthly calendar within Flex and populated with data from a web-service (created in PHP). I hate reinventing the wheel, but I also hate wasting time searching for a solution to a simple enough problem. So below you can find a quick answer to the problem [...]]]></description>
			<content:encoded><![CDATA[<p>At work I recently had a need to create a monthly calendar within Flex and populated with data from a web-service (created in PHP). I hate reinventing the wheel, but I also hate wasting time searching for a solution to a simple enough problem. So below you can find a quick answer to the problem as I couldn&#8217;t find one myself.</p>
<p><strong>PHP</strong>: The following code creates a simple XML feed.</p>
<hr />
<pre class="brush: php;">
&lt;?php
	/**
	 * Create a XML calendar for a given year &amp;amp; month
	 *
	 * Noah Massey &amp;amp; Matthew Metnetsky
	 */
	function calendar_xml($doc, $year, $month)
	{
		if (!$doc) throw new Exception('doc is bad');
		if (!$month) throw new Exception('month  is bad');
		if (!$year) throw new Exception('year is bad');

		$numDays = date('t', mktime(0, 0, 0, $month, 1, $year));

		$monthNode = $doc-&gt;appendChild($doc-&gt;createElement('month'));

		$day = 1 - date('w', mktime(0, 0, 0, $month, 1, $year));

		do {
			$weekNode = $monthNode-&gt;appendChild($doc-&gt;createElement('week'));

			for ($y=0;$y&lt;7 and $day &lt;= $numDays; $day++,$y++) {
				//$sunday = ($day == $y);
				//$sunday = ($day == ($y + 6));
				$dayNode = $weekNode-&gt;appendChild($doc-&gt;createElement('day'));
				$numAttr = $doc-&gt;createAttribute('num');
				$numAttr-&gt;appendChild($doc-&gt;createTextNode($day));
				$dayNode-&gt;appendChild($numAttr);

				/*
				 * we could loop through database records here for holidays etc
				 * the _real_ version uses mktime(0, 0, 0, $month, $day, $year)
				 * to check for a stamp within an array of holiday records
				 * and then tack on some more nodes to $dayNode
				 */
			}
		} while ($day &lt;= $numDays);
	}

	$doc = new DOMDocument('1.0', 'UTF-8');
	$doc-&gt;formatOutput = true;

	$date = getdate();
	$year = (int) (array_key_exists('year', $_GET))? $_GET['year'] : $date['year'];
	$month = (int) (array_key_exists('month', $_GET))? $_GET['month'] : $date['mon'];

	calendar_xml($doc, $year, $month);
	header('Content-Type: application/xml', true);
	echo($doc-&gt;saveXML());
?&gt;
</pre>
<p>If you look at the rather large comment block above you can see where the value comes in. It&#8217;s pretty simple to use a unix timestamp to find records which you actually would want in the XML like holidays, events, etc.</p>
<p>The basic output looks like the following, and can be see in full at <a target="_blank" href="http://cowarthill.com/blog/wp-content/uploads/2009/01/calendar-xml.php">http://cowarthill.com/blog/wp-content/uploads/2009/01/calendar-xml.php</a></p>
<pre class="brush: xml;">
	&lt;month&gt;
		&lt;week&gt;
			&lt;day num=&quot;{day of month}&quot; /&gt;
		&lt;/week&gt;
	&lt;/month&gt;
</pre>
<hr />
Once you have all of this in place you need to pull it into Flex. I found using a DataGrid very easy once you have the XML from the web service. And to customize the display I created a custom itemRenderer for the DataGridColumns. Here&#8217;s a few snippets&#8230;.</p>
<p><strong>MXML:</strong> Lets define our DataGrid and our HTTPService which will retrieve our data.</p>
<pre class="brush: xml;">
&lt;mx:DataGrid id=&quot;dg&quot;
	sortableColumns=&quot;false&quot;
	draggableColumns=&quot;false&quot;
	selectable=&quot;false&quot;
	showScrollTips=&quot;true&quot;
	horizontalScrollPolicy=&quot;off&quot;
	verticalScrollPolicy=&quot;off&quot;&gt;
	&lt;mx:columns&gt;
		&lt;mx:DataGridColumn headerText=&quot;Sunday&quot; itemRenderer=&quot;calendar.MonthCell&quot; /&gt;
		&lt;mx:DataGridColumn headerText=&quot;Monday&quot; itemRenderer=&quot;calendar.MonthCell&quot; /&gt;
		&lt;mx:DataGridColumn headerText=&quot;Tuesday&quot; itemRenderer=&quot;calendar.MonthCell&quot; /&gt;
		&lt;mx:DataGridColumn headerText=&quot;Wednesday&quot; itemRenderer=&quot;calendar.MonthCell&quot; /&gt;
		&lt;mx:DataGridColumn headerText=&quot;Thursday&quot; itemRenderer=&quot;calendar.MonthCell&quot;  /&gt;
		&lt;mx:DataGridColumn headerText=&quot;Friday&quot; itemRenderer=&quot;calendar.MonthCell&quot; /&gt;
		&lt;mx:DataGridColumn headerText=&quot;Saturday&quot; itemRenderer=&quot;calendar.MonthCell&quot; /&gt;
	&lt;/mx:columns&gt;
&lt;/mx:DataGrid&gt;

&lt;mx:HTTPService id=&quot;calHS&quot;
	url=&quot;http://localhost/project/calendar-xml.php&quot;
	method=&quot;GET&quot;
	result=&quot;handleCalXML(event);&quot;
	fault=&quot;Alert.show('Failed to retrieve XML from server');&quot;
	resultFormat=&quot;e4x&quot;
	contentType=&quot;application/xml&quot;/&gt;
</pre>
<p><strong>AS3:</strong> Now we need at least two functions: 1 to call the service and retrieve the XML; and a second take the response and assign it to the DataGrid.</p>
<pre class="brush: jscript;">
/* month should be 1-based (January) */
public function retrieveCalendar():void
{
	calHS.url = 'http://localhost/project/calendar-xml.php?year=' + year + '&amp;amp;month=' + month;
	calHS.send();
}

/* handle the results from from calendar-xml.php */
private function handleCalXML(event:ResultEvent):void
{
	var weeks:XMLList = event.result.week as XMLList;

	/* dg is defined above in the MXML */
	this.dg.dataProvider = dayInfo;
	this.dg.rowCount = weeks.length(); // grid show be no bigger than necessary
}
</pre>
<p>Now we&#8217;re going to create a reusable itemRenderer which is instantiated for each cell in the DataGrid. You can do this in MXML directly, but I like AS3. The most important function to focus on is the setter for data because it assigns the values based on the XML we received.</p>
<pre class="brush: jscript;">
package calendar
{
	import mx.collections.XMLListCollection;
	import mx.controls.TextArea;
	import mx.controls.listClasses.BaseListData;
	import mx.controls.listClasses.IDropInListItemRenderer;
	import mx.controls.listClasses.IListItemRenderer;
	import mx.core.IDataRenderer;
	import mx.events.FlexEvent;

	public class MonthCell  extends TextArea
							implements IDataRenderer, IDropInListItemRenderer, IListItemRenderer
	{
		private var _week:XMLListCollection = null;
		private var _day:XML = null;
		private var _listData:BaseListData;

		public function MonthCell()
		{
			super();

			this.editable = false;
			this.wordWrap = true;
			this.selectable = false;
		}

		[Bindable(FlexEvent.DATA_CHANGE)]
		public override function get listData():BaseListData
		{
			return this._listData;
		}

		public override function set listData(ldata:BaseListData):void
		{
			if (this._listData != ldata) {
			this._listData = ldata;
			this.dispatchEvent(new FlexEvent(FlexEvent.DATA_CHANGE));
			}
		}

		[Bindable(FlexEvent.DATA_CHANGE)]
		public override function get data():Object
		{
			return this._week;
		}

		public override function set data(value:Object):void
		{
			var xml:XML = value as XML;

			if (xml) {
				// convert XML to a list for extra methods
				this._week = new XMLListCollection(xml.children());

				// make sure we've got enough records in week
				// to be able to get the current day
				if (this._week.length &gt; this.listData.columnIndex) {
					this._day = this._week.getItemAt(this.listData.columnIndex) as XML;
				} else {
					this._day = null; // clear in-case we're reused
				}
			}

			// do we have a valid day XML object
			if (this._week == null || this._day == null || int(this._day.@num) &lt; 1) {
				this.htmlText = &quot;&quot;;
			} else {
				/* lets display the day of the month in the cell */
				this.htmlText = &quot;&lt;p&gt;&lt;b&gt;&quot; + this._day.@num + &quot;&lt;/b&gt;&lt;/p&gt;&quot;;
			}

			this.invalidateProperties();
			this.dispatchEvent(new FlexEvent(FlexEvent.DATA_CHANGE));
		}
	}
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://cowarthill.com/blog/index.php/2009/01/21/php-flex-xml-calendar/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
