<?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>Matt DeKok&#039;s Portfolio</title>
	<atom:link href="http://www.animestreamers.com/beta/portfolio/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.animestreamers.com/beta/portfolio</link>
	<description></description>
	<lastBuildDate>Thu, 22 Mar 2012 00:16:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Multi-Dimensional Array Sort Function</title>
		<link>http://www.animestreamers.com/beta/portfolio/80/multi-dimensional-array-sort-function/</link>
		<comments>http://www.animestreamers.com/beta/portfolio/80/multi-dimensional-array-sort-function/#comments</comments>
		<pubDate>Fri, 20 Jan 2012 15:50:12 +0000</pubDate>
		<dc:creator>Matt DeKok</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Arrays]]></category>

		<guid isPermaLink="false">http://www.animestreamers.com/beta/portfolio/?p=80</guid>
		<description><![CDATA[// mdasort(&#38;$array, $sort_by_key, $direction = SORT_ASC) // ---------------------------------------------------------------------------------- // returns true/false // // &#38;$array = array to be sorted // $sort_by_key = the key of the array element to sort by. This element is an element // of the second-dimension &#8230; <a href="http://www.animestreamers.com/beta/portfolio/80/multi-dimensional-array-sort-function/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<pre class="crayon-plain-tag"><code>// mdasort(&amp;$array, $sort_by_key, $direction = SORT_ASC)
// ----------------------------------------------------------------------------------
// returns true/false
//
// &amp;$array      = array to be sorted
// $sort_by_key = the key of the array element to sort by. This element is an element
//                of the second-dimension array
// $direction   = SORT_ASC or SORT_DESC
function mdasort(&amp;$array, $sort_by_key, $direction = SORT_ASC)
{
    try
    {
        $comparisons = array();
        
        foreach($array as $comp)
        {
            $comparisons[] = $comp[$sort_by_key];
        }
    
        array_multisort($comparisons, $direction, $array);
        
        return true;
    }
    catch (Exception $e)
    {
        echo $e;
        return false;
    }
}</code></pre>
<p>Here is an example of its useage&#8230;</p>
<pre class="crayon-plain-tag"><code>$arr = array(
    array('lemon'),
    array('apple'),
    array('watermelon'),
    array('grape')
);

mdasort($arr, 0, SORT_DESC);

print_r($arr);</code></pre><p>
<p>Will look like&#8230;</p>
<pre class="crayon-plain-tag"><code>Array
(
    [0] =&gt; Array
        (
            [0] =&gt; watermelon
        )

    [1] =&gt; Array
        (
            [0] =&gt; lemon
        )

    [2] =&gt; Array
        (
            [0] =&gt; grape
        )

    [3] =&gt; Array
        (
            [0] =&gt; apple
        )

)</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://www.animestreamers.com/beta/portfolio/80/multi-dimensional-array-sort-function/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Smarty date_diff Function</title>
		<link>http://www.animestreamers.com/beta/portfolio/51/smarty-date_diff-function/</link>
		<comments>http://www.animestreamers.com/beta/portfolio/51/smarty-date_diff-function/#comments</comments>
		<pubDate>Mon, 16 Jan 2012 21:00:31 +0000</pubDate>
		<dc:creator>Matt DeKok</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Smarty Plugins]]></category>
		<category><![CDATA[Smarty]]></category>

		<guid isPermaLink="false">http://www.animestreamers.com/beta/portfolio/?p=51</guid>
		<description><![CDATA[This is a function-type plugin for the Smarty template engine to calculate the difference between two dates in days (default), weeks, or years. Download: Click here and extract the file to your Smarty "plugins/" folder. &#60;?php /* * Smarty plugin &#8230; <a href="http://www.animestreamers.com/beta/portfolio/51/smarty-date_diff-function/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>This is a function-type plugin for the Smarty template engine to calculate the difference between two dates in days (default), weeks, or years.</p>
<p>Download: <a href="/portfolio/files/function.date_diff.zip">Click here</a> and extract the file to your Smarty <code>"plugins/"</code> folder.</p>
<pre class="crayon-plain-tag"><code>&lt;?php
/*
* Smarty plugin
* -------------------------------------------------------------
* Type: function
* Name: date_diff
* Version: 2.0
* Date: June 22, 2008
* Author: Matt DeKok
* Purpose: factor difference between two dates in days, weeks,
*          or years
* Input: date1 = &quot;mm/dd/yyyy&quot; or &quot;yyyy/mm/dd&quot; or &quot;yyyy-mm-dd&quot;
*        date2 = &quot;mm/dd/yyyy&quot; or &quot;yyyy/mm/dd&quot; or &quot;yyyy-mm-dd&quot; or $smarty.now
*        assign = name of variable to assign difference to
*        interval = &quot;days&quot; (default), &quot;weeks&quot;, &quot;years&quot;
* Examples: {date_diff date1=&quot;5/12/2003&quot; date2=$smarty.now interval=&quot;weeks&quot;}
*           {date_diff date1=&quot;5/12/2003&quot; date2=&quot;5/10/2008&quot; assign=&quot;diff&quot;}{$diff}
* -------------------------------------------------------------
*/
function smarty_function_date_diff($params, &amp;$smarty)
{
    $date1 = mktime(0,0,0,1,1,2000);
    $date2 = mktime(0,0,0,date(&quot;m&quot;),date(&quot;d&quot;),date(&quot;Y&quot;));
    $assign = null;
    $interval = &quot;days&quot;;
    
    extract($params);

    $i = 1/60/60/24;
    if($interval == &quot;weeks&quot;)
    {
        $i = $i/7;
    }
    elseif($interval == &quot;years&quot;)
    {
        $i = $i/365.25;
    }
    
    $date1 = is_string($date1) ? strtotime($date1) : $date1;
    $date2 = is_string($date2) ? strtotime($date2) : $date2;
    
    if($assign != null)
    {
        $smarty-&gt;assign($assign,floor(($date2 - $date1)*$i));
    }
    else
    {
        return floor(($date2 - $date1)*$i);
    }
}
?&gt;</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://www.animestreamers.com/beta/portfolio/51/smarty-date_diff-function/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MySQL Database Class</title>
		<link>http://www.animestreamers.com/beta/portfolio/27/mysql-database-class/</link>
		<comments>http://www.animestreamers.com/beta/portfolio/27/mysql-database-class/#comments</comments>
		<pubDate>Mon, 16 Jan 2012 19:25:31 +0000</pubDate>
		<dc:creator>Matt DeKok</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://www.animestreamers.com/beta/portfolio/?p=27</guid>
		<description><![CDATA[This MySQL database class is probably one of the simplest database objects/classes to use for PHP. The number of lines of code that you need to use for any query is just 1-2! Any query at all can be called and return &#8230; <a href="http://www.animestreamers.com/beta/portfolio/27/mysql-database-class/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>This MySQL database class is probably one of the simplest database objects/classes to use for PHP. The number of lines of code that you need to use for any query is just 1-2! Any query at all can be called and return a result whether it be data or true/false. The query uses the secure PDO object to prepare input.</p>
<pre class="crayon-plain-tag"><code>class database
{
    public $db;                 //PDO Object
    
    const GET_RECORDSET = 0;
    const GET_RECORD = 1;
    const GET_VALUE = 2;
    
    function __construct($host, $username, $password, $database_name)
    {
        $dsn = &quot;mysql:host=$host;dbname=$database_name&quot;;
        $options = array(PDO::ATTR_ERRMODE =&gt; PDO::ERRMODE_EXCEPTION);
        try
        {
            @$db = new PDO($dsn, $username, $password, $options);
        }
        catch (PDOException $e)
        {
            $error = $e-&gt;getMessage();
            echo '&lt;div class=&quot;error&quot;&gt;&lt;strong&gt;PDO Error:&lt;/strong&gt;&lt;br /&gt;' . $error . '&lt;/div&gt;';
            exit;
        }
        $this-&gt;db = $db;
    }
    
    function query($sql_statement, $bind_values = array(), $return_type = self::GET_RECORDSET)
    {
        $query = $this-&gt;db-&gt;prepare($sql_statement);
        
        //bind dynamic values to the query string
        foreach($bind_values as $key =&gt; $value)
        {
            $dt = PDO::PARAM_STR;                               //value is a string
            if(is_int($value)) $dt = PDO::PARAM_INT;            //value is an integer
            elseif(is_bool($value)) $dt = PDO::PARAM_BOOL;      //value is a boolean
            $query-&gt;bindValue(&quot;:$key&quot;, $value, $dt);
        }
        
        //catch errors during execution and return an error message
        try
        {
            $result = $query-&gt;execute();
        }
        catch (PDOException $e)
        {
            $error = $e-&gt;getMessage();
            echo '&lt;div class=&quot;error&quot;&gt;&lt;strong&gt;SQL Error:&lt;/strong&gt; &lt;code&gt;' . $sql_statement . '&lt;/code&gt;&lt;br /&gt;' . $error . '&lt;/div&gt;';
            return false;
        }
        
        try
        {
            if($return_type == self::GET_RECORDSET)     //returns a 2-dimensional array
            {
                $data = $query-&gt;fetchAll();
            }
            elseif($return_type == self::GET_RECORD)    //returns a 1-dimensional array
            {
                $data = $query-&gt;fetch();
            }
            else                                        //returns a value
            {
                $data = $query-&gt;fetchColumn();
            }
        }
        catch (PDOException $e)
        {
            if(is_bool($result))       // If the result is a boolean, return true/false
            {
                $data = $result;
            }
            else
            {
                $error = $e-&gt;getMessage();
                echo '&lt;div class=&quot;error&quot;&gt;&lt;strong&gt;PDO Error:&lt;/strong&gt;&lt;br /&gt;' . $error . '&lt;/div&gt;';
                exit;
            }
        }
        
        //Closes the cursor, enabling the statement to be executed again.
        $query-&gt;closeCursor();
        
        return $data;
    }
}</code></pre>
<p>Calling the object is done like the following:</p>
<pre class="crayon-plain-tag"><code>define('DB_HOST','localhost');
define('DB_USER','username');
define('DB_PASS','password');
define('DB_NAME','database name');
$dbase = new database(DB_HOST, DB_USER, DB_PASS, DB_NAME);</code></pre>
<p>Executing a query that does not return data results in a <strong>true</strong> or <strong>false</strong> value. When inserting user input into the SQL string use the following method to prepare the variables so as to prevent SQL injection.</p>
<pre class="crayon-plain-tag"><code>$values = array(
    'input1' =&gt; $_POST['input1'],
    'name' =&gt; $_POST['name']
);

$result = $dbase-&gt;query(&quot;INSERT INTO table (input1, name) VALUES(:input1, :name);&quot;, $values);</code></pre><p><p>
<p>There are a few ways to return data. The first is a record set, which is returned into a mult-dimensional array. The first dimension is the record, and the second dimension is the fields of that record. This is the default. The next type is an individual record. It is returned into a single-dimensional array. The last type is a single value (no array).</p>
<pre class="crayon-plain-tag"><code>// a record
$values = array('user_id' =&gt; $_SESSION['user_id']);
$user = $dbase-&gt;query(&quot;SELECT * FROM users WHERE user_id = :user_id;&quot;, $values, database::GET_RECORD);

// a value
$values = array('user_id' =&gt; $_SESSION['user_id']);
$user_level = $dbase-&gt;query(&quot;SELECT user_level FROM users WHERE user_id = :user_id;&quot;, $values, database::GET_VALUE);</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://www.animestreamers.com/beta/portfolio/27/mysql-database-class/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

