<?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>cnicollonline &#187; PHP</title>
	<atom:link href="http://cnicollonline.com/category/programming/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://cnicollonline.com</link>
	<description>&#34;I was just guessing at numbers and figures, pulling the puzzles apart&#34;</description>
	<lastBuildDate>Mon, 23 Aug 2010 19:16:07 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Custom PHP exception handling</title>
		<link>http://cnicollonline.com/programming/custom-php-exception-handling/#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed</link>
		<comments>http://cnicollonline.com/programming/custom-php-exception-handling/#comments</comments>
		<pubDate>Mon, 23 Aug 2010 19:08:34 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[demo]]></category>
		<category><![CDATA[exception-handling]]></category>

		<guid isPermaLink="false">http://cnicollonline.com/?p=297</guid>
		<description><![CDATA[A large project I&#8217;ve been working on as of late has many moving parts. It allowed a lot of dynamic edits that utilized AJAX for sending dynamic information to and from a database. When errors occurred it was alright for me, because I could debug them however what it came the client&#8230; Well let&#8217;s just [...]]]></description>
			<content:encoded><![CDATA[<p>A large project I&#8217;ve been working on as of late has many moving parts. It allowed a lot of dynamic edits that utilized AJAX for sending dynamic information to and from a database. When errors occurred it was alright for me, because I could debug them however what it came the client&#8230; Well let&#8217;s just say they were convinced that with these errors their computer was going to spontaneous&nbsp;combust.</p>
<p>I&#8217;ll admit it now that I was a little lazy when developing some of the classes. I went back and modified them so that they would implement <a href="http://www.w3schools.com/php/php_exception.asp">exception handling</a>, only I went one step further and used custom exception handling so that I could log any errors that may&nbsp;occur.</p>
<p>Within this posting, im going to create a class that will have errors, and I&#8217;ll also create a custom exception class that will catch any of these errors and log them to a&nbsp;file</p>
<p style="text-align: center;"><img class="aligncenter" src="http://farm5.static.flickr.com/4073/4920516367_e94b52e37f.jpg" alt="cantcatch2" width="500" height="367" /></p>
<p><span id="more-297"></span></p>
<p>You can download the complete source <a href="http://cnicollonline.com/wp-downloads/08-23-10_12-03_php_custom_exception.zip#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed">here</a>. This class will be a <span style="text-decoration: underline;">very</span> basic SQL class that&nbsp;will:</p>
<ul>
<li>Connect to a&nbsp;database</li>
<li>Disconnect from a&nbsp;database</li>
<li>Select from a&nbsp;database</li>
</ul>
<p>The directory setup after this tutorial should look as&nbsp;follows:</p>
<ul>
<h4>Directory&nbsp;Setup</h4>
<li>index.php</li>
<li>/lib/
<ul>
<li>SQL.class.php</li>
<li>SQL.exception.class.php</li>
</ul>
</li>
<li>/sql/
<ul>
<li>world.sql</li>
</ul>
</li>
<li>SQLException.log</li>
</ul>
<p><em>Make sure you have the world database installed within your database. You can download it <a href="http://dev.mysql.com/doc/world-setup/en/world-setup.html">here</a> or find it in the download of the completed&nbsp;source.</em></p>
<h2>The SQL&nbsp;class</h2>
<p>Before I jump into the mix, this is written for people whom already have a basic grasp on any OOP language, as well as some of the basics of exception&nbsp;handling.</p>
<h4>SQL.class.php</h4>
<pre class="brush: php;">
&lt;?php

include('SQL.exception.class.php');

class SQL {

	private $username;
	private $password;
	private $database;
	private $connection;

	/**
	*
	*
	*/
	public function  __construct($u,$p,$d) {
		$this-&gt;username = $u;
		$this-&gt;password = $p;
		$this-&gt;database = $d;
		$this-&gt;connection = false;
	}

	public function connect() {
		if(!$this-&gt;connection) {
			// use @ to surpress the error
			if(!@mysql_connect('localhost',$this-&gt;username,$this-&gt;password)) {
				throw new SQLException('Can\'t connect to the database, either the username or password are wrong');
			}

			// use @ to surpress the error
			if (!@mysql_select_db($this-&gt;database)) {
				throw new SQLException('The database '.$this-&gt;database.' does not exist.');
			}

			$this-&gt;connection = true;
		}
	}

	public function disconnect() {
		if($this-&gt;connection) {
			mysql_close();
			$this-&gt;connection = false;
		}
	}

	public function getCity($city) {
		$cities = array();

		$query = 'SELECT * FROM City WHERE NAME ='.&quot;'$city'&quot;;
		$results = @mysql_query($query);
		if ($results) {
			while($row = mysql_fetch_array($results)) {
				$cities[] = $row;
			}

			if (sizeof($cities) &gt; 0) {
				return $cities;
			}
			else {
				throw new SQLException('The following city: '.$city.' was not found',$query);
			}
		}
		else {
			throw new SQLException('Error with query when trying to get a city: ',$query);
		}
	}
}

?&gt;
</pre>
<p>Theres the SQL class. Theres a few things in here that I should make note of. First, I&#8217;ve included the SQLException custom class, we have yet to create that, so hang on for now! Secondly the way that a try catch block works, is that it needs to try to do something and if that something does not work, it will catch that exception to better handle it. In order to catch an exception one must be thrown. We&#8217;re throwing the exceptions anytime we feel an error might need to be&nbsp;reported.</p>
<h2>Next up the custom exception&nbsp;class</h2>
<p>Create a new file within your lib folder called&nbsp;&#8220;SQL.exception.class.php&#8221;</p>
<h4>SQL.exception.class.php</h4>
<pre class="brush: php;">
&lt;?php
class SQLException extends Exception {

	// path to the log file
	private $log_file;

	public function __construct($message=NULL, $query = NULL) {

		// NOTE:: THIS MAY NEED TO CHANGE
		$this-&gt;log_file =  $_SERVER['DOCUMENT_ROOT'].'/'.$_SERVER['REQUEST_URI'].'/SQLException.log';

		$code = mysql_errno();
		$sql_error = mysql_error();

		// open the log file for appending
		if ($fp = fopen($this-&gt;log_file,'a')) {

			// construct the log message
			$log_msg = date(&quot;[Y-m-d H:i:s]&quot;) .
				&quot; Code: $code &quot; .
				&quot; || Message: $message&quot;.
				&quot; || SQL error: $sql_error \n&quot;.
				&quot;Query: $query\n-------\n&quot;;

			fwrite($fp, $log_msg);

			fclose($fp);
		}

		// call parent constructor
		parent::__construct($message, $code);
	}

}
?&gt;
</pre>
<p>All the magic here happens when we extend PHP&#8217;s Exception class. Moving down to the class constructor, I&#8217;ve set up two parameters. <code>$message</code> takes the message that will be passed into the parent constructor. This message will be shown to the user to better explain whats going. The second parameter <code>$query</code> is meant for the log file. This is passed in so that we as the developers can view the query that may have been botched. Moving into the constructor now, it&#8217;s important to provide a path to the logfile, so the first line of code we see does just that. Next the two variables <code>$code</code> and <code>$sql_error</code> grab a little more information for the log file. The las bit of code opens the log file, appends a new entry, saves and closes it. To finish the class off, run the parents&nbsp;constructor.</p>
<h2>Finally the index or testing&nbsp;file.</h2>
<p>This is the index file that will be within our root directory. It contains sets of variables to help generate exceptions. This file is also where we &#8220;try&#8221; to complete a statement and if anything goes wrong we &#8220;catch&#8221; that exception, kill the script and explain why things didn&#8217;t work&nbsp;out.</p>
<h4>index.php</h4>
<pre class="brush: php;">
&lt;?php
include_once('lib/SQL.class.php');

// ==================== EXCEPTION ERROR ====================
// switch these two database variables to see a exception error occur
$database = 'world';
//$database = 'wrongDB';			// &lt;--- this will result in an exception error when uncommented.

// ==================== EXCEPTION ERROR ====================
// switch these two username variables to see a exception error occur
$username = 'root';
//$username = 'wrongUserName';		// &lt;--- this will result in an exception error when uncommented.

// ==================== EXCEPTION ERROR ====================
// switch these two username variables to see a exception error occur
$password = 'root';
//$password = 'wrongPassword';		// &lt;--- this will result in an exception error when uncommented.

// ==================== EXCEPTION ERROR ====================
// switch these two city variables to see a exception error occur
$city = 'Vancouver';
//$city = 'Gotham City';			// &lt;--- this will result in an exception error when uncommented.

$db = new SQL($username,$password,$database);

/**
* try catch block.
* try - to connect to the database and once connected, disconnect.
* catch - any exception that will occur and kill the script.
*/
try {
	$db-&gt;connect();
	$db-&gt;disconnect();
} catch (SQLException $e) {
	$db-&gt;disconnect();
	die($e-&gt;getMessage());
}

/**
* try catch block
* try to connect to the database
* try to run a select query (note: to get an exception error change the city variable above)
* print the query
* disconnect
* catch any error that may occur.
*/
try {
	$db-&gt;connect();
	$r = $db-&gt;getCity($city);

	// print the results
	echo '&lt;pre&gt;';
		print_r($r);
	echo '&lt;/pre&gt;';

	$db-&gt;disconnect();
} catch (SQLException $e) {
	$db-&gt;disconnect();
	die($e-&gt;getMessage());
}

?&gt;
</pre>
<h2>Conclusion</h2>
<p>Exception handling is a fun subject. The possibilities of being able to recover from errors that might occur create endless possibilities when scripting, just don&#8217;t do what I do and leave it all to the bitter&nbsp;end!</p>
]]></content:encoded>
			<wfw:commentRss>http://cnicollonline.com/programming/custom-php-exception-handling/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Class: File Details released</title>
		<link>http://cnicollonline.com/programming/class-file-details-released/#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed</link>
		<comments>http://cnicollonline.com/programming/class-file-details-released/#comments</comments>
		<pubDate>Mon, 15 Jun 2009 19:41:16 +0000</pubDate>
		<dc:creator>Cosizzle</dc:creator>
				<category><![CDATA[Class]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Snippits]]></category>
		<category><![CDATA[arrays]]></category>

		<guid isPermaLink="false">http://cnicollonline.com/?p=209</guid>
		<description><![CDATA[I was tasked with a project at work in which I had to build a site that would be quite dynamic. The problem however was that I could not use a database to hold information. What I did (and something I would avoid if I could) was create a class that looks within a folder [...]]]></description>
			<content:encoded><![CDATA[<p><!--<br />
- WORDPRESS TEMPLATE FOR CREATING OR POSTING TO MY BLOG.<br />
- COMMON TAGS TO&nbsp;NOTE:</p>
<p>-<br />
<h2>MAIN&nbsp;TITLE</h2>
<p>-<br />
<h3></h3>
<p>-<br />
<h4></h4>
<p>06-15-09_00-00_fileDetails<br />
-
<ul>
<li>LIST&nbsp;ITEM</li>
</ul>
<p>-
<ol>
<li>ORDER LIST&nbsp;ITEM</li>
</ol>
<p>- adding a download link<br />
<a href="http://www.cnicollonline.com/wp-articles/PATH TO LINK" target="_self">click&nbsp;here</a></p>
<p>- adding an&nbsp;image</p>
<div class="tutorial_image"><img src="http://www.cnicollonline.com/wp-articles/PATH TO IMG/1.jpg" border="0" /></div>
<p>--></p>
<p>I was tasked with a project at work in which I had to build a site that would be quite dynamic. The problem however was that I could not use a database to hold information. What I did (and something I would avoid if I could) was create a class that looks within a folder thats specified and builds an array off the files within the&nbsp;folder.</p>
<p><span id="more-209"></span></p>
<h3>How it&nbsp;works</h3>
<p>Reads a folder of files, breaks apart the file name by a specified &#8220;breaker&#8221;. It then creates a new array off the specified file name&nbsp;attributes</p>
<div class="tutorial_image"><img src="http://www.cnicollonline.com/wp-articles/06-15-09_00-00_fileDetails/fileInfo.jpg" border="0" /></div>
<h3>Demo</h3>
<p>File&nbsp;setup:</p>
<pre class="brush: php;">
include_once('FileDetail.class.php');
// a new object reference using an array for the parameter which specifies the file name attributes
// FileDetail(array(attributes));
$files = new FileDetail(array('id','movie','name','ext'));

// loadFiles(Folder, separator)
$files-&gt;loadFiles('test_files2','_');

// New variable to hold the file name array
$fileNames = $files-&gt;getFileNames();

// New Variable to hold the file details array
$fileDetails = $files-&gt;getFileDetails();
</pre>
<p>Testing the&nbsp;arrays:</p>
<pre class="brush: php;">
// ========================================= EXAMPLE =========================================
echo '&lt;h4&gt;File Names Test 2&lt;/h4&gt;';
echo '&lt;pre&gt;';
	print_r($fileNames);
echo '&lt;/pre&gt;';

// ========================================= EXAMPLE =========================================
echo '&lt;h4&gt;File Details&lt;/h4&gt;';
echo '&lt;pre&gt;';
	print_r($fileDetails);
echo '&lt;/pre&gt;';
</pre>
<p><strong>File&nbsp;Names</strong></p>
<pre>
Array
(
    [0] => 01_wall-e_walle02.jpg
    [1] => 02_batman_joker02.jpg
    [2] => 02_wall-e_walle.jpg
    [3] => 04_batman_joker02.jpg
    [4] => 05_batman_joker.jpg
)
</pre>
<p><strong>File&nbsp;Details</strong></p>
<pre>
Array
(
    [0] => Array
        (
            [id] => 01
            [movie] => wall-e
            [name] => walle02
            [ext] => jpg
        )

    [1] => Array
        (
            [id] => 02
            [movie] => batman
            [name] => joker02
            [ext] => jpg
        )

    [2] => Array
        (
            [id] => 02
            [movie] => wall-e
            [name] => walle
            [ext] => jpg
        )

    [3] => Array
        (
            [id] => 04
            [movie] => batman
            [name] => joker02
            [ext] => jpg
        )

    [4] => Array
        (
            [id] => 05
            [movie] => batman
            [name] => joker
            [ext] => jpg
        )

)
</pre>
<p>Real World&nbsp;Example:</p>
<pre class="brush: php;">
echo '&lt;h4&gt;Get files of a specific parameter&lt;/h4&gt;';
echo '&lt;h5&gt; movie -&gt; batman&lt;/h5&gt;';
echo '&lt;pre&gt;';
	print_r($files-&gt;getFilesByKey('movie','batman'));
echo '&lt;/pre&gt;';
</pre>
<p><strong>Get files of a specific parameter (movies->batman)</strong></p>
<pre>
Array
(
    [0] => Array
        (
            [id] => 02
            [movie] => batman
            [name] => joker02
            [ext] => jpg
        )

    [1] => Array
        (
            [id] => 04
            [movie] => batman
            [name] => joker02
            [ext] => jpg
        )

    [2] => Array
        (
            [id] => 05
            [movie] => batman
            [name] => joker
            [ext] => jpg
        )

)
</pre>
<h3>Conclusion</h3>
<p>The class can be used in many which ways whether it&#8217;s loading a gallery of images or building a website based off of the attributes of the images. The class and demo files can be downloaded&nbsp;<a href="http://www.cnicollonline.com/wp-articles/06-15-09_00-00_fileDetails/fileDetails.zip">here</a></p>
]]></content:encoded>
			<wfw:commentRss>http://cnicollonline.com/programming/class-file-details-released/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Arrays and Loops with php [Level 2]</title>
		<link>http://cnicollonline.com/programming/arrays-and-loops-with-php-level-2/#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed</link>
		<comments>http://cnicollonline.com/programming/arrays-and-loops-with-php-level-2/#comments</comments>
		<pubDate>Fri, 05 Jun 2009 18:27:01 +0000</pubDate>
		<dc:creator>Cosizzle</dc:creator>
				<category><![CDATA[Notes]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[arrays]]></category>
		<category><![CDATA[loops]]></category>
		<category><![CDATA[multidimensional arrays]]></category>

		<guid isPermaLink="false">http://cnicollonline.com/?p=204</guid>
		<description><![CDATA[A few weeks ago I had wrote Arrays and Loops with php [Level 1]. This topic is expanding on that idea. I&#8217;ll be covering more complex array examples using 2D multidimensional&#160;arrays. Multidimensional&#160;Arrays The idea of a multidimensional array is something that can easily confuse someone, especially when they become complex with information (ie. 2D and [...]]]></description>
			<content:encoded><![CDATA[<p><!--<br />
- WORDPRESS TEMPLATE FOR CREATING OR POSTING TO MY BLOG.<br />
- COMMON TAGS TO&nbsp;NOTE:</p>
<p>-<br />
<h2>MAIN&nbsp;TITLE</h2>
<p>-<br />
<h3></h3>
<p>-<br />
<h4></h4>
<p>-
<ul>
<li>LIST&nbsp;ITEM</li>
</ul>
<p>-
<ol>
<li>ORDER LIST&nbsp;ITEM</li>
</ol>
<p>- adding a download link<br />
<a href="http://www.cnicollonline.com/wp-articles/PATH TO LINK" target="_self">click&nbsp;here</a></p>
<p>- adding an&nbsp;image</p>
<div class="tutorial_image"><img src="http://www.cnicollonline.com/wp-articles/PATH TO IMG/1.jpg" border="0" /></div>
<p>--></p>
<p>A few weeks ago I had wrote <a href="http://cnicollonline.com/?p=186#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed" target="_self">Arrays and Loops with php [Level 1].</a> This topic is expanding on that idea. I&#8217;ll be covering more complex array examples using 2D multidimensional&nbsp;arrays.</p>
<p><span id="more-204"></span></p>
<h3>Multidimensional&nbsp;Arrays</h3>
<p>The idea of a multidimensional array is something that can easily confuse someone, especially when they become complex with information (ie. 2D and 3D arrays). I like to think of multidimensional arrays as a miniature&nbsp;database.</p>
<p>A multidimensional array can hold multiple values to a single key. Take a look at the output&nbsp;below.</p>
<pre>Array
(
    [0] => Array
        (
            [name] => Cody
            [gender] => male
            [colour] => green
            [fruit] => apples
            [sport] => frisbee
        )

    [1] => Array
        (
            [name] => Sam
            [gender] => male
            [colour] => blue
            [fruit] => oranges
            [sport] => golf
        )

    [2] => Array
        (
            [name] => Jenny
            [gender] => female
            [colour] => pink
            [fruit] => pineapple
            [sport] => hockey
        )

)
</pre>
<p>This example is an example of a 2D array using key values. By default PHP will auto fill the keys, but this can lead to confusion down the road. To set up the&nbsp;array: </p>
<pre class="brush: php;">
$people = array( array(&quot;name&quot;=&gt;&quot;Cody&quot;,
						&quot;gender&quot;=&gt;&quot;male&quot;,
						&quot;colour&quot;=&gt;&quot;green&quot;,
						&quot;fruit&quot;=&gt;&quot;apples&quot;,
						&quot;sport&quot;=&gt;&quot;frisbee&quot;),
				array(&quot;name&quot;=&gt;&quot;Sam&quot;,
					&quot;gender&quot;=&gt;&quot;male&quot;,
						&quot;colour&quot;=&gt;&quot;blue&quot;,
						&quot;fruit&quot;=&gt;&quot;oranges&quot;,
						&quot;sport&quot;=&gt;&quot;golf&quot;),
				array(&quot;name&quot;=&gt;&quot;Jenny&quot;,
						&quot;gender&quot;=&gt;&quot;female&quot;,
						&quot;colour&quot;=&gt;&quot;pink&quot;,
						&quot;fruit&quot;=&gt;&quot;pineapple&quot;,
						&quot;sport&quot;=&gt;&quot;hockey&quot;)
				);

echo '&lt;pre&gt;';
	print_r($people);
echo '&lt;/pre&gt;';
</pre>
<p>There&#8217;s a lot going on, take some time and read through it. First we set up a main container that will hold all of our information
<pre>$people</pre>
<p> we then put a new array within our already created&nbsp;array.</p>
<p>Retrieving a single value is quite easy. We just call on what array element we want, then we ask for a specific property, in this example the&nbsp;name.</p>
<pre class="brush: php;">
echo '&lt;pre&gt;';
echo $people[0]['name'];
echo '&lt;/pre&gt;';
</pre>
<h3>How this can be&nbsp;used</h3>
<p>By looping of course! We can loop through this in many different ways to provide an&nbsp;example:</p>
<pre class="brush: php;">
for ($i=0; $i&lt;sizeOf($people); $i++) {
	$string = $people[$i]['name'] . &quot;'s favorite fruit is &quot; . $people[$i]['fruit'];
	if ($people[$i]['gender'] === &quot;male&quot;) {
		$string .= &quot; his favorite colour is &quot; . $people[$i]['colour'] . &quot; and he enjoys to play &quot; . $people[$i]['sport'];
	}
	else if ($people[$i]['gender'] === &quot;female&quot;) {
		$string .= &quot; her favorite colour is &quot; . $people[$i]['colour'] . &quot; and she enjoys to play &quot; . $people[$i]['sport'];
	}
	echo $string;
	echo '&lt;br&gt;';
}
</pre>
<p>output:</p>
<pre>
Cody's favorite fruit is apple his favorite colour is green and he enjoys to play frisbee
Sam's favorite fruit is oranges his favorite colour is blue and he enjoys to play golf
Jenny's favorite fruit is pineapple her favorite colour is pink and she enjoys to play hockey
</pre>
<p>Here I used a if statement within my for loop to check the gender value. If it were a male I had the output specify &#8220;he&#8221;, and the same check for a&nbsp;female.</p>
<p>Another use for a multidimensional array is a table layout, or&nbsp;grid.</p>
<pre class="brush: php;">
$grid = array(array('Cody','male','green'),
			array('Sam','male','blue'),
			array('Jenny', 'female','pink')
);
for ($r=0; $r&lt;sizeOf($grid); $r++) {
	for($c=0; $c&lt;sizeOf($grid[$r]); $c++) {
		echo $grid[$r][$c]. &quot; | &quot;;
	}
	echo '&lt;br&gt;';
}
</pre>
<p>output:</p>
<pre>
Cody | male | green |
Sam | male | blue |
Jenny | female | pink |
</pre>
<h3>Conclusion</h3>
<p>multidimensional arrays make for an interesting topic, there is so much you can do, but without doing they can be hard to explain. Here is the source to help you get&nbsp;started.</p>
<pre class="brush: php; collapse: true; light: false; toolbar: true;">
&lt;?php
$people = array( array(&quot;name&quot;=&gt;&quot;Cody&quot;,
						&quot;gender&quot;=&gt;&quot;male&quot;,
						&quot;colour&quot;=&gt;&quot;green&quot;,
						&quot;fruit&quot;=&gt;&quot;apples&quot;,
						&quot;sport&quot;=&gt;&quot;frisbee&quot;),
				array(&quot;name&quot;=&gt;&quot;Sam&quot;,
					&quot;gender&quot;=&gt;&quot;male&quot;,
						&quot;colour&quot;=&gt;&quot;blue&quot;,
						&quot;fruit&quot;=&gt;&quot;oranges&quot;,
						&quot;sport&quot;=&gt;&quot;golf&quot;),
				array(&quot;name&quot;=&gt;&quot;Jenny&quot;,
						&quot;gender&quot;=&gt;&quot;female&quot;,
						&quot;colour&quot;=&gt;&quot;pink&quot;,
						&quot;fruit&quot;=&gt;&quot;pineapple&quot;,
						&quot;sport&quot;=&gt;&quot;hockey&quot;)
				);

echo '&lt;pre&gt;';
	print_r($people);
echo '&lt;/pre&gt;';

// Get a single value
echo '&lt;pre&gt;';
echo $people[0]['name'];
echo '&lt;/pre&gt;';
// ================================================= LOOP =================================================
for ($i=0; $i&lt;sizeOf($people); $i++) {
	$string = $people[$i]['name'] . &quot;'s favorite fruit is &quot; . $people[$i]['fruit'];
	if ($people[$i]['gender'] === &quot;male&quot;) {
		$string .= &quot; his favorite colour is &quot; . $people[$i]['colour'] . &quot; and he enjoys to play &quot; . $people[$i]['sport'];
	}
	else if ($people[$i]['gender'] === &quot;female&quot;) {
		$string .= &quot; her favorite colour is &quot; . $people[$i]['colour'] . &quot; and she enjoys to play &quot; . $people[$i]['sport'];
	}
	echo $string;
	echo '&lt;br&gt;';
}

// ================================================= GRID =================================================
$grid = array(array('Cody','male','green'),
			array('Sam','male','blue'),
			array('Jenny', 'female','pink')
);
for ($r=0; $r&lt;sizeOf($grid); $r++) {
	for($c=0; $c&lt;sizeOf($grid[$r]); $c++) {
		echo $grid[$r][$c]. &quot; | &quot;;
	}
	echo '&lt;br&gt;';
}

?&gt;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://cnicollonline.com/programming/arrays-and-loops-with-php-level-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Arrays and Loops with php [Level 1]</title>
		<link>http://cnicollonline.com/programming/arrays-and-loops-with-php-level-1/#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed</link>
		<comments>http://cnicollonline.com/programming/arrays-and-loops-with-php-level-1/#comments</comments>
		<pubDate>Tue, 26 May 2009 18:19:43 +0000</pubDate>
		<dc:creator>Cosizzle</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[arrays]]></category>
		<category><![CDATA[loops]]></category>

		<guid isPermaLink="false">http://cnicollonline.com/?p=186</guid>
		<description><![CDATA[Way back when I started programming there were two things I just could not wrap my head around. Unfortunately they fell hand in hand which made doing an alternative quote tedious. Loops and arrays were nightmares. This post is outlines a few small pointers I&#8217;ve picked up along the&#160;way. What is an array, what is [...]]]></description>
			<content:encoded><![CDATA[<p><!--<br />
- WORDPRESS TEMPLATE FOR CREATING OR POSTING TO MY BLOG.<br />
- COMMON TAGS TO&nbsp;NOTE:</p>
<p>-<br />
<h2>MAIN&nbsp;TITLE</h2>
<p>-<br />
<h3></h3>
<p>-<br />
<h4></h4>
<p>-
<ul>
<li>LIST&nbsp;ITEM</li>
</ul>
<p>-
<ol>
<li>ORDER LIST&nbsp;ITEM</li>
</ol>
<p>- adding a download link<br />
<a href="http://www.cnicollonline.com/wp-articles/PATH TO LINK" target="_self">click&nbsp;here</a></p>
<p>- adding an&nbsp;image</p>
<div class="tutorial_image"><img src="http://www.cnicollonline.com/wp-articles/PATH TO IMG/1.jpg" border="0" /></div>
<p>--></p>
<p>Way back when I started programming there were two things I just could not wrap my head around. Unfortunately they fell hand in hand which made doing an alternative quote tedious. Loops and <a href="http://www.php.net/array" target="_blank">arrays</a> were nightmares. This post is outlines a few small pointers I&#8217;ve picked up along the&nbsp;way.</p>
<p><span id="more-186"></span></p>
<h3>What is an array, what is a&nbsp;loop?</h3>
<p>The best way to think of an <a href="http://www.php.net/array" target="_blank">array</a> is as a container, or box. The box (<a href="http://www.php.net/array" target="_blank">array</a>) can hold values and objects for safe keeping, we can put things into the box, and we can take them out. We do this by using a <strong>loop.</strong> The loop will cycle through the <a href="http://www.php.net/array" target="_blank">array</a>, and within the loop we can perform actions to all the objects, a group of objects or any single&nbsp;object.</p>
<p>There are three types of loops are the <a href="http://ca2.php.net/manual/en/control-structures.for.php" target="_blank">for loop</a>, the <a href="http://ca2.php.net/manual/en/control-structures.foreach.php" target="_blank">foreach</a>, and the <a href="http://ca.php.net/while" target="_blank">while loop</a>. What loop to use and when I belive comes with experience and practice. The three loops are similar in what they do, but depending when you use them, or how they&#8217;re used is the tricky&nbsp;part.</p>
<h3>Defining an&nbsp;Array</h3>
<p>I stated earlier that an array was simply a collection of values or&nbsp;objects.</p>
<pre class="brush: php;">
$fruits = array(&quot;apples&quot;, &quot;oranges&quot;, &quot;pears&quot;, &quot;cherries&quot;);
</pre>
<p>Thats all there is to an array, I have it stored within a variable that acts as the container. Now how do we access these delicious&nbsp;fruits?</p>
<pre class="brush: php;">
echo $fruits[0];
echo '&lt;br&gt;';
echo $fruits[1];
echo '&lt;br&gt;';
echo $fruits[2];
echo '&lt;br&gt;';
echo $fruits[3];
</pre>
<p><strong>NOTE:</strong>This is not a loop but its a wonderful method to quickly print out all the elements within an array. I use this a lot when developing and&nbsp;debugging.</p>
<pre class="brush: php;">
// print array
echo '&lt;pre&gt;';
	print_r($fruits); // print array
echo '&lt;/pre&gt;';
</pre>
<p><em>outputs:</em></p>
<pre>Array
(
    [0] => apples
    [1] => oranges
    [2] => pears
    [3] => cherries
)
</pre>
<p><strong>What makes an array?</strong> You can see from the output above that there are two elements within the array [key]=>[value]. Each array element is assigned a unique key. The key is how we can call any given member within the&nbsp;array.</p>
<p><strong>How do we add a new fruit?</strong> To add something to our array we call the array, and give it a new value in an empty index position (php will do the math for&nbsp;you)</p>
<pre class="brush: php;">
$fruits[] = 'grapes';
$fruits[] = 'mango';
</pre>
<p>printing the array again we can now see that indeed our fruits have been&nbsp;added.</p>
<pre>Array
(
    [0] => apples
    [1] => oranges
    [2] => pears
    [3] => cherries
    [4] => grapes
    [5] => mango
)
</pre>
<h3>Working with&nbsp;loops</h3>
<p>You might be asking yourself &#8220;Well how is that any easier then writing it out?&#8221;. Thats when loops become a handy tool, we can simply call any of the three loops to loop through the array and print everything within the array (opposed to printing them individually using&nbsp;$fruits[x])</p>
<p><a href="http://ca2.php.net/manual/en/control-structures.foreach.php" target="_blank">foreach loop:</a> Perhaps as far as this demo goes the minimalist and simplest loop. It basically iterates over each element and outputs its&nbsp;value.</p>
<pre class="brush: php;">
// foreach
foreach ($fruits as $loop) {
	echo $loop;
	echo '&lt;br&gt;';
}
echo '&lt;hr&gt;';
</pre>
<p>The next two loops are similar in what they do. They both allow for more control within the loop allowing you to perform actions based on conditional&nbsp;statements.</p>
<p><a href="http://ca2.php.net/manual/en/control-structures.for.php" target="_blank">for&nbsp;loop</a></p>
<pre class="brush: php;">
// for loop
for ($i=0; $i&lt;sizeOf($fruits); $i++) {
	echo $fruits[$i];
	echo '&lt;br&gt;';
}
echo '&lt;hr&gt;';
</pre>
<p><a href="http://ca.php.net/while" target="_blank">while&nbsp;loop</a></p>
<pre class="brush: php;">
// while loop
$count = 0;
while ($count &lt; sizeOf($fruits)) {
	echo $fruits[$count];
	echo '&lt;br&gt;';
	$count++;
}
</pre>
<p>Theres so much you can do with loops and still I struggle on getting things done with them. I wanted to keep this post simple, there are still multidimensional arrays which add a whole layer of complexcity to the topic. Hopefully I&#8217;ll continue this topic&nbsp;soon.</p>
<p>Heres the full&nbsp;script</p>
<pre class="brush: php; collapse: true; light: false; toolbar: true;">
&lt;?php

// fruit array
$fruits = array(&quot;apples&quot;, &quot;oranges&quot;, &quot;pears&quot;, &quot;cherries&quot;);

// getting the fruit
echo $fruits[0];
echo '&lt;br&gt;';
echo $fruits[1];
echo '&lt;br&gt;';
echo $fruits[2];
echo '&lt;br&gt;';
echo $fruits[3];

// print array
echo '&lt;pre&gt;';
	print_r($fruits); // print array
echo '&lt;/pre&gt;';

// add to the array
$fruits[] = 'grapes';
$fruits[] = 'mango';

// print array
echo '&lt;pre&gt;';
	print_r($fruits); // print array
echo '&lt;/pre&gt;';

// ================================================= LOOPS =================================================
echo '&lt;hr&gt;';

// foreach
foreach ($fruits as $loop) {
	echo $loop;
	echo '&lt;br&gt;';
}
echo '&lt;hr&gt;';

// for loop
for ($i=0; $i&lt;sizeOf($fruits); $i++) {
	echo $fruits[$i];
	echo '&lt;br&gt;';
}
echo '&lt;hr&gt;';

// while loop
$count = 0;
while ($count &lt; sizeOf($fruits)) {
	echo $fruits[$count];
	echo '&lt;br&gt;';
	$count++;
}
?&gt;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://cnicollonline.com/programming/arrays-and-loops-with-php-level-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Phone Validation</title>
		<link>http://cnicollonline.com/programming/phone-validation/#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed</link>
		<comments>http://cnicollonline.com/programming/phone-validation/#comments</comments>
		<pubDate>Thu, 14 May 2009 17:06:50 +0000</pubDate>
		<dc:creator>Cosizzle</dc:creator>
				<category><![CDATA[Notes]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Snippits]]></category>
		<category><![CDATA[demo]]></category>
		<category><![CDATA[download]]></category>
		<category><![CDATA[phone]]></category>

		<guid isPermaLink="false">http://cnicollonline.com/?p=174</guid>
		<description><![CDATA[I tend to do a lot of work with database entry through PHP forums. Over time I&#8217;ve seen nemorous ways in which someone will enter their phone&#160;number. The&#160;Problem Because there are so many different ways to enter a phone number. How it&#8217;s handled, processed and sent to the database becomes a challenge, and as a [...]]]></description>
			<content:encoded><![CDATA[<!--
- WORDPRESS TEMPLATE FOR CREATING OR POSTING TO MY BLOG.
- COMMON TAGS TO NOTE:

- <h2>MAIN TITLE</h2>
- <h3></h3>
- <h4></h4>

- <ul><li>LIST ITEM</li></ul>

- <ol><li>ORDER LIST ITEM</li></ol>

- adding a download link
<a href="http://www.cnicollonline.com/wp-articles/PATH TO LINK" target="_self">click here</a>

- adding an image
<div class="tutorial_image"><img src="http://www.cnicollonline.com/wp-articles/PATH TO IMG/1.jpg" border="0" /></div>
-->

<p>I tend to do a lot of work with database entry through PHP forums. Over time I've seen nemorous ways in which someone will enter their phone number.</p>

<span id="more-174"></span>

<h2>The Problem</h2>
<p>Because there are so many different ways to enter a phone number. How it's handled, processed and sent to the database becomes a challenge, and as a programmer you have to ask "what is the best way".</p>
<h2>The Solution</h2>
<p><p>The first solution would be to make your column in which will hold the phone number within your database a varchar(20). I suppose for something quick and dirty it will work, but in no means is this efficient.</p>
<p>The second solution would be to take a number:</p>
<ul>
	<li>(555)555-5555</li>
	<li>555.555.5555</li>
	<li>555 555 5555</li>
	<li>55 55  55 5555</li>
</ul>
<p>and remove unneeded characters and insert it into the database as a int value of 5555555555</p></p>
<p>The class:</p>
[php]
<br />
<b>Fatal error</b>:  Cannot redeclare class PhoneValidate in <b>/home/cnicollo/public_html/wp-content/plugins/php-execution-plugin/includes/class.php_execution.php(273) : eval()'d code</b> on line <b>51</b><br />

