<?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>PHP Starter &#187; Kurtis</title>
	<atom:link href="http://phpstarter.net/author/kazimmerman/feed/" rel="self" type="application/rss+xml" />
	<link>http://phpstarter.net</link>
	<description>PHP Tips &#38; Tools From Starters to Experts</description>
	<lastBuildDate>Fri, 25 Jun 2010 14:14:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Working with AJAX</title>
		<link>http://phpstarter.net/2009/03/working-with-ajax/</link>
		<comments>http://phpstarter.net/2009/03/working-with-ajax/#comments</comments>
		<pubDate>Thu, 05 Mar 2009 06:00:24 +0000</pubDate>
		<dc:creator>Kurtis</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Intermediate]]></category>

		<guid isPermaLink="false">http://phpstarter.net/?p=409</guid>
		<description><![CDATA[AJAX is all over the internet today, and though completely unnecessary, a website with it can function much more quickly. Data can be transferred to and from users efficiently, but a major drawback is the extra code needed and some security issues inherent. Nevertheless, for certain applications, AJAX is great to know and take your [...]
No related posts.]]></description>
			<content:encoded><![CDATA[<p>AJAX is all over the internet today, and though completely unnecessary, a website with it can function much more quickly.  Data can be transferred to and from users efficiently, but a major drawback is the extra code needed and some security issues inherent.  Nevertheless, for certain applications, AJAX is great to know and take your project to a new level.<span id="more-409"></span>The truth is that tutorials for learning the basics of AJAX are all over the internet.  For the quick setup of JavaScript, check out <a href="http://www.google.com/search?q=php+ajax+tutorial&amp;ie=utf-8&amp;oe=utf-8&amp;aq=t&amp;rls=org.mozilla:en-US:official&amp;client=firefox-a" target="_blank">any of these</a>.  What I want to focus on are more practical applications of the PHP end of AJAX.  Surely you can understand the basics of passing variables to a PHP processing page, but let&#8217;s go over a few examples to understand what AJAX can accomplish for us.<br />
First of all, a soon-to-come feature on <a href="http://www.jeoreview.com">JeoReview</a> is on the page where boards can be created.  The issue is that there is a chance that someone will pick a board name that is already in use, and while the PHP is set up to catch the error, I would like for JavaScript to be able to handle it earlier and more quickly to prevent posting to pages over and over again.  So we will set up the JavaScript much like the examples show:</p>
<pre class="brush: javascript">
var xmlHttp;

function checkName(name) {
var url=&quot;checkName.php&quot;;
url=url+&quot;?n=&quot;+name;
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open(&quot;GET&quot;,url,true);
xmlHttp.send(null);
}

function stateChanged()
{
if (xmlHttp.readyState==4 || xmlHttp.readyState==&quot;complete&quot;)
{
document.getElementById(&quot;nStatus&quot;).innerHTML=xmlHttp.responseText;
}
}
function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject(&quot;Msxml2.XMLHTTP&quot;);
}
catch (e)
{
xmlHttp=new ActiveXObject(&quot;Microsoft.XMLHTTP&quot;);
}
}

return xmlHttp;
}</pre>
<p>I would place this into a separate JavaScript file and link it to the page where the action takes place, but all that is really needed from this point is to add an event trigger to the form field to execute this function.  Something like this:</p>
<pre class="brush: php">

...

&lt;input type=&#039;text&#039; value=&#039;&#039; onchange=&#039;checkName(this.value);&#039; /&gt;

&lt;p id=&#039;nStatus&#039;&gt;Please enter a name for your board.&lt;/p&gt;

...
</pre>
<p>You can see then that the value from the form field will be checked in the function which will send the data to a PHP file which we must now define.  Whatever that PHP file &#8220;responds&#8221; with (or echoes back) will be displayed in the &#8216;p&#8217; tag we defined below the input field.  So let&#8217;s finish it off with the PHP file which must check the name entered against the boards already existing in the MySQL database, leading to something like this:</p>
<pre class="brush: php">

&lt;?php

//Connect to MySQL...

$name = mysql_real_escape_string($_GET[&#039;n&#039;]);

//We&#039;re using the GET method here, but there are applications of POST

$getBoards = mysql_query(&quot;SELECT `id` FROM `boards` WHERE `name`=&#039;$name&#039;&quot;);

if (mysql_num_rows($getBoards) == 0) { echo &quot;OK&quot;; }

else { echo &quot;Taken&quot;; }

?&gt;
</pre>
<p>Depending on your application, the echoed strings would be changed and perhaps even a bit more complex, but these work fine for the basic application we worked through.</p>
<p>As I noted above, we used the GET method, just like what is used in normal forms, where variables are passed through the URL to be accessed by the PHP file.  However, POST can be used and should be used in certain situations.  In general, they coincide with the general rules for form method choosing.  GET should be used for shorter requests of one or two variables and POST for multiple fields and especially those sent to the database.  Remember that the URL from GET can only be so long before cut off and may not be able to hold all the information from a long request.  Therefore, POST is certainly the most viable choice because it can be used in all applications by simply altering the method in the JavaScript and PHP and adding a few lines as noted on the tutorials listed above.</p>
<p>Still wondering where AJAX can come in handy?  We&#8217;ve all seen those handy &#8220;username checks&#8221; on registration pages and the suggestions provided by Google, Yahoo!, YouTube, and other big-name websites, and these all employ AJAX.   Whether you realized it or not because of their speed, these pages actually utilize more code than a typical PHP/ASP check, but they have certain disadvantages and will always be <a href="http://www.webmaster-forums.net/html-css-and-javascript/is-ajax-worth-it">up for debate</a>.</p>
<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://phpstarter.net/2009/03/working-with-ajax/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Regular Expressions May Cause Irregularity</title>
		<link>http://phpstarter.net/2009/02/regular-expressions-may-cause-irregularity/</link>
		<comments>http://phpstarter.net/2009/02/regular-expressions-may-cause-irregularity/#comments</comments>
		<pubDate>Tue, 24 Feb 2009 06:00:14 +0000</pubDate>
		<dc:creator>Kurtis</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Intermediate]]></category>

		<guid isPermaLink="false">http://phpstarter.net/?p=379</guid>
		<description><![CDATA[Regular expressions seem rather complex, even like a foreign language to beginning programmers.  The mixture of symbols and characters can bring you to tears if you have a limited understanding, but with the knowledge of the significance of each symbol and construct, regular expressions can bring you from irregularity to peaceful contention bliss.
No related posts.]]></description>
			<content:encoded><![CDATA[<p>Regular expressions seem rather complex, even like a foreign language to beginning programmers.  The mixture of symbols and characters can bring you to tears if you have a limited understanding, but with the knowledge of the significance of each symbol and construct, regular expressions can bring you from irregularity to peaceful contention bliss.<span id="more-379"></span>A regular expression (regex for short) is a pattern describing a string of text.  There are levels of complexity for regular expressions, from simple strings of literal characters to repetition and grouping.</p>
<p>The most basic regular expression consists of a single <strong>literal character</strong> like <em>a</em>.  It will match that character in a string, but only on the first time, unless you apply repetitive properties to it or are more specific.  To use literal symbols, <em>[</em>, <em>\</em>,<em> ^</em>,<em> $</em>,<em> .</em>,<em> |</em>,<em> ?</em>,<em> *</em>,<em> (</em>,<em> )</em> you must escape them with a backslash (<em>\</em>).   For example, <strong>1\+1=2</strong> is the correct regular expression to match <strong>1+1=2</strong>.</p>
<p>Next are character classes or character sets.  Instead of specifying each letter of the alphabet or digit, it is possible instead to use sets of alphanumeric characters to match a string.  For instance <strong>six[a-z]+</strong> matches <strong>sixteen</strong>, <strong>sixty</strong>, and <strong>sixlet</strong>, among others.  Charcter sets should be placed within brackets and can include any range of lower-case characters, upper-case characters, digits from 0 to 9, hyphens, and underscores.</p>
<p>Regular expressions do have some shorthand character classes to ease your mind:  <strong>\d</strong> for all digits, <strong>\w</strong> for all alphanumeric characters or the underscore (&#8220;_&#8221;), <strong>\s</strong> for whitespace characters including tabs and line breaks, <strong>\t</strong> for tabs individually, <strong>\r</strong> for carriage returns, and <strong>\n</strong> for line feeds.  Remember that Windows text files use <strong>\r\n</strong> to terminate lines while UNIX text files simply use <strong>\n</strong>.</p>
<p>The dot or period (&#8220;.&#8221;) stands for any character except line break characters, meaning the same as <strong>[^\n]</strong>, where the <strong>^</strong> character means the negation of the following character (&#8220;anything but &#8230;&#8221;).  Often times a character class or negated character class is faster and more precise, so be cautious when using the dot.</p>
<p>Alternation allows for some choice in your regular expressions.  Using the vertical pipe bar (&#8220;|&#8221;), you can make <strong>sixteen</strong>, <strong>sixty</strong>, and <strong>sixlet</strong> all match the following regular expression: <strong>six(teen|ty|let)</strong>.  The question mark (&#8220;?&#8221;) allows for optional characters, for instance when dealing with British-English and American-English spellings (<strong>colou?r </strong>matches <strong>color</strong> and <strong>colour</strong>).</p>
<p>Lastly is the issue of repetition.  The asterisk (&#8220;*&#8221;) tells the engine to atempt to match the class zero or more times.  The plus sign (&#8220;+&#8221;) tries to match one or more times.  An integer in curly brackets following a class can specify an exact amount of instances.</p>
<p>Now for some examples.  The description above can seem quite weighty and boring, but some applications should solidify the ideas and clarify any confusion.</p>
<p><strong>Format of an e-mail address</strong></p>
<pre class="brush: php">

&lt;?php

$goodEmail = &quot;yourname@yourdomain.com&quot;;

$badEmail = &quot;thisIsNoEmailAtAll&quot;;

$niceTry = &quot;thisIsClose@domain&quot;;

$tooManyAts = &quot;thisIsCloseToo@@domain&quot;;

if (@preg_match(&#039;/[-a-zA-Z0-9]+@{1}[-a-zA-Z0-9]+[\.]{1}[a-zA-Z]{2,4}[\.]*[-a-zA-Z0-9]*/&#039;, $email)) {

echo &quot;Good email!&quot;;

}

else {

echo &quot;The format of your e-mail address is unacceptable.&quot;;

}

?&gt;
</pre>
<p>You can run through each e-mail address above, and the one named as good will work while the others will fail.  Notice that we allow for three or four letter domains as well as those like &#8220;co.uk&#8221; where the domain would have two periods (&#8220;.&#8221;).</p>
<p>Another common use of regular expressions is in URL rewriting.  For instance, on my website, JeoReview, when a user access a URL like &#8216;http://www.jeoreview.com/board/Board-Name&#8217; a different page is served to show the board itself.  In truth, that URL does not exist.  The regular expression, as placed in the <strong>.htaccess</strong> file, looks like this:</p>
<pre class="brush: php">

Options FollowSymLinks
RewriteEngine On
RewriteRule ^board/(.+)$ loadboard.php?name=$1
</pre>
<p>The caret (&#8220;^&#8221;) marks the beginning of the string and the dollar sign (&#8220;$&#8221;) marks the end.  Because nearly any character can be used for the board name, a dot (&#8220;.&#8221;) is used to match the name itself.  In truth, the better alternative would be to match exactly what characters can be used to increase speed and accuracy.  In any case, the <strong>$1</strong> refers to the first character case, and each subsequent integer is filled with the following character cases, though in this example there is only one.</p>
<p>There are plenty of other great uses of regular expressions.  Check some out by visiting these great resources and cheat sheets:</p>
<ul>
<li>Regular-Expressions Reference (<a href="http://www.regular-expressions.info/reference.html">http://www.regular-expressions.info/reference.html</a>)</li>
<li>Regular Expression Checker (<a href="http://regjex.com/">http://regjex.com/</a>)</li>
<li>Cheat Sheet (<a href="http://www.addedbytes.com/cheat-sheets/regular-expressions-cheat-sheet/">http://www.addedbytes.com/cheat-sheets/regular-expressions-cheat-sheet/</a>)</li>
<li>Using Regular Expressions and PHP (<a href="http://www.regular-expressions.info/php.html">http://www.regular-expressions.info/php.html</a><cite>)</cite></li>
</ul>
<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://phpstarter.net/2009/02/regular-expressions-may-cause-irregularity/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Basic PHP/MySQL Actions</title>
		<link>http://phpstarter.net/2009/02/basic-phpmysql-actions/</link>
		<comments>http://phpstarter.net/2009/02/basic-phpmysql-actions/#comments</comments>
		<pubDate>Tue, 17 Feb 2009 06:00:22 +0000</pubDate>
		<dc:creator>Kurtis</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Beginner]]></category>

		<guid isPermaLink="false">http://phpstarter.net/?p=357</guid>
		<description><![CDATA[Using databases to store user identification, image information, and so much more for dynamic websites is the easiest way to ease maintenance. MySQL is the most popular choice of database, and in this article, I'll explain the most basic functions: connecting and using a MySQL database and its respective tables.
No related posts.]]></description>
			<content:encoded><![CDATA[<p>Using databases to store user identification, image information, and so much more for dynamic websites is the easiest way to ease maintenance.  MySQL is the most popular choice of database, and in this article, I&#8217;ll explain the most basic functions: connecting and using a MySQL database and its respective tables.<span id="more-357"></span>The first step in using a MySQL database is to make sure your host has the necessities set up.  Typically, your web host&#8217;s control panel will allow the creation of a database/user pair which will allow you to connect.  However, most do not allow the creation of these directly through a PHP script.  Check with your host to see how you can get your MySQL database running.</p>
<p>The next step, and probably the first in your running script is connecting to the user and database, using the following two functions:</p>
<pre class="brush: php">

&lt;?php

...

mysql_connect(host, user, password);

mysql_select_db(database [, link]);

...

?&gt;
</pre>
<p>Again, your host will work with you with respect to establishing the user, password, and database, and <em>host</em> is typically &#8220;localhost&#8221; when accessing the database on the same server as your website, but check with your host if you are having problems.  The link, which is optional, would refer to the variable holding your <strong>mysql_connect()</strong> reference.</p>
<p>Now you have a database ready to go, but without a table, there&#8217;s no place for your streams of data to go, so let&#8217;s set one up.  Most of our queries to MySQL are going to be run through the appropriately named <strong>mysql_query()</strong> function, and the text within will change with each different application.  To create a table, we run a query like this:</p>
<pre class="brush: php">

&lt;?php

//Connect to MySQL before running any queries, every time

mysql_query(&quot;CREATE TABLE table_name(

column_one ATTRIBUTES,

column_two ATTRIBUTES,

...

)&quot;);

mysql_close(link);

?&gt;
</pre>
<p>You should take the time before creating a table to decide what all needs to be stored and what would be the best manner to do so, and then use one of the column types and attributes name <a href="http://dev.mysql.com/doc/refman/5.1/en/create-table.html" target="_blank">at this resource</a>.  Some may seem confusing, but there are plenty of elaborations and tutorials available to describe the uses of each type.  The <strong>mysql_close()</strong> function simply closes our the defined connection and is typically unnecessary as the connection will be closed at the end of the script anyway.</p>
<p>With a table available for queries, we can begin adding and working with our data.  The main abilities available with the <strong>mysql_query()</strong> function our <strong>SELECT</strong>, <strong>SHOW</strong>, <strong>DESCRIBE</strong>, <strong>EXPLAIN</strong>, <strong>INSERT</strong>, <strong>UPDATE</strong>, <strong>DELETE</strong>, and <strong>DROP</strong>.  The first four return a resource (explained in detail in a minute) and the latter four return a boolean value depending on the success.</p>
<p><strong>SHOW</strong>, <strong>DESCRIBE</strong>, and <strong>EXPLAIN</strong> are honestly rarely used within the context of a functioning, user-run website.  They are moreso used on the administrator&#8217;s end and even then are not as functional as the others.  <strong>DELETE</strong> and <strong>DROP</strong> should certainly only be used on the administrator&#8217;s end and probably, again, only through MySQL itself and not through a publicly-available executable file.  Only through proper authentication should someone be able to delete a record or even an entire MySQL table.</p>
<p>Let&#8217;s start with <strong>INSERT</strong>, as that is where you must start to use the other functions.  To insert data into a table, run the function like this:</p>
<pre class="brush: php">

&lt;?php

//Connect to MySQL before running any queries, every time

mysql_query(&quot;INSERT INTO `table_name` (col1, col2, ...)

VALUES(&#039;val1&#039;, &#039;val2&#039;, ...)&quot;) or die(mysql_error());

...

?&gt;
</pre>
<p>The column names in the first set of parentheses should correspond with the correct value in the second set.  You may also opt, with each execution of <strong>mysql_query()</strong> to use the <strong>mysql_error()</strong> function to report an error should one occur, though hopefully you&#8217;ve worked your script well and an error is handled by your own error-checking.  However, some things do slip by and happen unpreventably.</p>
<p>Next is the <strong>SELECT</strong> application.  Just as it sounds, you select records from an identified table and can then use the data however you wish, by displaying it, checking it against variables, etc.  Either way, a <strong>SELECT</strong> application looks like this:</p>
<pre class="brush: php">

&lt;?php

//Connect to MySQL before running any queries, every time

$getRows = mysql_query(&quot;SELECT * FROM `table_name` WHERE `col_name`=&#039;val&#039;&quot;);

while ($result = mysql_fetch_array($getRows)) {

echo $result[&#039;col_name&#039;];

...

}

...

?&gt;
</pre>
<p>The <strong>*</strong> can be replaced with each column you wish to extract, separated by commas, and the WHERE clause is in the query is optional but should be used when looking for particular records.  You should note that <strong>SELECT</strong> returns a resource, so we have to loop through that resource using the <strong>mysql_fetch_array()</strong>, <strong>mysql_fetch_assoc()</strong>, <strong>mysql_fetch_row()</strong> or an appropriate alternative.  Then the data from the records is stored in an array where each column is referenced by its respective name.</p>
<p>To delete a record (or records) use a similar construct to the <strong>SELECT</strong> application, but no looping or variable-calling is necessary.  It simply works through the query:</p>
<pre class="brush: php">

&lt;?php

//Connect to MySQL before running any queries, every time

mysql_query(&quot;DELETE FROM `table_name` WHERE `col_name`=&#039;val&#039;&quot;);

...

?&gt;
</pre>
<p>To delete an entire table, use the <strong>DROP</strong> application, though realize that every record will be obliterated:</p>
<pre class="brush: php">

&lt;?php

//Connect to MySQL before running any queries, every time

mysql_query(&quot;DROP TABLE `table_name`&quot;);

...

?&gt;
</pre>
<p>Lastly, editing records is fairly easy though it can be more cumbersome.  Using the <strong>UPDATE</strong> application like so can allow for the altering of preexisting rows.</p>
<pre class="brush: php">

&lt;?php

//Connect to MySQL before running any queries, every time

mysql_query(&quot;UPDATE `table_name`

SET `col_name` = &#039;val&#039;, `col2_name` = &#039;val2&#039;, ...

WHERE `col_name` = &#039;val&#039;&quot;);

...
?&gt;
</pre>
<p>You can change as many columns as you need, separating each with a comma, but be careful with spacing because the success of the update can depend highly on the spacing as I have seen in the past, though I can&#8217;t find a good reference on what the exact spacing should look like.</p>
<p>With these basic applications in tow, little cannot be accomplished with your MySQL database.  With a dynamic, user-driven website these will certainly be integrated into your website.  To keep a more watchful eye on your database and perform certain one-time actions on your tables, you may also choose to employ a GUI like <a href="http://www.phpmyadmin.net" target="_blank">PHPMyAdmin</a> or something similar which your host may already have available through the control panel.</p>
<p>Happy querying!</p>
<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://phpstarter.net/2009/02/basic-phpmysql-actions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Explosive PHP Application</title>
		<link>http://phpstarter.net/2009/02/explosive-php-application/</link>
		<comments>http://phpstarter.net/2009/02/explosive-php-application/#comments</comments>
		<pubDate>Tue, 10 Feb 2009 06:00:18 +0000</pubDate>
		<dc:creator>Kurtis</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Beginner]]></category>

		<guid isPermaLink="false">http://phpstarter.net/?p=341</guid>
		<description><![CDATA[Sometimes a complex PHP-built website can become tedious when dealing with minor changes to the template and HTML markup.  Changing, for instance, a set of links (navigation) or other integral parts of a website, can become quite the lengthy process.  In this article, I'll share a technique that I sometimes use to make something as simple as a website navigation a little more automated using an explosive function.
No related posts.]]></description>
			<content:encoded><![CDATA[<p>Sometimes a complex PHP-built website can become tedious when dealing with minor changes to the template and HTML markup.  Changing, for instance, a set of links (navigation) or other integral parts of a website, can become quite the lengthy process.  In this article, I&#8217;ll share a technique that I sometimes use to make something as simple as a website navigation a little more automated using an explosive function.<span id="more-341"></span>So maybe you&#8217;ve caught on to the references to explosions and are thinking that something&#8217;s about to get dangerous, and really, this application is quite safe and easy to use.  The function itself is properly named &#8220;explode&#8221;:</p>
<p><span>
<pre class="brush: php">

&lt;?php

...

$arr = explode(delimiter, string [,limit]);

...

?&gt;
</pre>
<p>Now you may be thinking: that&#8217;s all well in good but what in the world is a delimiter?  I thought the same way before I truly understood the function.  The delimiter is just the common separator between each element.  For instance, if we wanted to explode a sentence into an array with each element as a separate word from the string, the delimiter would be an empty space (&#8221; &#8220;).  The optional &#8220;limit&#8221; is a limit of the number of elements to allow to be created.  If it is left empty or set to &#8220;-1,&#8221; no limit will be set.  Otherwise, once the limit is reached, the last element will be the rest of the unexploded string.</p>
<p>Now this is all good and well unless you have an awesome application of the function, and believe me, there are many, but my favorite is the dynamic navigation idea.  I always get sick of having to add in all the extra template elements when altering a website&#8217;s navigation, and with this simple idea that can cease.</p>
<p>Here&#8217;s a basic run-down of what will happen:  first, we&#8217;ll define a string holding all the link names and source locations.  Then, we&#8217;ll explode that string to an array of all link name-source pairs which we will in turn explode individually to produce a great link.  Here&#8217;s an example:</p>
<pre class="brush: php">

&lt;?php

...

$links = &quot;Home:index.php,About Us:about.php,Contact Us:contact.php,Portfolio:portfolio.php&quot;;

$links = explode(&quot;,&quot;,$links);

echo &quot;&lt;ul&gt;&quot;;

foreach ($links as $link) {

$link = explode(&quot;:&quot;,$link);

echo &quot;&lt;li&gt;&lt;a href=&#039;&quot;.$link[1].&quot;&#039;&gt;&quot;.$link[0].&quot;&lt;/a&gt;&lt;/li&gt;&quot;;

}

echo &quot;&lt;/ul&gt;&quot;;

?&gt;
</pre>
<p>Be aware of the order in which we are exploding the elements.  If you explode incorrectly, you could end up with inaccurate pairs.  With this application, to update the navigation, instead of sifting through what could become complex HTML, you simply need to edit the &#8220;links&#8221; variable and let the PHP take care of business for you.  Nothing complex, but certainly useful.</p>
<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://phpstarter.net/2009/02/explosive-php-application/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Dynamically Converting Text to an Image</title>
		<link>http://phpstarter.net/2009/02/dynamically-converting-text-to-an-image/</link>
		<comments>http://phpstarter.net/2009/02/dynamically-converting-text-to-an-image/#comments</comments>
		<pubDate>Thu, 05 Feb 2009 06:00:28 +0000</pubDate>
		<dc:creator>Kurtis</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[GD Library]]></category>
		<category><![CDATA[Intermediate]]></category>

		<guid isPermaLink="false">http://phpstarter.net/?p=337</guid>
		<description><![CDATA[As CSS expands, so does web design as a whole, but where it still lacks (and most likely will always lack) is in the area of fonts.  Depending on the user's home computer and the actions he/she has taken on it, there are different fonts inherently available.  In this article, however, we will talk about bypassing the user's system and use the fonts you would like, via PHP and the GD library.
No related posts.]]></description>
			<content:encoded><![CDATA[<p>As CSS expands, so does web design as a whole, but where it still lacks (and most likely will always lack) is in the area of fonts.  Depending on the user&#8217;s home computer and the actions he/she has taken on it, there are different fonts inherently available.  In this article, however, we will talk about bypassing the user&#8217;s system and use the fonts you would like, via PHP and the GD library.<span id="more-337"></span></p>
<p>SEO experts would say using images to replace text is a bad idea because crawlers cannot read the text on images to analyze the content on your website, but this process will effectively use JavaScript to overcome that.  First, we&#8217;ll talk about the basic idea of creating an image with the text using a basic GD function, <a href="http://www.php.net/imageloadfont" target="_blank">imageloadfont()</a>.</p>
<pre class="brush: php">

&lt;?php

$font = imageloadfont(&quot;path/to/font.gdf&quot;);

?&gt;
</pre>
<p>You can see the file is in a strange format, but you can safely use <a href="http://www.wedwick.com/wftopf.exe" target="_blank">this program</a> to convert Windows font files to the necessary format.  Using this program within the spectrum of an image created with PHP, text can be written on the image with a certain font:</p>
<pre class="brush: php">

&lt;?php

$string = &quot;Your string goes here&quot;;

//You should define the next two based on the font you are using

$charwidth = ...;

$charheight = ...;

$imagewidth = ($charwidth*strlen($string));

$text = imagecreatetruecolor = ($imagewidth,$charheight);

$font = imageloadfont(&quot;path/to/font.gdf&quot;);

$white = imagecolorallocate($text,255,255,255);

$black = imagecolorallocate($text,0,0,0);

imagefill($text,0,0,$white);

imagestring($text,$font,0,0,$string,$black);

header(&quot;Content-type: image/png&quot;);

imagepng($text);

imagedestroy($text);

?&gt;
</pre>
<p>You can see the image is tailored to fit the text so there is no extra whitespace.  The only downside is that the &#8216;charwidth&#8217; and &#8216;charheight&#8217; variables have to be altered for different fonts.</p>
<p>With just this basic script you can turn text into an image, but how about returning to the overarching idea of dynamically turning text on a website into an image to utilize a desired font.  We&#8217;ll use some simple JavaScript for that.</p>
<p>Note: I&#8217;m using the most basic template of an HTML file to show the idea of the script.</p>
<pre class="brush: html">

&lt;html&gt;

&lt;head&gt;
&lt;title&gt;Text to Image Conversion&lt;/title&gt;

&lt;script type=&#039;text/javascript&#039;&gt;

function text2img() {

htwo = document.getElementsByTagName(&quot;h2&quot;);

for (i=0,tot=htwo.length;i&lt;tot;i++) {

text = htwo.innerHTML;

htwo.innerHTML = &quot;&lt;img src=&#039;text2img.php?s=&quot;+text+&quot;&#039; alt=&#039;&quot;+text+&quot;&#039; /&gt;&quot;;

}

&lt;/script&gt;
&lt;body onload=&quot;text2img();&quot;&gt;

&lt;h2&gt;This will become an image&lt;/h2&gt;

&lt;p&gt;This won&#039;t become an image.&lt;/p&gt;

&lt;h2&gt;This will also become an image.&lt;/h2&gt;

&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>Referencing the PHP file we created in the JavaScript of this file will turn each &#8216;h2&#8242; element into an image.  As I mentioned earlier, this will not mess with most search engines, though, because the text will remain for those where JavaScript is turned off.  Note, though, that that also means that users with JavaScript disabled will not see the fancy image either, just the plain text, so be sure to style it with CSS as well to insure the design of your website.</p>
<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://phpstarter.net/2009/02/dynamically-converting-text-to-an-image/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Editing Images &#8211; Resizing, Cropping, and more!</title>
		<link>http://phpstarter.net/2009/01/editing-images-resizing-cropping-and-more/</link>
		<comments>http://phpstarter.net/2009/01/editing-images-resizing-cropping-and-more/#comments</comments>
		<pubDate>Thu, 29 Jan 2009 06:00:09 +0000</pubDate>
		<dc:creator>Kurtis</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[GD Library]]></category>
		<category><![CDATA[Intermediate]]></category>

		<guid isPermaLink="false">http://phpstarter.net/?p=295</guid>
		<description><![CDATA[We&#8217;ve seen the graphics PHP can create for you, but how extensive is PHP&#8217;s ability to edit preexisiting images? In this article, I will explore some common functions that image editing software provides, but I&#8217;ll only use PHP.  We&#8217;ll focus mainly on cropping, resizing, flipping, and adding some effects to photos.Of course there are hundreds [...]
No related posts.]]></description>
			<content:encoded><![CDATA[<p>We&#8217;ve seen the graphics PHP can create for you, but how extensive is PHP&#8217;s ability to edit preexisiting images?  In this article, I will explore some common functions that image editing software provides, but I&#8217;ll only use PHP.  We&#8217;ll focus mainly on cropping, resizing, flipping, and adding some effects to photos.<span id="more-295"></span>Of course there are hundreds of image editing software packages available, many for free, but there is something to be said for editing images with PHP.  In fact, it just may be that working with graphics may mean utilizing internet programs to do what we typically do with programs like Photoshop.</p>
<p>First of all, let&#8217;s talk about cropping and resizing images as the two use the same function, just in a slightly different way.  The function is <a href="http://www.php.net/imagecopyresampled" target="_blank">imagecopyresampled()</a>.</p>
<pre class="brush: php">

&lt;?php

imagecopyresampled($new,$orig,$newX,$newY,$oldX,$oldY,$newWidth,$newHeight,$oldWidth,$oldHeight);

?&gt;
</pre>
<p>It&#8217;s quite a doozy, but all of the parameters are self-explanatory.  The variables &#8216;newX&#8217; and &#8216;oldX&#8217; refer to the new starting x-coordinates and old coordinate, respectively, and so on for the y-coordinates as well.</p>
<p>To crop an image, the idea is that we select a rectangular section of the original image and copy it to a new image.  Realize that in this case the &#8216;oldWidth&#8217; and &#8216;oldHeight&#8217; variables refer to the selection cropped, not the entire original image.  The process looks like this:</p>
<pre class="brush: php">

&lt;?php

//Cropping an image
header(&quot;Content-type: image/jpeg&quot;);

$top = ...; //Define upper-most pixel of selection

$bottom = ...; //Define lower-most pixel of selection

$left = ...; //Define left-most pixel of selection

$right = ...; //Define right-most pixel of selection
$image = imagecreatefromjpeg(&quot;/path/to/image.jpg&quot;); //Function matches file type

$new = imagecreatetruecolor(($right-$left),($bottom-$top)); //Becomes width,height

imagecopyresampled($new,$image,0,0,$left,$top,($right-$left),($bottom-$top),($right-$left),($bottom-$top));

imagejpeg($new,null,100);

imagedestroy($new);

imagedestroy($image);

?&gt;
</pre>
<p>Next is the very similar resizing function.  The only difference with resizing is that you don&#8217;t need to pick a certain area of the image; the entire image will be copied, simply at a different width and height.</p>
<pre class="brush: php">

&lt;?php

//Resizing an image
header(&quot;Content-type: image/jpeg&quot;);

$newWidth = ...; //Define new image&#039;s width

$newHeight = ...; //Define new image&#039;s height
$image = imagecreatefromjpeg(&quot;/path/to/image.jpg&quot;); //Function matches file type

list($width,$height) = getimagesize(&quot;/path/to/image.jpg&quot;); //Get old width and height

$new = imagecreatetruecolor($newWidth,$newHeight);

imagecopyresampled($new,$image,0,0,0,0,$newWidth,$newHeight,$width,$height);

imagejpeg($new,null,100);

imagedestroy($new);

imagedestroy($image);

?&gt;
</pre>
<p>You can see that these two ideas are very closely related, so there isn&#8217;t much difference between the two functions.  Next, let&#8217;s talk about flipping images, both horizontally and vertically.</p>
<p>There are two new functions we&#8217;ll talk about here, one that gets the color of the image at a specified pixel, and the other sets the color of the image at a specified pixel.</p>
<pre class="brush: php">

&lt;?php

$color = imagecolorat($image,$x,$y); //Get color

imagesetpixel($image,$x,$y,$color); //Set color

?&gt;
</pre>
<p>The only other thing we need to talk about is the idea of rolling through each pixel, and we&#8217;ll do this using two simultaneous &#8216;for&#8217; statements, one that runs through the image pixel-by-pixel horizontally, and another one that runs through each vertically at the same time.  This will allow us to get and set the color of each pixel in the image.</p>
<p>To flip an image horizontally, the left-most pixel becomes the right-most and vice-versa.  The top-most and bottom-most pixels switch when we flip vertically.  The process looks like this:</p>
<pre class="brush: php">

&lt;?php

$type = ...; //Define flip type (horizontal or vertical)

$image = imagecreatefromjpeg(&quot;/path/to/image.jpg&quot;); //Function matches file type
list($width,$height) = getimagesize($imagepath); //Get width and height
$new = imagecreatetruecolor($width,$height);  //Becomes new width and height
for ($x=0; $x&lt;$width; $x++) {
for ($y=0; $y&lt;$height; $y++) {
$color = imagecolorat($image,$x,$y); //Get pixel color
if ($type == &quot;horizontal&quot;) {
imagesetpixel($new,(($width-1)-$x),$y,$color);
}
else { //Type is vertical
imagesetpixel($new,$x,(($height-1)-$y),$color);
}
}
}

imagejpeg($new,null,100);

imagedestroy($new);

imagedestroy($image);

?&gt;
</pre>
<p>Last, but not least, I&#8217;ll show you how to apply several different types of effects to an image, and this doesn&#8217;t require any special calculations, just one function, <a href="http://php.net/imagefilter" target="_blank">imagefilter()</a>.  You can read about all of the different filters available, but the main ones are blurring, converting to gray scale, and changing contrast/brightness.</p>
<p>With these GD functions in tow, there is very little you can&#8217;t do with PHP.  Alright, so there&#8217;s still quite a bit that PHP can&#8217;t do with images practically, but it&#8217;s only a matter of time before programmers are able to apply even the most complex Photoshop functions to PHP.</p>
<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://phpstarter.net/2009/01/editing-images-resizing-cropping-and-more/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Understanding Decision Structures</title>
		<link>http://phpstarter.net/2009/01/understanding-decision-structures/</link>
		<comments>http://phpstarter.net/2009/01/understanding-decision-structures/#comments</comments>
		<pubDate>Thu, 22 Jan 2009 06:00:53 +0000</pubDate>
		<dc:creator>Kurtis</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Beginner]]></category>

		<guid isPermaLink="false">http://phpstarter.net/?p=61</guid>
		<description><![CDATA[Decision structures form the basis of all programming languages; just as they sound, these constructs move the program down a certain path depending on variables which are checked.  This gives a program functionality and the ability to handle different input and analyze the data within the script. Though the term sounds complex, decision structures just [...]
No related posts.]]></description>
			<content:encoded><![CDATA[<p>Decision structures form the basis of all programming languages; just as they sound, these constructs move the program down a certain path depending on variables which are checked.  This gives a program functionality and the ability to handle different input and analyze the data within the script.<br />
<span id="more-61"></span>Though the term sounds complex, decision structures just help the script make a decision.  If something is true/false, do this.  If not, do this.  All decision structures come down to a true or false situation, and usually a comparison is made that results in one of the two choices.  There are several comparison operators, and it takes a basic understanding of logic to fully utilize decision structures.</p>
<p>The comparision operators are:</p>
<table border="0">
<tbody>
<tr>
<td>==</td>
<td>($a == $b)</td>
<td>Returns true if $a equals $b</td>
</tr>
<tr>
<td>===</td>
<td>($a === $b)</td>
<td>Returns true if $a equals $b and is of the same type (int, string, etc)</td>
</tr>
<tr>
<td>!=</td>
<td>($a != $b)</td>
<td>Returns true if $a does not equal $b</td>
</tr>
<tr>
<td>!==</td>
<td>($a !== $b)</td>
<td>Returns true if $a does not equal $b or is not of the same type</td>
</tr>
<tr>
<td>&lt;</td>
<td>($a &lt; $b)</td>
<td>Returns true if $a is less than $b</td>
</tr>
<tr>
<td>&gt;</td>
<td>($a &gt; $b)</td>
<td>Returns true if $a is greater than $b</td>
</tr>
<tr>
<td>&lt;=</td>
<td>($a &lt;= $b)</td>
<td>Returns true if $a is less than or equal to $b</td>
</tr>
<tr>
<td>&gt;=</td>
<td>($a &gt;= $b)</td>
<td>Returns true if $a is greater than or equal to $b</td>
</tr>
</tbody>
</table>
<p>Logical operators are a bit different but are also used in decision structures.  There are five different logical operators, but for the sake of this article, in truth three are the most important.</p>
<table border="0">
<tbody>
<tr>
<td>&amp;&amp; , and</td>
<td>($a &amp;&amp; $b)</td>
<td>Returns true if A and B are both be true</td>
</tr>
<tr>
<td>|| , or</td>
<td>($a || $b)</td>
<td>Returns true if A or B (or both) is (are) true</td>
</tr>
<tr>
<td>xor</td>
<td>($a xor $b)</td>
<td>Returns true if A or B is true and the other is false</td>
</tr>
</tbody>
</table>
<p>For more information on the reason behind two variations of &#8220;and&#8221; and &#8220;or&#8221; operators, check out the manual on <a href="http://us.php.net/manual/en/language.operators.precedence.php">operator precedence</a>.</p>
<p>Now let&#8217;s get on to the decision-making part of programming with PHP.  There are two main ways to do this, and we will start with the if-else statements.</p>
<p>Basically, this comprises the main idea of decision making.  It handles like this:  &#8220;If THIS is true, do something.  Otherwise (else), do something else.&#8221;  Using the operators defined above, you can make a basic script:</p>
<pre class="brush: php">
if ($a == $b) {
echo &quot;The variables are equal.&quot;;
}
else {
echo &quot;The variables are unequal.&quot;;
}
?&gt;
</pre>
<p>This would be great if there were always just two choices, but sometimes there are several choices between these two.  Think about a school grading scale.  The following would assign a grade based on a ten-point scale.  Since there are more than two possibilities, we will add some elseif statements inbetween.</p>
<pre class="brush: php">

&lt;?php

$grade = 93;

if ($grade &gt;= 90) {
$letter-grade = &quot;A&quot;;
}
elseif ($grade &gt; 80) {
$letter-grade = &quot;B&quot;;
}
elseif ($grade &gt; 70) {
$letter-grade = &quot;C&quot;;
}
elseif ($grade &gt; 60) {
$letter-grade = &quot;D&quot;;
}
else {
$letter-grade = &quot;F&quot;;
}
?&gt;
</pre>
<p>Notice that we work downward to make sure the grade only fits in one category.  It&#8217;s important to think about your script logically before setting out to write it.  Now, the type of structure described above will handle any situation, but in truth it isn&#8217;t always the best choice.  Suppose you wanted to create a script that would change a certain heading based on the date, if it is a holiday or not.  While an if-else/elseif structure would work, there is an alternative, the &#8220;switch&#8221; construct which focuses on just one variable rather than comparison between two or more.</p>
<pre class="brush: php">

&lt;?php
$date = ...; //Obtain date in the form &quot;mm/dd&quot;

switch($date) {
case &quot;01/01&quot;:
echo &quot;Happy New Year&quot;;
break;
case &quot;02/14&quot;:
echo &quot;Happy Valentine&#039;s Day&quot;;
break;
case &quot;05/10&quot;:
echo &quot;Happy Mother&#039;s Day&quot;;
break;
default:
echo &quot;Happy Day!&quot;;
break;
}
?&gt;
</pre>
<p>Obviously, hundreds of other holidays can be added in as a different &#8220;case,&#8221; but the idea is captured with this short script.  The &#8220;default&#8221; case acts as an &#8220;else&#8221; in this structure, catching anything that did not match any case.</p>
<p>With these two ideas, it is easy to build fairly impressive scripts with PHP, and you will even find that the idea carries over to most different programming languages with tiny changes in syntax  and operators.  Combined with more complex variables handled by user input and organized sessions, you will see that decision structures are truly the core of a programming language.</p>
<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://phpstarter.net/2009/01/understanding-decision-structures/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Configuring PHP on Windows Systems</title>
		<link>http://phpstarter.net/2009/01/configuring-php-on-windows-systems/</link>
		<comments>http://phpstarter.net/2009/01/configuring-php-on-windows-systems/#comments</comments>
		<pubDate>Thu, 15 Jan 2009 06:00:27 +0000</pubDate>
		<dc:creator>Kurtis</dc:creator>
				<category><![CDATA[Apache]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Intermediate]]></category>

		<guid isPermaLink="false">http://phpstarter.net/?p=233</guid>
		<description><![CDATA[Though not necessary, running PHP on your home machine can make development much quicker and easier. Without affecting the actual website, you can edit files and execute them locally, uploading all to the server when finished. This article will cover the installation of Apache and configuration of PHP on your local Windows machine.There are several [...]
No related posts.]]></description>
			<content:encoded><![CDATA[<p>Though not necessary, running PHP on your home machine can make development much quicker and easier.  Without affecting the actual website, you can edit files and execute them locally, uploading all to the server when finished.  This article will cover the installation of Apache and configuration of PHP on your local Windows machine.<span id="more-233"></span>There are several methods available for installing Apache, PHP, and valuable modules on your computer, ranging from all-in-one packages, easy PHP installers, and of course, manual installation.</p>
<p>First of all, let&#8217;s detail the major all-in-one installers.  The two most popular are <a href="http://www.apachefriends.org/en/xampp.html" target="_blank">XAMPP</a> and <a href="http://www.wampserver.com/en/" target="_blank">WAMP</a> (notice the AMP they have in common).  XAMPP is an acronym, each letter standing for a feature of the package:</p>
<ul>
<li><strong>X</strong>: means the package is cross-platform (Linux, Windows, etc)</li>
<li><strong>A</strong>: Apache HTTP Server</li>
<li><strong>M</strong>: MySQL</li>
<li><strong>P</strong>: PHP</li>
<li><strong>P</strong>: Perl</li>
</ul>
<p>The XAMPP package, which can be easily downloaded and installed on Windows machines, includes the following features as well: mod_php, mod_perl, mod_ssl, OpenSSL, phpMyAdmin, Webalizer, Mercury Mail Transport System for Win32 and NetWare Systems v3.32, Ming, JpGraph, FileZilla FTP Server, mcrypt, eAccelerator, SQLite, and WEB-DAV + mod_auth_mysql.  It may seem like a lot because, well, it is!  Take my word for it:  if you&#8217;re looking for a quick, simply install with all of the necessities, XAMPP is great!</p>
<p>WAMP is also an acronym, where the <strong>AMP</strong> stands for Apache, MySQL, and PHP, and the <strong>W</strong> here specifies that it only works on <strong>W</strong>indows machines.  WAMP also includes phpMyAdmin but is much more stripped in its features.  I suggest WAMP if you want a lighter install with only the bare-bones features.</p>
<p>The next option is to install Apache and PHP on your own, though this solution is still rather easy.  By downloading the <a href="http://httpd.apache.org/download.cgi">Apache installer</a> (choose the last Win32 option) and running through the steps (detailed later), you can couple it with an easy PHP installer, either one available directly from the PHP website, located <a href="http://us.php.net/manual/en/install.windows.installer.msi.php">here</a>, or one from a third-party source, like the <a href="http://phplens.com/phpeverywhere/easywindows" target="_blank">EasyWindows Installer</a>.  Following the steps they suggest will allow another quick installation option.  This is certainly the lightest quick installation available.</p>
<p>The last option is certainly the toughest, but I, as well as the PHP developers themselves, recommend it.  Installing PHP yourself allows you to take a close look at the files that configure it and work with them as well.  This may seem intimidating to some, so that&#8217;s why there are so many other options available.  You can follow the manual instructions at <a href="http://us.php.net/manual/en/install.windows.manual.php" target="_blank">php.net</a> or follow my instructions for installing Apache and PHP below.  I&#8217;ve sifted through PHP&#8217;s instructions to create the easiest-to-understand steps I could.</p>
<p>Let&#8217;s begin by configuring PHP.</p>
<ol>
<li>Download the <a href="http://www.php.net/downloads.php" target="_blank">PHP package</a>.  I suggest the latest Windows binary (<strong>5.2.8 </strong>as of this article).</li>
<li>Extract the entire package to a folder on your machine (e.g. C:/PHP).  Note that the latest releases of PHP do not create their own folder, so you must extract them to the folder you wish for PHP to reside in.</li>
<li>Make <em>php5ts.dll</em> available to the web server and CGI and CLI binaries through one of the three following options:
<ul>
<li>Copy the file to the windows system directory (<em>C:/WINDOWS</em>)</li>
<li>Copy the file to the web server&#8217;s directory</li>
<li>Add the path to PHP to the PATH environment variable by following these <a href="http://us.php.net/manual/en/faq.installation.php#faq.installation.addtopath">easy steps</a>.</li>
</ul>
</li>
<li>Set up a valid <em>php.ini</em> file.  There are two available in your PHP folder: <em>php.ini-recommended</em> and <em>php.ini-dist</em>.  The &#8220;recommended&#8221; file makes your installation more secure, but if you are installing on a home machine, the &#8220;dist&#8221; file is fine.  Simply save the file as <em>php.ini</em> in your PHP folder.</li>
<li>Now, edit the <em>php.ini </em>file you just saved by finding the <strong>doc_root</strong> variable and setting it equal to the following: <em>C:\Apache\htdocs</em></li>
<li>Find the<strong> extension_dir</strong> variable and set it equal to <em>C:\PHP\ext</em></li>
<li>Be sure to use <strong>backslashes</strong> in the <em>php.ini</em> file.</li>
<li>Save the file and close it.</li>
</ol>
<p>Now you can begin installing Apache and finishing configuration.</p>
<ol>
<li>Download the latest release of Apache <a href="http://httpd.apache.org/download.cgi" target="_blank">here</a> (choose the last Win32 option).  Use the installer to set up Apache, and install it in <em>C:/Apache</em>.</li>
<li>To set up PHP to work as an Apache module, open the <em>httpd.conf</em> file (located in <em>C:/Apache/conf/</em>) and add the following lines to the bottom of the file:
<ul>
<li>LoadModule php5_module &#8220;c:/php/php5apache2_2.dll&#8221;</li>
<li>AddType application/x-httpd-php .php</li>
<li>PHPIniDir &#8220;C:/PHP&#8221;</li>
</ul>
</li>
<li>Be sure to use <strong>forward slashes</strong> in the <em>httpd.conf</em> file.</li>
<li>Save the file and close it.</li>
<li>Restart Apache by double-clicking the icon in your system tray.  Click the &#8220;Restart&#8221; button, and if Apache restarts, everything is ready to work!  If not, try working back through the steps and make sure your settings are correct.</li>
</ol>
<p>With Apache and PHP installed, visiting <em>http://localhost/</em> will load the files from your <em>C:/Apache/htdocs</em> folder, usually by default with an HTML file saying <strong>It works!</strong></p>
<p>Whatever method you employ, setting up Apache and PHP is rather easy and simple to do.  Choose the manner best suited for you, and follow the proper steps.  In no time, you&#8217;ll have your own Apache server with PHP set up on your computer.</p>
<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://phpstarter.net/2009/01/configuring-php-on-windows-systems/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Handling File Uploads</title>
		<link>http://phpstarter.net/2009/01/handling-file-uploads/</link>
		<comments>http://phpstarter.net/2009/01/handling-file-uploads/#comments</comments>
		<pubDate>Thu, 08 Jan 2009 06:00:43 +0000</pubDate>
		<dc:creator>Kurtis</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Beginner]]></category>

		<guid isPermaLink="false">http://phpstarter.net/?p=221</guid>
		<description><![CDATA[There are very few websites in existence that do not allow, for whatever function, uploading files via the browser. Whether for forum avatars, a gallery script, or even a school project, websites across the World Wide Web employ a myriad of upload scripts, including the ever-popular MySpace, Facebook, and YouTube. With this script and step-by-step [...]
No related posts.]]></description>
			<content:encoded><![CDATA[<p>There are very few websites in existence that do not allow, for whatever function, uploading files via the browser.  Whether for forum avatars, a gallery script, or even a school project, websites across the World Wide Web employ a myriad of upload scripts, including the ever-popular MySpace, Facebook, and YouTube.  With this script and step-by-step explanation, your website can now allow users to add their own files.</p>
<p><span id="more-221"></span></p>
<p>An upload script can seem like a daunting task, a process of several steps and error-handling, and in truth, there is a lot to take into account, but the uploading step itself is very simple.  Before we even begin with the form, I suggest (for those not too afraid) checking some settings in your <em>php.ini</em> file, typically located in your PHP root folder.  This step isn&#8217;t necessary, so feel free to move on now.  Find the following lines and check the settings as explained below:</p>
<ul>
<li><strong>file_uploads</strong>: Be sure this is set to <em>On</em> or your uploads will fail every time</li>
<li><strong>upload_max_filesize</strong>: Must be at least the maximum file size you wish to allow for uploads</li>
<li><strong>post_max_size</strong>: Represents the total amount of data allowed to be passed from a form, including file uploads, so I recommend setting this higher than the <strong>upload_max_filesize</strong></li>
</ul>
<p>For most typical PHP setups, the settings should work as they are, but for more advanced users I always recommend learning exactly what that <em>php.ini </em>file does with respect to certain PHP applications.  With those settings taken care of, let&#8217;s move on to setting up the basic HTML form.  For the purpose of this example, I&#8217;ll use the most basic markup possible.</p>
<pre class="brush: html">
&lt;html&gt;
&lt;body&gt;
&lt;form method=&quot;post&quot; action=&quot;upload.php&quot; enctype=&quot;multipart/form-data&quot;&gt;
&lt;input type=&quot;hidden&quot; name=&quot;MAX_FILE_SIZE&quot; value=&quot;500000&quot; /&gt;
&lt;input type=&quot;file&quot; name=&quot;file&quot; /&gt;
&lt;input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;Submit&quot; /&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>Three things to keep in mind are the following specifications:</p>
<ul>
<li><strong>enctype=&#8221;multipart/form-data&#8221;</strong>: This attribute must be set as shown or the form will not process properly.  This is different from most basic PHP forms.</li>
<li><strong>&lt;input type=&#8221;hidden&#8221;&#8230;</strong>: Specifies the maximum file size, in bytes, allowed to be uploaded.  You can omit this line if you specify a maximum in your <em>php.ini</em> file (as described earlier).</li>
<li><strong>&lt;input type=&#8221;file&#8221;&#8230;</strong>: Notice we are using the &#8220;file&#8221; type here, again, mandatory for file uploads.  This line will provide the input field with the &#8220;Browse&#8221; button.</li>
</ul>
<p>Certainly, the HTML form is the easiest part, so let&#8217;s move on to actually handling the file and moving it to its final destination through the PHP processing file.  Above we designated that as &#8220;upload.php.&#8221;</p>
<p>First of all, we&#8217;ll set two variables, arrays actually, which will be very handy a little later on in the script: the possible PHP error messages and the allowed file extensions.</p>
<pre class="brush: php">

&lt;?php

$errors = array(1 =&gt; &quot;The file exceeds the maximum file size specification.&quot;,
2 =&gt; &quot;The file exceeds the maximum file size specification.&quot;,
3 =&gt; &quot;The file was not fully uploaded.&quot;,
4 =&gt; &quot;The file was not properly uploaded.&quot;,
6 =&gt; &quot;An error occurred during uploading.&quot;,
7 =&gt; &quot;An error occurred during uploading.&quot;,
8 =&gt; &quot;An error occurred during uploading.&quot;);

$allowedExtensions = array(&quot;jpeg&quot;,&quot;jpg&quot;,&quot;gif&quot;,&quot;png&quot;);

?&gt;
</pre>
<p>I suggest leaving the error messages as they are (despite the odd numbering), but feel free to edit the allowed extensions to suit your needs.  As we move through the script, the error messages will be further explained.</p>
<p>The next step in my upload script is to create two functions with very specific, well, functions.  The first will be used when the upload fails:</p>
<pre class="brush: php">

function uploadFail($error) {
header(&quot;refresh: 3; url=index.php&quot;);
echo &quot;&lt;p&gt;&quot;.$error.&quot;  You are now being returned to the upload form.&lt;/p&gt;&quot;;
}
</pre>
<p>This will return the user to the upload form just after displaying the error message which is passed to the function.  The next function will obtain the file&#8217;s extension:</p>
<pre class="brush: php">

function getExtension($file) {
$file = explode(&quot;.&quot;,$file);
return strtolower($file[count($file)-1]);
}
</pre>
<p>Certainly there are several methods for obtaining the file&#8217;s extension, but this is the shortest one I could put together.  Our last stop before starting the handling is one more array which is completely optional.  I use it to ease the process of getting the file&#8217;s attributes:</p>
<pre class="brush: php">

$file = array(&quot;tmp&quot; =&gt; $_FILES[&#039;file&#039;][&#039;tmp_name&#039;],
&quot;name&quot; =&gt; $_FILES[&#039;file&#039;][&#039;name&#039;],
&quot;type&quot; =&gt; $_FILES[&#039;file&#039;][&#039;type&#039;],
&quot;size&quot; =&gt; $_FILES[&#039;file&#039;][&#039;size&#039;],
&quot;error&quot; =&gt; $_FILES[&#039;file&#039;][&#039;error&#039;]);
$file[&#039;ext&#039;] = getExtension($file[&#039;name&#039;]);
</pre>
<p>This will remove the need for such long lines each time an attribute is needed, though this step is not necessary.  Note that we used our extension-finding function to do just that, and also be aware that the file type (MIME type) is available if you wish to check the type that way.</p>
<p>Finally, we&#8217;re here!  We are ready to begin handling the file and checking for errors.  First we&#8217;ll make sure the page is accessed through the form by making sure the &#8216;submit&#8217; button was pressed:</p>
<pre class="brush: php">

if (!isset($_POST[&#039;submit&#039;])) { uploadFail(&quot;You may only access this page through the form.&quot;); }
</pre>
<p>Next, we&#8217;ll check for those PHP errors we specified earlier.  If the variable is set to &#8217;0,&#8217; that means there is no error.  Otherwise, let&#8217;s reference the array we set earlier and return to the form.  Note that the inherent PHP error returns an integer based on the error encountered.  Our descriptions match pretty well what the problem is, but you can read more extensive descriptions <a href="http://us.php.net/manual/en/features.file-upload.errors.php" target="_blank">in the manual</a>.</p>
<pre class="brush: php">

else {
if ($file[&#039;error&#039;] != 0) { uploadFail($errors[$file[&#039;error&#039;]]); }
</pre>
<p>The last step in checking the file is to reference it against the extensions we listed earlier, so let&#8217;s do that now:</p>
<pre class="brush: php">

else {
if (!in_array($file[&#039;ext&#039;],$allowedExtensions)) { uploadFail(&quot;Uploaded file (&quot;.$file[&#039;name&#039;].&quot;) is of a disallowed type.&quot;); }
</pre>
<p>If the file makes it this far, that means everything checks out and we just have two steps left: setting the final file name and moving it to the &#8216;files&#8217; folder.</p>
<pre class="brush: php">

else { //Now the file is fine; let&#039;s finish the upload
//Make sure file name is not already in use
while (file_exists(&quot;files/&quot;.$file[&#039;name&#039;])) {
$file[&#039;name&#039;] = time().&quot;-&quot;.$file[&#039;name&#039;];
}
//Finally move the file to the proper place
if(move_uploaded_file($file[&#039;tmp&#039;],&quot;files/&quot;.$file[&#039;name&#039;])) {
header(&quot;Location: success.php&quot;);
}
</pre>
<p>Notice we use the <a href="http://php.net/fileexists" target="_blank">file_exists()</a> function to make sure a file of the same name isn&#8217;t already located in the folder, and if so, we rename it something sure to be more unique.  Then, we use the <a href="http://www.php.net/moveuploadedfile" target="_blank">move_uploaded_file()</a> function to move the temporary file to its final resting place, and we take the user to the &#8216;success&#8217; page where they are greeted with much love and praise for finishing the entire process.  You can create a custom file however you wish and reference it in your &#8216;upload&#8217; file.</p>
<p>All together, the &#8216;upload&#8217; file can seem complicated, but it isn&#8217;t too tough.  By establishing easy-to-use functions and variables early in the process, we were able to simplify the task and break it down into manageable parts.  The finished script looks like this:</p>
<pre class="brush: php">

&lt;?php
//The following array will hold all PHP error messages
$errors = array(1 =&gt; &quot;The file exceeds the maximum file size specification.&quot;,
2 =&gt; &quot;The file exceeds the maximum file size specification.&quot;,
3 =&gt; &quot;The file was not fully uploaded.&quot;,
4 =&gt; &quot;The file was not properly uploaded.&quot;,
6 =&gt; &quot;An error occurred during uploading.&quot;,
7 =&gt; &quot;An error occurred during uploading.&quot;,
8 =&gt; &quot;An error occurred during uploading.&quot;);
$allowedExtensions = array(&quot;jpeg&quot;,&quot;jpg&quot;,&quot;png&quot;,&quot;gif&quot;,&quot;txt&quot;);
//This function will return the user to the form, displaying the proper error message
function uploadFail($error) {
header(&quot;refresh: 3; url=index.php&quot;);
echo &quot;&lt;p&gt;&quot;.$error.&quot;  You are now being returned to the upload form.&lt;/p&gt;&quot;;
}
//This function will get the file&#039;s extension
function getExtension($file) {
$file = explode(&quot;.&quot;,$file);
return strtolower($file[count($file)-1]);
}
//The following array will hold all of our file information
$file = array(&quot;tmp&quot; =&gt; $_FILES[&#039;file&#039;][&#039;tmp_name&#039;],
&quot;name&quot; =&gt; $_FILES[&#039;file&#039;][&#039;name&#039;],
&quot;type&quot; =&gt; $_FILES[&#039;file&#039;][&#039;type&#039;],
&quot;size&quot; =&gt; $_FILES[&#039;file&#039;][&#039;size&#039;],
&quot;error&quot; =&gt; $_FILES[&#039;file&#039;][&#039;error&#039;]);
$file[&#039;ext&#039;] = getExtension($file[&#039;name&#039;]);
//Processing begins here
//Make sure the page is accessed through the form
if (!isset($_POST[&#039;submit&#039;])) { uploadFail(&quot;You may only access this page through the form.&quot;); }
else {
if ($file[&#039;error&#039;] != 0) { uploadFail($errors[$file[&#039;error&#039;]]); } //Check for PHP errors
else {
//Check file extension
if (!in_array($file[&#039;ext&#039;],$allowedExtensions)) { uploadFail(&quot;Uploaded file (&quot;.$file[&#039;name&#039;].&quot;) is of a disallowed type.&quot;); }
else { //Now the file is fine; let&#039;s finish the upload
//Make sure file name is not already in use
while (file_exists(&quot;files/&quot;.$file[&#039;name&#039;])) {
$file[&#039;name&#039;] = time().&quot;-&quot;.$file[&#039;name&#039;];
}
//Finally move the file to the proper place
if(move_uploaded_file($file[&#039;tmp&#039;],&quot;files/&quot;.$file[&#039;name&#039;])) {
header(&quot;Location: success.php&quot;);
}
else { uploadFail($errors[6]); }
}
}
}
?&gt;
</pre>
<p>Now you&#8217;re ready to make your website even more dynamic!  By allowing file uploads, your visitors have the chance to add their own unique contribution to a website they&#8217;ll enjoy even more!</p>
<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://phpstarter.net/2009/01/handling-file-uploads/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Very Basic Pie Chart</title>
		<link>http://phpstarter.net/2008/12/very-basic-pie-chart/</link>
		<comments>http://phpstarter.net/2008/12/very-basic-pie-chart/#comments</comments>
		<pubDate>Tue, 30 Dec 2008 06:00:06 +0000</pubDate>
		<dc:creator>Kurtis</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[GD Library]]></category>
		<category><![CDATA[Intermediate]]></category>

		<guid isPermaLink="false">http://phpstarter.net/?p=201</guid>
		<description><![CDATA[Pie chart, circle graph, call it what you want, but these charts are loved by descriptive statisticians across the world for their instant appeal to the viewer&#8217;s eyes and inferential mind.  Though descriptive statistics can be deceiving, pie charts, created in this article with the GD library, easily describe the data in a very literal [...]
No related posts.]]></description>
			<content:encoded><![CDATA[<p>Pie chart, circle graph, call it what you want, but these charts are loved by descriptive statisticians across the world for their instant appeal to the viewer&#8217;s eyes and inferential mind.  Though descriptive statistics can be deceiving, pie charts, created in this article with the GD library, easily describe the data in a very literal way.  In this post, we&#8217;ll go over the basic GD functions available for putting together a simple pie chart.</p>
<p><span id="more-201"></span>So far in the GD library series, I&#8217;ve detailed several key functions that one should certainly know before diving in to an extensive project.  To review some of these functions as involved with an application, check out the <a href="http://phpstarter.net/tag/gd-library/">series thus far</a>.</p>
<p>Now that you know about declaring colors, fonts, and setting up the basics of the image, let&#8217;s get right to revealing the newest (and only new) function in this article, <a href="http://www.php.net/imagefilledarc" target="_blank">imagefilledarc()</a>, declared like this:</p>
<pre class="brush: php">

&lt;?php

...

imagefilledarc($img,$cX,$cY,$width,$height,$startDeg,$endDeg,$col,$style);

...

?&gt;
</pre>
<p>Now, let&#8217;s break this down, as this is quite a hefty function:</p>
<ul>
<li>$img = The image you are working with, established with something like <a href="http://www.php.net/imagecreate" target="_blank">imagecreate()</a></li>
<li>$cX, $cY = The coordinates of the pixel that will be the center of the arc, and in this case, the entire pie chart</li>
<li>$width, $height = Quite obviously, the width and height of the arc, meaning the pixel span horizontally and then vertically</li>
<li>$startDeg, $endDeg = The starting and ending point on the circle for the arc, in degrees</li>
<li>$col = The color to fill the arc with, as established earlier with<a href="http://www.php.net/imagecolorallocate" target="_blank"> imagecolorallocate()</a></li>
<li>$style = One of several styles available, including <strong>IMG_ARC_PIE</strong>, <strong>IMG_ARC_CHORD</strong>, <strong>IMG_ARC_NOFILL</strong>, <strong>IMG_ARC_EDGED</strong>, and in this article we&#8217;ll be focusing on <strong>IMG_ARC_PIE</strong></li>
</ul>
<p>With this basic understanding of the function, let&#8217;s put together a basic pie chart with everything we know.  We&#8217;ll begin, as we typically do, by establishing the image and creating a blank, white canvas to work with.</p>
<pre class="brush: php">

&lt;?php

$pie = imagecreate(500,500);

$white = imagecolorallocate($pie,255,255,255);

imagefill($pie,0,0,$white);

...

?&gt;
</pre>
<p>Obviously, if you were putting together an accurate pie chart, you would have actual data and statistics to work with, but in this case, we&#8217;re just running through the basics, so we&#8217;ll use rather even, easy percentages.  Next, we&#8217;ll create an array holding the five percentages that will define the five &#8220;slices&#8221; of the pie, and we&#8217;ll also load a font to use later with labeling.  You remember <a href="http://www.php.net/imageloadfont">how to load a font</a>, right?</p>
<pre class="brush: php">

&lt;?php

...

$font = imageloadfont(&#039;arial.gdf&#039;);

$black = imagecolorallocate($pie,0,0,0);  //This is for the labeling also

$percentages = array(&quot;40&quot;,&quot;30&quot;,&quot;15&quot;,&quot;10&quot;,&quot;5&quot;);

...

?&gt;
</pre>
<p>Next, we&#8217;ll put together that awesome pie (I understand if you&#8217;re starting to get hungry), and I like to use a simple <a href="http://www.php.net/for" target="_blank">for()</a> loop.  Either you can manually set the loop values if you know how many values you will have at all times, or you can make it a bit more dynamic, letting PHP do the calculations.  Let&#8217;s do this slowly, simply setting up the loop and the calculations.</p>
<pre class="brush: php">

&lt;?php

...

$x = 0;

for ($z=0, $c=count($percentages); $z&lt;$c; $z++) {

$degrees = (($percentages[$z]/100)*360);

$color = imagecolorallocate($pie,mt_rand(20,255),mt_rand(20,255),mt_rand(20,255));

...

?&gt;
</pre>
<p>With just this, we are ready to create the &#8220;slices&#8221; of the pie, but I want to be sure a couple of things about the loop are made clear.  First of all, if you are going to use the <a href="http://www.php.net/count" target="_blank">count()</a> function to set the limit, establish it with the incrementing variable; otherwise, PHP will have to calculate it each time the loop is run through which demands more time and resources.  Also, make sure you understand the reason for the variable before the loop. With this, we establish &#8216;x&#8217; as the starting degree and set it to &#8217;0,&#8217; as that is where we want to start.  When calculating the degrees for each element, you can actually just multiply the percentage by 3.6, but I wanted to be sure it was obvious what I was doing there.  Lastly, we dynamically set a color, as we want each &#8220;slice&#8221; to be different.  In reality, we could end up with two slices of the same color, but the chances are slim.  Feel free to take the necessary precautions to prevent this, if you wish.  Now, let&#8217;s get to the most important part, making, not baking, a pie:</p>
<pre class="brush: php">

&lt;?php

...

$x = 0;

for ($z=0, $c=count($percentages); $z&lt;$c; $z++) {

$degrees = (($percentages[$z]/100)*360);

$color = imagecolorallocate($pie,mt_rand(20,255),mt_rand(20,255),mt_rand(20,255));

imagefilledarc($pie,100,100,125,125,$x,($x+$degrees),$color,IMG_ARC_PIE);

imagefilledrectangle($pie,250,(50+(25*($z+1))),275,(75+(25*($z+1))),$color);

imagestring($pie,$font,276,(55+(25*($z+1))),$vals[$z].&quot;%&quot;,$black);

$x+=$degrees;

}

header(&quot;Content-type: image/png&quot;);

imagepng($pie);

imagedestroy($pie);

?&gt;
</pre>
<p>Finally, we have our pie chart.  We simply use the calculations we already found, along with whatever center, width, and height you wish to use to draw the pie.  Then, we simply label the pie with the corresponding percentages.  With a more complex script, you could easily label the pie with the actual descriptors for each &#8220;slice.&#8221;  Finally, we add on the total degrees to reset the starting value for the next section.  Then, output the image and your pie is ready to eat, or rather, look at.</p>
<p>I want to stress with this script that I titled it quite appropriately; this is a <strong>basic </strong>pie chart.  There are definitely ways to make it look more appealing, certainly, but with this basic setup, you are on your way to becoming a professional statistician, except for all that fancy math behind it.</p>
<p>Check out a <a href="http://www.phpstarter.net/samples/201/piechart_maker.php">sample pie chart</a>; enter a list of percentages (that must add up to 100; there is nothing in place to check for this) and watch your pie come to life.</p>
<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://phpstarter.net/2008/12/very-basic-pie-chart/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

