<?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>Amyst Design Company &#187; PHP Programming</title>
	<atom:link href="http://amystdesign.com/blog/category/php/feed" rel="self" type="application/rss+xml" />
	<link>http://amystdesign.com/blog</link>
	<description>Company Blog</description>
	<lastBuildDate>Sat, 03 Apr 2010 17:09:30 +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>PHP ISO Currency Code Array</title>
		<link>http://amystdesign.com/blog/php/php-iso-currency-code-array</link>
		<comments>http://amystdesign.com/blog/php/php-iso-currency-code-array#comments</comments>
		<pubDate>Sat, 03 Apr 2010 17:04:19 +0000</pubDate>
		<dc:creator>Doug Linsmeyer</dc:creator>
				<category><![CDATA[PHP Programming]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[currency code]]></category>
		<category><![CDATA[currency code list]]></category>
		<category><![CDATA[currency list]]></category>
		<category><![CDATA[iso]]></category>

		<guid isPermaLink="false">http://amystdesign.com/blog/?p=111</guid>
		<description><![CDATA[Recently, a project required us to build an application that could handle the sale of products in different currencies (an international e-commerce store). To accomplish this, we built a MySQL table to store currencies and their conversion rates against the USD. Then we built a cron-tab job (a task that is run at a set [...]]]></description>
			<content:encoded><![CDATA[<p>Recently, a project required us to build an application that could handle the sale of products in different currencies (an international e-commerce store). To accomplish this, we built a MySQL table to store currencies and their conversion rates against the USD. Then we built a cron-tab job (a task that is run at a set time interval in perpetuity) to update the database table conversion rates every 24 hours.</p>
<p>The first step to building this system was to put together a PHP array of ISO currency code&#8217;s to build the table rows from.</p>
<p>After looking around for a big, I found that there are a lot of currency code lists. Unfortunately for me, I couldn&#8217;t find any that were reasonably formatted and definitely none that were already PHP snippets.</p>
<pre class="brush: php;">
$currencies = array(
	array('Dirham', 'aed'),
	array('Afghani', 'afa'),
	array('Lek', 'all'),
	array('Dram', 'amd'),
	array('Guilder', 'ang'),
	array('Netherlands Antilles Guilder', 'ang'),
	array('New Kwanza', 'aon'),
	array('Peso', 'arp'),
	array('Schilling', 'ats'),
	array('Australian Dollar', 'aud'),
	array('Dollar', 'aud'),
	array('Florin', 'awf'),
	array('Manat', 'azm'),
	array('Convertible Mark', 'bak'),
	array('Dollar', 'bbd'),
	array('Taka', 'bdt'),
	array('Franc', 'bef'),
	array('Lev', 'bgl'),
	array('Dinar', 'bhd'),
	array('Burundi Franc', 'bif'),
	array('Dollar', 'bmd'),
	array('Dollar', 'bnd'),
	array('Boliviano', 'bob'),
	array('Brazilian Real', 'brl'),
	array('Dollar', 'bsd'),
	array('Rupee', 'btr'),
	array('Pula', 'bwp'),
	array('Ruble', 'byr'),
	array('Dollar', 'bzd'),
	array('Dollar', 'cad'),
	array('Congolese Franc', 'cdf'),
	array('Franc', 'chf'),
	array('Swiss Franc', 'chf'),
	array('Peso', 'clp'),
	array('Yuan Renminbi', 'cny'),
	array('Peso', 'cop'),
	array('Colon', 'crc'),
	array('Peso', 'cup'),
	array('Escudo', 'cve'),
	array('Pound', 'cyp'),
	array('Koruna', 'czk'),
	array('Deutsche Mark', 'dem'),
	array('Franc', 'djf'),
	array('Krone', 'dkk'),
	array('Peso', 'dop'),
	array('Algerian Dinar', 'dzd'),
	array('Sucre', 'ecs'),
	array('Kroon', 'eek'),
	array('Pound', 'egp'),
	array('Nakfa', 'ern'),
	array('Peseta', 'esp'),
	array('Birr', 'etb'),
	array('Euro', 'eur'),
	array('Markka', 'fim'),
	array('Dollar', 'fjd'),
	array('Pound', 'fkp'),
	array('Franc', 'frf'),
	array('British Pounds', 'gbp'),
	array('Lari', 'gel'),
	array('Cedi', 'ghc'),
	array('Pound', 'gip'),
	array('Dalasi', 'gmd'),
	array('Franc', 'gnf'),
	array('Drachma', 'grd'),
	array('Quetzal', 'gtq'),
	array('Dollar', 'gyd'),
	array('Dollar', 'hkd'),
	array('Lempira', 'hnl'),
	array('Kuna', 'hrk'),
	array('Gourde', 'htg'),
	array('Forint', 'huf'),
	array('Rupiah', 'idr'),
	array('Punt', 'iep'),
	array('Punt', 'iep'),
	array('Shekel', 'ils'),
	array('Rupee', 'inr'),
	array('Dinar', 'iqd'),
	array('Rial', 'irr'),
	array('Krona', 'isk'),
	array('Lira', 'itl'),
	array('Dollar', 'jmd'),
	array('Dinar', 'jod'),
	array('Yen', 'jpy'),
	array('Shilling', 'kes'),
	array('Som', 'kgs'),
	array('Riel', 'khr'),
	array('Franc', 'kmf'),
	array('Won', 'kpw'),
	array('Won', 'krw'),
	array('Dinar', 'kwd'),
	array('Dollar', 'kyd'),
	array('Tenge', 'kzt'),
	array('Kip', 'lak'),
	array('Pound', 'lbp'),
	array('Rupee', 'lkr'),
	array('Dollar', 'lrd'),
	array('Loti', 'lsl'),
	array('Lita', 'ltl'),
	array('Franc', 'luf'),
	array('Lat', 'lvl'),
	array('Dinar', 'lyd'),
	array('Dirham', 'mad'),
	array('Moroccan Dirham', 'mad'),
	array('Leu', 'mdl'),
	array('Malagasy Franc', 'mgf'),
	array('Denar', 'mkd'),
	array('Kyat', 'mmk'),
	array('Tugrik', 'mnt'),
	array('Pataca', 'mop'),
	array('Ouguiya', 'mro'),
	array('Lira', 'mtl'),
	array('Rupee', 'mur'),
	array('Rufiyaa', 'mvr'),
	array('Kwacha', 'mwk'),
	array('Peso', 'mxp'),
	array('Ringgit', 'myr'),
	array('Metical', 'mzm'),
	array('Dollar', 'nad'),
	array('Naira', 'ngn'),
	array('Cordoba Oro', 'nio'),
	array('Guilder', 'nlg'),
	array('Krone', 'nok'),
	array('Nepalese Rupee', 'npr'),
	array('Dollar', 'nzd'),
	array('Sul Rial', 'omr'),
	array('Balboa', 'pab'),
	array('Nuevo Sol', 'pen'),
	array('Kina', 'pgk'),
	array('Peso', 'php'),
	array('Rupee', 'pkr'),
	array('Zloty', 'plz'),
	array('Escudo', 'pte'),
	array('Guarani', 'pyg'),
	array('Rial', 'qar'),
	array('Leu', 'rol'),
	array('Ruble', 'rur'),
	array('Rwanda Franc', 'rwf'),
	array('Riyal', 'sar'),
	array('Dollar', 'sbd'),
	array('Luigino', 'sbl'),
	array('Rupee', 'scr'),
	array('Dinar', 'sdd'),
	array('Krona', 'sek'),
	array('Dollar', 'sgd'),
	array('Pound', 'shp'),
	array('Tolar', 'sit'),
	array('Koruna', 'skk'),
	array('Leone', 'sll'),
	array('Shilling', 'sos'),
	array('Guilder', 'srg'),
	array('Dobra', 'std'),
	array('Pound', 'syp'),
	array('Lilangeni', 'szl'),
	array('Baht', 'thb'),
	array('Somoni', 'tjs'),
	array('Manat', 'tmm'),
	array('Dinar', 'tnd'),
	array('Pa\'anga', 'top'),
	array('Lira', 'trl'),
	array('Dollar', 'ttd'),
	array('Dollar', 'twd'),
	array('Shilling', 'tzs'),
	array('Hryvnia', 'uah'),
	array('Shilling', 'ugx'),
	array('US Dollar', 'usd'),
	array('Peso', 'uyu'),
	array('Som', 'uzs'),
	array('Bolivar', 'veb'),
	array('Dong', 'vnd'),
	array('Vatu', 'vuv'),
	array('Tala', 'wst'),
	array('CFA Franc', 'xaf'),
	array('Ounces', 'xag'),
	array('Ounces', 'xau'),
	array('Dollar', 'xcd'),
	array('Special Drawing Right', 'xdr'),
	array('Ounces', 'xpd'),
	array('Ounces', 'xpt'),
	array('Rial', 'yer'),
	array('New Dinar', 'yun'),
	array('Rand', 'zar'),
	array('South African Rand', 'zar'),
	array('South African Rand', 'zar'),
	array('Kwacha', 'zmk'),
	array('Zimbabwe Dollar', 'zwd')
);
</pre>
]]></content:encoded>
			<wfw:commentRss>http://amystdesign.com/blog/php/php-iso-currency-code-array/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP Class Autoloader Function</title>
		<link>http://amystdesign.com/blog/php/php-class-autoloader-function</link>
		<comments>http://amystdesign.com/blog/php/php-class-autoloader-function#comments</comments>
		<pubDate>Fri, 16 Oct 2009 22:05:02 +0000</pubDate>
		<dc:creator>Doug Linsmeyer</dc:creator>
				<category><![CDATA[PHP Programming]]></category>

		<guid isPermaLink="false">http://amystdesign.com/blog/?p=109</guid>
		<description><![CDATA[Anyone who has done serious work with PHP since 5.0 knows the way to stay future proof is to adopt and love like it was your own the object oriented programming model. This little snippet has gone a LONG way to making my life easier. To use it, simply include it in your globally included [...]]]></description>
			<content:encoded><![CDATA[<p>Anyone who has done serious work with PHP since 5.0 knows the way to stay future proof is to adopt and love like it was your own the object oriented programming model. This little snippet has gone a LONG way to making my life easier. To use it, simply include it in your globally included functions library (yes, functions not objects.. regular every day function methods).</p>
<p>In the snippet below there is a constant &#8220;PATH_CLASSES&#8221;, in my global definitions I have this defined as the absolute path to my class libraries which are all in the same directory for this purpose.</p>
<pre class="brush: php;">
function __autoload( $class_name )
{
	$file_formats = array('%s.php', '%s.class.php', 'class.%s.php', '%s.inc.php');
	foreach ( $file_formats as $file_format )
	{
		$file = PATH_CLASSES.sprintf($file_format, strtolower($class_name));
		if ( file_exists($file) ) require_once($file);
	}
	return;
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://amystdesign.com/blog/php/php-class-autoloader-function/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP Search an array of objects for a property value</title>
		<link>http://amystdesign.com/blog/php/php-search-an-array-of-objects-for-a-property-value</link>
		<comments>http://amystdesign.com/blog/php/php-search-an-array-of-objects-for-a-property-value#comments</comments>
		<pubDate>Fri, 16 Oct 2009 20:56:38 +0000</pubDate>
		<dc:creator>Doug Linsmeyer</dc:creator>
				<category><![CDATA[PHP Programming]]></category>

		<guid isPermaLink="false">http://amystdesign.com/blog/?p=106</guid>
		<description><![CDATA[While developing a new project management application I needed a way to search an array of objects for a specific object property value. For example, I had an array of &#8220;employee&#8221; objects that contain a great many properties to describe each &#8220;employee&#8221; and you want to search this array for an employee object that has [...]]]></description>
			<content:encoded><![CDATA[<p>While developing a new project management application I needed a way to search an array of objects for a specific object property value. For example, I had an array of &#8220;employee&#8221; objects that contain a great many properties to describe each &#8220;employee&#8221; and you want to search this array for an employee object that has a specific value for one of it&#8217;s properties. To solve this I have come up with the following little snippet:</p>
<pre class="brush: php;">
function search_object_array($needle_key, $needle_val, $haystack)
{
	// iterate through our haystack
	for ( $i = 0; $i &lt; count($haystack); $i++ )
	{
		// ensure this array element is an object and has a key that matches our needle's key
		if ( is_object($haystack[$i]) and property_exists($haystack[$i], $needle_key) )
		{
			// determine if comparison is case sensitive
			if ( strtolower($needle_val) == strtolower($haystack[$i]-&gt;$needle_key) ) return $i;
		}
	}
	// no match found
	return false;
}
</pre>
<p>This function returns the index of the object array if a match is found and false if no matches are found.</p>
<p>So lets test it.<br />
First I&#8217;ll define a class that we will use to generate our objects that will populate our array, well call this class &#8220;foo&#8221;.</p>
<pre class="brush: php;">
class foo
{
	public $id;
	public $name;
	public $description;

	public function __construct( $id, $name, $description )
	{
		$this-&gt;id = $id;
		$this-&gt;name = $name;
		$this-&gt;description = $description;
	}
}
</pre>
<p>Next we&#8217;ll populate our object array &#8220;$obj_arr&#8221; with objects</p>
<pre class="brush: php;">
$obj_arr[0] = new foo(1, 'Joe', 'Joes description');
$obj_arr[1] = new foo(2, 'Sara', 'Saras description');
$obj_arr[2] = new foo(3, 'Adam', 'Adams description');
$obj_arr[3] = new foo(4, 'Jake', 'Jakes description');
$obj_arr[4] = new foo(5, 'Sally', 'Sallys description');
$obj_arr[5] = new foo(6, 'Paul', 'Pauls description');
$obj_arr[6] = new foo(7, 'Freddie', 'Freddies description');
</pre>
<p>Now lets search our new array of objects for an object that matches our criteria:</p>
<pre class="brush: php;">
$result = search_object_array('name', 'sally', $obj_arr);
echo $result;
// will print: 4
</pre>
<p>I hope this helps!</p>
]]></content:encoded>
			<wfw:commentRss>http://amystdesign.com/blog/php/php-search-an-array-of-objects-for-a-property-value/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>PHP Recursive file search and replace function</title>
		<link>http://amystdesign.com/blog/php/php-recursive-file-search-and-replace-function</link>
		<comments>http://amystdesign.com/blog/php/php-recursive-file-search-and-replace-function#comments</comments>
		<pubDate>Thu, 24 Sep 2009 21:47:58 +0000</pubDate>
		<dc:creator>Doug Linsmeyer</dc:creator>
				<category><![CDATA[PHP Programming]]></category>
		<category><![CDATA[find string]]></category>
		<category><![CDATA[recursive file search and replace]]></category>
		<category><![CDATA[search]]></category>
		<category><![CDATA[search and repalce]]></category>
		<category><![CDATA[search file]]></category>

		<guid isPermaLink="false">http://amystdesign.com/blog/?p=95</guid>
		<description><![CDATA[I had a client recently who had unfortunately been attacked by one of those fun PHP insertion hacks, often associated with outdated installations of WordPress (fixed in later versions). Regardless it left the client with around 800 files with an identical block of code at the very beginning of the file. I won&#8217;t get into [...]]]></description>
			<content:encoded><![CDATA[<p>I had a client recently who had unfortunately been attacked by one of those fun PHP insertion hacks, often associated with outdated installations of WordPress (fixed in later versions). Regardless it left the client with around 800 files with an identical block of code at the very beginning of the file.</p>
<p>I won&#8217;t get into treating the problem (the security weakness) here, instead I am just talking about how I treated the symptom, the malicious code that was inserted. I did a quick Google search and came up with a recursive directory/file listing function that suited my needs perfectly. Credit for this code is given in the below code.</p>
<p>NOTES:<br />
It is very important to note that this code may not work for you perfectly out of the box as I created it for a specific task, for instance this script will ONLY search *.php and *.html files unless you add other extensions or remove that routine all together.</p>
<p>FUNCTION CODE:</p>
<pre class="brush: php;">
/*
--------------------------------------------------------------------------------

	PHP SCRIPT
	---
	For:     Amyst Design Company
	By:      Doug Linsmeyer, Amyst Design Company
	Date:    2009-09-24
	Revised:
	Version: 0

--------------------------------------------------------------------------------
Purpose:
Recusively search all files and sub-folders for a regex pattern and
replace it with another strong and resave the file.

Some original code by mallsop.com on 05/2001.
Updated by mallsop.com on 04/2009.
PHP 5 or better. Tested ok on unix hosting box.
GPL License.
Some snippets from other php code posted online.
--------------------------------------------------------------------------------
*/

function scanDirectory($needle, $replacement, $dirid, $dirname, $path, $spaces)
{
	while ( ($file = readdir($dirid)) != false )
	{
		if ( ($file != '.') &amp;&amp; ($file != '..') )
		{
			$dirname_full  = $dirname.'/'.$file;
			if ( is_dir($dirname_full) &amp;&amp; ($file != 'cgi-bin') )
			{
				$return .= $spaces . '&lt;b&gt;' . $file . '&lt;/b&gt;&lt;br /&gt;' . &quot;\n&quot;;
				$dirid_next = opendir($dirname_full); // was just $file
				$newpath = $path.'/'.$file;
				$nextspaces = $spaces.'&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;';
				$return .= scanDirectory($needle, $replacement, $dirid_next, $dirname_full, $newpath, $nextspaces);
				$nextspaces = '';
				closedir($dirid_next);
				$dirname_here = '';
			}
			else
			{
				$break = explode('.', $file);
				$file_ext = $break[1];
				$file_ext_lowercase = strtolower($file_ext);
				if ( ($file_ext_lowercase == 'php') OR ($file_ext_lowercase == 'html') )
				{

					$code = file_get_contents($dirname . '/' . $file);
					if ( preg_match($needle, $code) )
					{
						$matchtext = '&lt;span style=&quot;color:#cc0000;&quot;&gt;...Found.&lt;/span&gt;';
						$newcode = preg_replace($needle, $replacement, $code);
						file_put_contents($dirname . '/' . $file, $newcode);
						$matchtext .= '...Fixed.';
					}
					else
					{
						$matchtext = '&lt;span style=&quot;color:#00cc00;&quot;&gt;...Not Found&lt;/span&gt;';
					}
					$return .= $spaces . $file . $matchtext . '&lt;br /&gt;' . &quot;\n&quot;;

				}
			}
		}
	}
	return ($return);
}
</pre>
<p>IMPLEMENTATION:</p>
<pre class="brush: php;">
$needle = '/\&lt;\? [\/\*]{4}eval\(base64_decode\(\'[A-Za-z0-9]+\'\)\); \?\&gt;/';
$replacement = '';
$dirname_start = '/home/wildchi2/public_html/';
$dirid_start = opendir($dirname_start);
$dirname_here = basename($dirname_start);
$spaces = '&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;';
$output = scanDirectory($needle, $replacement, $dirid_start, $dirname_start, $dirname_here, $spaces);
?&gt;
&lt;html&gt;
	&lt;head&gt;
		&lt;title&gt;Recursive File Search and Replace Utility by Amyst Design, 2009&lt;/title&gt;
	&lt;/head&gt;
	&lt;body&gt;
		&lt;h1&gt;Recursive Search and Replace Utility.&lt;/h1&gt;
		&lt;h2&gt;By Amyst Design Co.&lt;/h2&gt;
		&lt;h3&gt;Searching all files in the &lt;?=$dirname_start?&gt; folder.&lt;/h3&gt;
		&lt;?=$output?&gt;
		&lt;h3&gt;Finished.&lt;/h3&gt;
	&lt;/body&gt;
&lt;/html&gt;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://amystdesign.com/blog/php/php-recursive-file-search-and-replace-function/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>PHP Build Array of Years and Months Between Two Dates</title>
		<link>http://amystdesign.com/blog/php/php-build-array-of-years-and-months-between-two-dates</link>
		<comments>http://amystdesign.com/blog/php/php-build-array-of-years-and-months-between-two-dates#comments</comments>
		<pubDate>Thu, 18 Jun 2009 17:22:34 +0000</pubDate>
		<dc:creator>Doug Linsmeyer</dc:creator>
				<category><![CDATA[PHP Programming]]></category>

		<guid isPermaLink="false">http://amystdesign.com/blog/?p=93</guid>
		<description><![CDATA[On another recent project I had to generate monthly reports for the clients, to allow them to select the desired report I decided on giving them two HTML select inputs, the first for year and second for month. I tied the two fields together via ajax. To populate these select boxes I wanted to build [...]]]></description>
			<content:encoded><![CDATA[<p>On another recent project I had to generate monthly reports for the clients, to allow them to select the desired report I decided on giving them two HTML select inputs, the first for year and second for month. I tied the two fields together via ajax. </p>
<p>To populate these select boxes I wanted to build an multi-dimensional array of dates organized by year and then by month where the first date or month available is the date the client&#8217;s account was created (a date from the database) and the last date or month to be the current.</p>
<p>I build the following function to build my array of years and months, it is dependent upon another function posted on this site that <a href="http://amystdesign.com/blog/php/php-build-date-range-array" title="PHP build array of individual dates">builds an array of individual dates</a>. These could be combined into one &#8216;SUPER FUNCTION&#8217; but there was no need as the project required both functions discretely anyway.</p>
<p>The function requires two input options, a starting date and an ending date. These dates must be in the &#8217;0000-00-00&#8242; format.</p>
<p>The result of: </p>
<pre class="brush: php;">
$start = '2007-05-12';
$end = '2009-06-18';
print_r( year_month_array( $start, $end ) );
</pre>
<p>Would be:</p>
<pre class="brush: php;">
Array
(
    [2007] =&gt; Array
        (
            [0] =&gt; May
            [1] =&gt; June
            [2] =&gt; July
            [3] =&gt; August
            [4] =&gt; September
            [5] =&gt; October
            [6] =&gt; November
            [7] =&gt; December
        )

    [2008] =&gt; Array
        (
            [0] =&gt; January
            [1] =&gt; February
            [2] =&gt; March
            [3] =&gt; April
            [4] =&gt; May
            [5] =&gt; June
            [6] =&gt; July
            [7] =&gt; August
            [8] =&gt; September
            [9] =&gt; October
            [10] =&gt; November
            [11] =&gt; December
        )

    [2009] =&gt; Array
        (
            [0] =&gt; January
            [1] =&gt; February
            [2] =&gt; March
            [3] =&gt; April
            [4] =&gt; May
            [5] =&gt; June
        )

)
</pre>
<pre class="brush: php;">
function year_month_array($from, $to)
{
	$months = array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
	$dates = array_values(array_unique(get_date_range_array($from, $to, 'Y-n')));
	$data = array();
	foreach ( $dates as $date )
	{
		$date = explode('-', $date);
		$data[$date[0]][] = $months[$date[1]-1];
	}
	return $data;
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://amystdesign.com/blog/php/php-build-array-of-years-and-months-between-two-dates/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP Build Date Range Array</title>
		<link>http://amystdesign.com/blog/php/php-build-date-range-array</link>
		<comments>http://amystdesign.com/blog/php/php-build-date-range-array#comments</comments>
		<pubDate>Thu, 18 Jun 2009 17:08:26 +0000</pubDate>
		<dc:creator>Doug Linsmeyer</dc:creator>
				<category><![CDATA[PHP Programming]]></category>

		<guid isPermaLink="false">http://amystdesign.com/blog/?p=91</guid>
		<description><![CDATA[On a recent client project I was tasked with creating a chart of daily revenue from sales of, lets say, widgets. The first step to building this was to build an array of dates between two dates. I found a great little function from Mike&#8217;s Page, www.boonedocks.net that did almost exactly what I needed it [...]]]></description>
			<content:encoded><![CDATA[<p>On a recent client project I was tasked with creating a chart of daily revenue from sales of, lets say, widgets. The first step to building this was to build an array of dates between two dates. I found a great little function from Mike&#8217;s Page, <a href="http://boonedocks.net/mike/archives/137-Creating-a-Date-Range-Array-with-PHP.html" title="Link to source of this function">www.boonedocks.net</a> that did almost exactly what I needed it to do. I need to use a few different date formats so I modified it slightly and I am re-posting it here for your use.</p>
<p>This function accepts (3) options, the first is the starting date which needs to be in the format &#8217;0000-00-00&#8242;. The second is the ending date which also needs to be in the format &#8217;0000-00-00&#8242; and lastly the third is the out put date format, examples and ideas can be found in the PHP manual <a href="http://us2.php.net/manual/en/function.date.php" title="PHP manual for date function.">PHP: date &#8211; Manual</a>.</p>
<p>My input dates were already in the &#8216;Y-m-d&#8217; format so I didn&#8217;t bother changing this, but one could utilize the strtotime() function to make these two function input options variable.</p>
<p>The result is an array of dates in whichever format you define.</p>
<pre class="brush: php;">
function get_date_range_array( $date_from, $date_to, $format = 'Y-m-d' )
{
	$date_array = array();
	$i_date_from = mktime(1, 0, 0, substr($date_from, 5, 2), substr($date_from, 8, 2),substr($date_from, 0, 4));
	$i_date_to = mktime(1, 0, 0, substr($date_to, 5, 2), substr($date_to, 8, 2),substr($date_to, 0, 4));
	if ( $i_date_to &gt;= $i_date_from )
	{
		array_push($date_array, date($format, $i_date_from)); // first entry
		while ( $i_date_from &lt; $i_date_to )
		{
			$i_date_from += 86400; // add 24 hours
			array_push($date_array, date($format, $i_date_from));
		}
	}
	return $date_array;
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://amystdesign.com/blog/php/php-build-date-range-array/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>PHP Random Password Function</title>
		<link>http://amystdesign.com/blog/php/php-random-password-function</link>
		<comments>http://amystdesign.com/blog/php/php-random-password-function#comments</comments>
		<pubDate>Thu, 18 Jun 2009 16:53:02 +0000</pubDate>
		<dc:creator>Doug Linsmeyer</dc:creator>
				<category><![CDATA[PHP Programming]]></category>

		<guid isPermaLink="false">http://amystdesign.com/blog/?p=88</guid>
		<description><![CDATA[Anyone who has ever dealt with user authentication knows how important a strong password is, this is a very simple, and small function that will generate a password of variable length and complexity. The function accepts two options; the first, length is self explanatory. This is the length of the password you wish to generate, [...]]]></description>
			<content:encoded><![CDATA[<p>Anyone who has ever dealt with user authentication knows how important a strong password is, this is a very simple, and small function that will generate a password of variable length and complexity. The function accepts two options; the first, length is self explanatory. This is the length of the password you wish to generate, a common length is 6-8. The second is the complexity of the password, this involves the possible characters from which the password will be generated. I designed this to accept values 1-4 (any other value will default to 4) where 1 is the weakest and 4 the strongest.</p>
<p>The function returns a generated password.</p>
<pre class="brush: php;">
function generate_password ( $length = 8, $complexity = 3 )
{
	$password = '';
	switch ( $complexity )
	{
		case 1:
			$possible = '0123456789';
			break;
		case 2:
			$possible = '0123456789bcdfghjkmnpqrstvwxyz';
			break;
		case 3:
			$possible = '0123456789bcdfghjkmnpqrstvwxyzABCDEFGHIJKLMOPQRSTUVWXYZ';
			break;
		case 4:
		default:
			$possible = '0123456789bcdfghjkmnpqrstvwxyzABCDEFGHIJKLMOPQRSTUVWXYZ@#$%&amp;!?^~';
			break;
	}
	$i = 0;
	while ( $i &lt; $length )
	{
		// pick a random character from the possible ones
		$char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
		// we don't want this character if it's already in the password
		if ( !strstr($password, $char) )
		{
			$password .= $char;
			$i++;
		}
	}
	return $password;
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://amystdesign.com/blog/php/php-random-password-function/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP state and country list functions &#8211; JSON or Array</title>
		<link>http://amystdesign.com/blog/php/php-state-and-country-list-functions-json-or-array</link>
		<comments>http://amystdesign.com/blog/php/php-state-and-country-list-functions-json-or-array#comments</comments>
		<pubDate>Fri, 01 May 2009 14:38:28 +0000</pubDate>
		<dc:creator>Doug Linsmeyer</dc:creator>
				<category><![CDATA[PHP Programming]]></category>

		<guid isPermaLink="false">http://amystdesign.com/blog/?p=85</guid>
		<description><![CDATA[One of the most common select fields we are tasked to create as developers are state and country fields; most of us have a library of code snippets on hand for just such a thing, like a ready-made select list. On a recent project I was tasked with creating an autocomplete field and my select [...]]]></description>
			<content:encoded><![CDATA[<p>One of the most common select fields we are tasked to create as developers are state and country fields; most of us have a library of code snippets on hand for just such a thing, like a ready-made select list. On a recent project I was tasked with creating an autocomplete field and my select list just wouldn&#8217;t cut it as the JQUERY function I was using required JSON input.</p>
<p>To solve the problem I created a couple PHP functions that I use to build the lists dynamically, the functions output a state or country list as either a PHP array, or a JSON encoded array.</p>
<p>In the examples below I removed the line breaks in the arrays, simply because I prefer it to a very long list I have to scroll through.</p>
<p>Feel free to use these in your own projects. If you do, let me know.</p>
<p><strong>Code:</strong></p>
<pre class="brush: php;">
function list_states ( $mode = 'JSON' )
{
	$states = array('AL'=&gt;'Alabama', 'AK'=&gt;'Alaska', 'AZ'=&gt;'Arizona', 'AR'=&gt;'Arkansas', 'CA'=&gt;'California', 'CO'=&gt;'Colorado', 'CT'=&gt;'Connecticut', 'DE'=&gt;'Delaware', 'DC'=&gt;'District Of Columbia', 'FL'=&gt;'Florida', 'GA'=&gt;'Georgia', 'HI'=&gt;'Hawaii', 'ID'=&gt;'Idaho', 'IL'=&gt;'Illinois', 'IN'=&gt;'Indiana', 'IA'=&gt;'Iowa', 'KS'=&gt;'Kansas', 'KY'=&gt;'Kentucky', 'LA'=&gt;'Louisiana', 'ME'=&gt;'Maine', 'MD'=&gt;'Maryland', 'MA'=&gt;'Massachusetts', 'MI'=&gt;'Michigan', 'MN'=&gt;'Minnesota', 'MS'=&gt;'Mississippi', 'MO'=&gt;'Missouri', 'MT'=&gt;'Montana', 'NE'=&gt;'Nebraska', 'NV'=&gt;'Nevada', 'NH'=&gt;'New Hampshire', 'NJ'=&gt;'New Jersey', 'NM'=&gt;'New Mexico', 'NY'=&gt;'New York', 'NC'=&gt;'North Carolina', 'ND'=&gt;'North Dakota', 'OH'=&gt;'Ohio', 'OK'=&gt;'Oklahoma', 'OR'=&gt;'Oregon', 'PA'=&gt;'Pennsylvania', 'RI'=&gt;'Rhode Island', 'SC'=&gt;'South Carolina', 'SD'=&gt;'South Dakota', 'TN'=&gt;'Tennessee', 'TX'=&gt;'Texas','UT'=&gt;'Utah', 'VT'=&gt;'Vermont','VA'=&gt;'Virginia', 'WA'=&gt;'Washington','WV'=&gt;'West Virginia', 'WI'=&gt;'Wisconsin','WY'=&gt;'Wyoming');
	switch ( $mode )
	{
		case 'JSON':
			return json_encode($states);
			break;
		case 'Array':
			return $states;
			break;
	}
}

function list_countries ( $mode = 'JSON' )
{
	$countries = array( 'Afghanistan', 'Albania', 'Algeria', 'Andorra', 'Angola', 'Antigua and Barbuda', 'Argentina', 'Armenia', 'Australia', 'Austria', 'Azerbaijan', 'Bahamas', 'Bahrain', 'Bangladesh', 'Barbados', 'Belarus', 'Belgium', 'Belize', 'Benin', 'Bhutan', 'Bolivia', 'Bosnia and Herzegovina', 'Botswana', 'Brazil', 'Brunei', 'Bulgaria', 'Burkina Faso', 'Burundi', 'Cambodia', 'Cameroon', 'Canada', 'Cape Verde', 'Central African Republic', 'Chad', 'Chile', 'China', 'Colombi', 'Comoros', 'Congo (Brazzaville)', 'Congo', 'Costa Rica', 'Cote d\'Ivoire', 'Croatia', 'Cuba', 'Cyprus', 'Czech Republic', 'Denmark', 'Djibouti', 'Dominica', 'Dominican Republic', 'East Timor (Timor Timur)', 'Ecuador', 'Egypt', 'El Salvador', 'Equatorial Guinea', 'Eritrea', 'Estonia', 'Ethiopia', 'Fiji', 'Finland', 'France', 'Gabon', 'Gambia, The', 'Georgia', 'Germany', 'Ghana', 'Greece', 'Grenada', 'Guatemala', 'Guinea', 'Guinea-Bissau', 'Guyana', 'Haiti', 'Honduras', 'Hungary', 'Iceland', 'India', 'Indonesia', 'Iran', 'Iraq', 'Ireland', 'Israel', 'Italy', 'Jamaica', 'Japan', 'Jordan', 'Kazakhstan', 'Kenya', 'Kiribati', 'Korea, North', 'Korea, South', 'Kuwait', 'Kyrgyzstan', 'Laos', 'Latvia', 'Lebanon', 'Lesotho', 'Liberia', 'Libya', 'Liechtenstein', 'Lithuania', 'Luxembourg', 'Macedonia', 'Madagascar', 'Malawi', 'Malaysia', 'Maldives', 'Mali', 'Malta', 'Marshall Islands', 'Mauritania', 'Mauritius', 'Mexico', 'Micronesia', 'Moldova', 'Monaco', 'Mongolia', 'Morocco', 'Mozambique', 'Myanmar', 'Namibia', 'Nauru', 'Nepa', 'Netherlands', 'New Zealand', 'Nicaragua', 'Niger', 'Nigeria', 'Norway', 'Oman', 'Pakistan', 'Palau', 'Panama', 'Papua New Guinea', 'Paraguay', 'Peru', 'Philippines', 'Poland', 'Portugal', 'Qatar', 'Romania', 'Russia', 'Rwanda', 'Saint Kitts and Nevis', 'Saint Lucia', 'Saint Vincent', 'Samoa', 'San Marino', 'Sao Tome and Principe', 'Saudi Arabia', 'Senegal', 'Serbia and Montenegro', 'Seychelles', 'Sierra Leone', 'Singapore', 'Slovakia', 'Slovenia', 'Solomon Islands', 'Somalia', 'South Africa', 'Spain', 'Sri Lanka', 'Sudan', 'Suriname', 'Swaziland', 'Sweden', 'Switzerland', 'Syria', 'Taiwan', 'Tajikistan', 'Tanzania', 'Thailand', 'Togo', 'Tonga', 'Trinidad and Tobago', 'Tunisia', 'Turkey', 'Turkmenistan', 'Tuvalu', 'Uganda', 'Ukraine', 'United Arab Emirates', 'United Kingdom', 'United States', 'Uruguay', 'Uzbekistan', 'Vanuatu', 'Vatican City', 'Venezuela', 'Vietnam', 'Yemen', 'Zambia', 'Zimbabwe');
	switch ( $mode )
	{
		case 'JSON':
			return json_encode($countries );
			break;
		case 'Array':
			return $countries ;
			break;
	}

}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://amystdesign.com/blog/php/php-state-and-country-list-functions-json-or-array/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SQLite Database Class</title>
		<link>http://amystdesign.com/blog/php/sqlite-database-class</link>
		<comments>http://amystdesign.com/blog/php/sqlite-database-class#comments</comments>
		<pubDate>Mon, 27 Apr 2009 12:36:30 +0000</pubDate>
		<dc:creator>Doug Linsmeyer</dc:creator>
				<category><![CDATA[PHP Programming]]></category>
		<category><![CDATA[SQLite]]></category>

		<guid isPermaLink="false">http://amystdesign.com/blog/?p=73</guid>
		<description><![CDATA[I have been wanting to try SQLite for some time and I recently got the chance to for a new project. For the uninitiated: SQLite is a flat-file database system with most of the functionality of larger database software like MySQL or MSSQL. SQLite is best suited for small footprint, light use databases, handling site [...]]]></description>
			<content:encoded><![CDATA[<p>I have been wanting to try SQLite for some time and I recently got the chance to for a new project.</p>
<p>For the uninitiated:<br />
<em>SQLite is a flat-file database system with most of the functionality of larger database software like MySQL or MSSQL. SQLite is best suited for small footprint, light use databases, handling site newsreels or client contact data.</em></p>
<p>To start my new project I decided to clone a PHP database class that I have been using for some time for another platform. It does:</p>
<ul>
<li>Database connection/creation.</li>
<li>Select, insert, and update queries.</li>
<li>Array based insert/update queries for rapid programming.</li>
<li>Error reporting on SQLite class functions.</li>
<li>Data scrubbing on insert/update queries to prevent issues.</li>
</ul>
<p><strong>Source Code:</strong></p>
<pre class="brush: php;">
class sqlite_database
{
	protected $link;
	protected $error;
	private $db_path = '/path/to/your/database.db';

	function __construct()
	{
		$this-&gt;link = sqlite_open( $this-&gt;db_path, 0666, $this-&gt;error ) or die ('Error: ' . $this-&gt;error);
	}

	function close()
	{
		sqlite_close($this-&gt;link);
	}

	function last_id()
	{
		return sqlite_last_insert_rowid($this-&gt;link);
	}

	function escape_str( $string )
	{
		return sqlite_escape_string($string);
	}

	function query( $sql )
	{
		$r = sqlite_unbuffered_query($this-&gt;link, $sql, SQLITE_ASSOC, $this-&gt;error) or die('Error: ' . $this-&gt;error . '&lt;br /&gt;SQL: ' . $sql);
		if ( !$r )
		{
			return false;
		}
		else
		{
			if ( sqlite_valid($r) )
			{
				$data-&gt;dataset = array();
				while ( $row = sqlite_fetch_object($r) )
				{
					$data-&gt;dataset[] = $row;
				}
				$data-&gt;rows = count($data-&gt;dataset);
				return $data;
			}
			else
			{
				return false;
			}
		}
	}

	function query_insert( $table, $data )
	{
		$q = 'INSERT INTO ' . $table;
		$v = '';
		$n = '';
		foreach ( $data as $key =&gt; $val )
		{
			$n .= $key . ', ';
			if ( strtolower($val) == 'null' )
			{
				$v .= 'NULL, ';
			}
			elseif (strtolower($val) == 'now()' )
			{
				$v .= 'NOW(), ';
			}
			else
			{
				$v .= &quot;'&quot; . $this-&gt;escape_str($val) . &quot;', &quot;;
			}
    	}
		$q .= ' (' . rtrim($n, ', ') . ') VALUES (' . rtrim($v, ', ') . ');';
		$r = sqlite_unbuffered_query($this-&gt;link, $q, SQLITE_ASSOC, $this-&gt;error) or die('Error: ' . $this-&gt;error . '&lt;br /&gt;SQL: ' . $q);
		return ( $r ) ? true : false;
	}

	function query_update($table, $data, $where)
	{
		$q = &quot;UPDATE {$table} SET &quot;;
		foreach ( $data as $key =&gt; $val )
		{
			if ( strtolower($val) == 'null' )
			{
				$q .= &quot;`$key` = NULL, &quot;;
			}
			elseif ( strtolower($val) == 'now()' )
			{
				$q .= &quot;`$key` = NOW(), &quot;;
			}
			else
			{
				$q .= &quot;`$key`='&quot; . $this-&gt;escape_str($val) . &quot;', &quot;;
			}
		}
		foreach ( $where as $key =&gt; $val )
		{
			$where_sql .= '( ' . $key[0] . ' ' . $key[1] . ' \'' . $key[2] . '\' ) ';
		}
		$where_sql = substr($where_sql, 0, -1);
		$q = rtrim($q, ', ') . ' WHERE ' . $where_sql . ';';
		$r = sqlite_unbuffered_query($this-&gt;link, $q, SQLITE_ASSOC, $this-&gt;error) or die('Error: ' . $this-&gt;error . '&lt;br /&gt;SQL: ' . $q);
		return ( $r ) ? true : false;
	}
}
</pre>
<p><strong>Examples</strong><br />
Connect to a database:</p>
<pre class="brush: php;">
// require the sqlite database class library from Amyst Design
require('path/to/sqlite.php');
// instantiate the database object
// the path and file name of the database are set within the class
// If the database file doesn't exist SQLite will create the file
$db = new sqlite_database();
</pre>
<p>Insert a row of data:</p>
<pre class="brush: php;">
// build the array of data to be inserted
// using key/value pairs, set keys to database field names.
$data = array('field1' =&gt; $value1,
			  'field2' =&gt; $value2,
			  'fieldn' =&gt; $valuen);
// assuming the database object has been instantiated
// function will return true or false depending on outcome
$result = $db-&gt;query_insert( 'tablename', $data );
</pre>
<p>Update a row of data:</p>
<pre class="brush: php;">
// build the array of data to be inserted
// using key/value pairs, set keys to database field names.
$data = array('field1' =&gt; $value1,
			  'field2' =&gt; $value2,
			  'fieldn' =&gt; $valuen);
// build the array of criteria
// field name, operator, criteria
// this does not support complex where clauses
$where = array(array('fieldname1', '=', 'value1'),
			   array('fieldname2', '&gt;', 'value2'));
// assuming the database object has been instantiated
// function will return true or false depending on outcome
$result = $db-&gt;query_insert( 'tablename', $data, $id_to_update );
</pre>
]]></content:encoded>
			<wfw:commentRss>http://amystdesign.com/blog/php/sqlite-database-class/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Search for a file and delete it.</title>
		<link>http://amystdesign.com/blog/php/search-for-a-file-and-delete-it</link>
		<comments>http://amystdesign.com/blog/php/search-for-a-file-and-delete-it#comments</comments>
		<pubDate>Tue, 31 Mar 2009 21:29:31 +0000</pubDate>
		<dc:creator>Doug Linsmeyer</dc:creator>
				<category><![CDATA[PHP Programming]]></category>

		<guid isPermaLink="false">http://amystdesign.com/blog/?p=66</guid>
		<description><![CDATA[This snippet is fairly specific to the task it was required for, a client had a directory on their website that contained literally thousands of folders inside it, not nested just a ton of folders inside of one folder. The task was to search each one of these sub-folders (not recursively to any sub-folders inside [...]]]></description>
			<content:encoded><![CDATA[<p>This snippet is fairly specific to the task it was required for, a client had a directory on their website that contained literally thousands of folders inside it, not nested just a ton of folders inside of one folder. The task was to search each one of these sub-folders (not recursively to any sub-folders inside these first level ones, just the first level children to the target directory) and search for a file called &#8216;index.php&#8217; and delete it (the client preferred another file extension be used for the index page).</p>
<p>Think&#8230;</p>
<p>-target_directory<br />
&nbsp;&nbsp;&nbsp;| -subfolder_1<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;| -index.html<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;| -index.php<br />
&nbsp;&nbsp;&nbsp;| -subfolder_2<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;| -index.html<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;| -index.php<br />
&nbsp;&nbsp;&nbsp;| -subfolder_n<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;| -index.html<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;| -index.php</p>
<pre class="brush: php;">
// open the target directory
$dh = opendir('/path/to/target/directory');
// iterate through the folders in the target directory
while ( ( $file = readdir($dh) ) !== false )
{
	// only check folders, exclude files and the directory controls
	if ( $file != &quot;.&quot; &amp;&amp; $file != &quot;..&quot; &amp;&amp; !is_file($file) )
	{
		$index_path = $path.'/'.$file.'/'.'index.php';
		// check if 'index.php' exists
		if ( is_file($index_path) )
		{
			// delete 'index.php'
			unlink($index_path);
		}
	}
}
closedir($dh);
</pre>
]]></content:encoded>
			<wfw:commentRss>http://amystdesign.com/blog/php/search-for-a-file-and-delete-it/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
