<?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>Chris Clarke&#039;s Blog &#187; Tutorials</title>
	<atom:link href="http://sorrowfulunfounded.com/category/tutorials/feed/" rel="self" type="application/rss+xml" />
	<link>http://sorrowfulunfounded.com</link>
	<description>Official Weblog of Christopher Clarke</description>
	<lastBuildDate>Thu, 10 Nov 2011 04:32:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>WordPress Tutorial &#8211; Table of Contents in a Page</title>
		<link>http://sorrowfulunfounded.com/tutorials/wordpress/table-of-contents-page</link>
		<comments>http://sorrowfulunfounded.com/tutorials/wordpress/table-of-contents-page#comments</comments>
		<pubDate>Wed, 23 Sep 2009 08:05:35 +0000</pubDate>
		<dc:creator>Chris Clarke</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://sorrowfulunfounded.com/?p=482</guid>
		<description><![CDATA[This tutorial will explain how to add a table of contents to a generic WordPress page. Our table of contents will be a list of posts in chronological order. This tutorial is a follow up to a comment on the post for my WordPress Table of Contents Widget by Ashley who indicated that she would [...]]]></description>
			<content:encoded><![CDATA[<p>This tutorial will explain how to add a table of contents to a generic <a href="http://wordpress.org/">WordPress</a> page. Our table of contents will be a list of posts in chronological order. This tutorial is a follow up to a comment on the post for my <a href="http://sorrowfulunfounded.com/wp-toc-widget/">WordPress Table of Contents Widget</a> by Ashley who indicated that she would be interested in the capability of having a table of contents in a page rather then the sidebar.<span id="more-482"></span></p>
<p>WordPress has the wonderful capability of allowing specific pages to have custom templates. In the initial version of <a href="http://muses-success.info/">Muse&#8217;s Success</a>, we exploited this capability along with custom fields to generate the listing directory and listing detail pages.</p>
<p>Firstly, I would like you to make a copy of your default pages template. This file is usually named page.php, and located in /wp-content/themes/[name of your theme]/ directory of WordPress installation. Name this copy, table_of_contents.php, and open it in your favourite text editor.</p>
<p>Add the following to line 1 of your table_of_contents.php. It needs to be the first thing in your file.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #666666; font-style: italic;">/* Template Name: Table of Contents
*/</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p><strong>Find:</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> the_content<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'&lt;p class=&quot;serif&quot;&gt;Read the rest of this page &amp;raquo;&lt;/p&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>This line may be slightly different, your basically looking for an instance of &lt;?php the_content(&#8216;&#8230;&#8217;). ?&gt; Don&#8217;t worry about what the single parameter, if any.</p>
<p><strong>Immediately after add:</strong></p>
<p>This is the basic code to output post titles in chronological order. You can make slight variations of this, and it in general depends on how you&#8217;ve been naming your posts. In my experience, most authors are placing the chapter number and title in their WordPress post titles, so this example is geared towards these authors, but I will show some alternatives at the end of this tutorial.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$post</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$post_save</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$post</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$category_id</span> <span style="color: #339933;">=</span> get_post_meta<span style="color: #009900;">&#40;</span><span style="color: #000088;">$post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ID</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'toc_category_id'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$toc</span> <span style="color: #339933;">=</span> get_posts<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'numberposts=-1&amp;category='</span><span style="color: #339933;">.</span><span style="color: #000088;">$category_id</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&amp;order=ASC&amp;orderby=date'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$toc</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$post</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;a href=&quot;<span style="color: #000000; font-weight: bold;">&lt;?php</span> the_permalink<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot;&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> the_title<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/a&gt;&lt;br /&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endforeach</span><span style="color: #339933;">;</span> <span style="color: #000088;">$post</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$post_save</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p><strong>Create the Page</strong></p>
<p>Now, we have to create a WordPress page. You can do this by selecting Pages -> Add New in your WordPress Dashboard Menu. It&#8217;s up to you what you name your page, and what you write in the content area, but I would recommend the title is that of your book, and your content area contains a short introduction to your story (like what you would have on the blurb of a print book). </p>
<p><strong>Select the Custom Template</strong></p>
<p>Before you publish your page, we have a few special things we need to do to make the table of contents appear. You&#8217;ll need to select the Table of Contents template in the Attributes section (lower right) of the Add Page page as depicted.</p>
<p><img src="http://sorrowfulunfounded.com/wp-content/uploads/2009/09/attributes.png" alt="Page Attributes Screenshot" title="Page Attributes" width="276" height="237" class="alignnone size-full wp-image-594" /></p>
<p><strong>Set the Custom Field</strong></p>
<p>Next, we have to add a custom field. Custom fields can be added below the page content area. Name your custom field &#8220;toc_category_id&#8221; and set the value to the ID number of the category containing your posts. You can find the ID out by going to the Edit Categories (Dashboard menu -> Posts -> Categories) page, and clicking Edit under the category containing the posts that comprise your story. On the Edit Category page, take a look at your address bar, and you&#8217;ll see the text &#038;cat_ID=. The number after the equals sign is your category ID.</p>
<p><img src="http://sorrowfulunfounded.com/wp-content/uploads/2009/09/custom-field.png" alt="Custom Fields Screenshot" title="Custom Fields Screenshot" width="506" height="166" class="alignnone size-full wp-image-595" /></p>
<p>Click Add Custom Field, and then Publish, and you&#8217;re done. If all went well, when you view the page you just created, you should see your new table of contents.</p>
<p><strong>Using an ordered list instead</strong></p>
<p>Earlier, I mentioned that there are other variations of the code that are suited to different post naming schemes. The one I showed earlier was a basic one title after another list using linebreaks.</p>
<p>If you are one who names their posts solely the name of your chapter with no number, you might like to use an ordered list to automatically add the chapter number for you.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">&lt;ol&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$post</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$post_save</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$post</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$category_id</span> <span style="color: #339933;">=</span> get_post_meta<span style="color: #009900;">&#40;</span><span style="color: #000088;">$post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ID</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'toc_category_id'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$toc</span> <span style="color: #339933;">=</span> get_posts<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'numberposts=-1&amp;category='</span><span style="color: #339933;">.</span><span style="color: #000088;">$category_id</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&amp;order=ASC&amp;orderby=date'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$toc</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$post</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;li&gt;&lt;a href=&quot;<span style="color: #000000; font-weight: bold;">&lt;?php</span> the_permalink<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot;&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> the_title<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/a&gt;&lt;/li&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endforeach</span><span style="color: #339933;">;</span> <span style="color: #000088;">$post</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$post_save</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;/ol&gt;</pre></td></tr></table></div>

<p>Those knowing CSS (and respecting semantics) but naming their chapters with number and name might also want to use the above code, but use CSS to hide the numbers in visual browsers.</p>
]]></content:encoded>
			<wfw:commentRss>http://sorrowfulunfounded.com/tutorials/wordpress/table-of-contents-page/feed/</wfw:commentRss>
		<slash:comments>139</slash:comments>
		</item>
		<item>
		<title>Introduction to PHP: Installing PHP (Windows)</title>
		<link>http://sorrowfulunfounded.com/blog/2009/07/15/introduction-to-php-installing-php-windows/</link>
		<comments>http://sorrowfulunfounded.com/blog/2009/07/15/introduction-to-php-installing-php-windows/#comments</comments>
		<pubDate>Wed, 15 Jul 2009 09:11:16 +0000</pubDate>
		<dc:creator>Chris Clarke</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://sorrowfulunfounded.com/?p=428</guid>
		<description><![CDATA[This tutorial will take you through the steps needed to install PHP on Microsoft Windows. I&#8217;m using Vista, but with minor changes, these instructions should also be valid for Windows XP and Windows 7 as well. I will cover Linux and Mac OS X in another, future post. Instead of going through and telling you [...]]]></description>
			<content:encoded><![CDATA[<p>This tutorial will take you through the steps needed to install <a href="http://php.net">PHP</a> on Microsoft Windows. I&#8217;m using Vista, but with minor changes, these instructions should also be valid for Windows XP and Windows 7 as well.<span id="more-428"></span> I will cover Linux and Mac OS X in another, future post.</p>
<p>Instead of going through and telling you to download Apache, PHP, phpMyAdmin and MySQL individually, I’m going to instruct you to go and download a software package called <a href="http://www.wampserver.com/en/download.php">WampServer</a>. You will need to download the installer from the <a href="http://www.wampserver.com/en/download.php">WampServer</a> webpage. WAMP stands for Windows, Apache, MySQL and PHP meaning the software will install all these at once. This software will install like a normal Windows program, it is safe to just click Next until you reach the end of the installer. The installer will place a shortcut in your Start Menu and on your Desktop called Start WampServer. To start your PHP-enabled web server, all you need to do is double click this icon (in Vista, User Account Control will prompt you, it is safe to click Allow).</p>
<p>To check that your server is working, point your web browser to <a href="http://localhost/">http://localhost/</a>. If you see a page with server information, your server is working. If you don’t, post in the comments and I will help you troubleshoot.</p>
<p><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="wampserverindex" src="http://sorrowfulunfounded.com/wp-content/uploads/2009/07/wampserverindex.jpg" border="0" alt="wampserverindex" width="423" height="234" /></p>
<p>WampServer will add a new icon to your taskbar’s notification area which you can left click and control the various aspects of your new server.</p>
<p><a href="http://sorrowfulunfounded.com/wp-content/uploads/2009/07/wampleftclick.jpg" rel="lightbox[428]"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="wampleftclick" src="http://sorrowfulunfounded.com/wp-content/uploads/2009/07/wampleftclick_thumb.jpg" border="0" alt="wampleftclick" width="346" height="269" /></a></p>
<p>You can use the various options in this menu to configure your server. You will probably be most interested in the PHP submenu where you can easily enable and disable PHP extensions and features without having to deal with configuration files. The option to make changes to PHP using php.ini is still there if you want it though, and it won’t break the ability of WAMP to automatically make changes using the menu.</p>
<p>If you went with the default configuration in the installer, the folder in which you place your files will be located in the following location:</p>
<p>C:\wamp\www</p>
<p>Files and Folders that you place in this folder will be accessible at the following URL:</p>
<p><a href="http://localhost/">http://localhost/</a></p>
<p>It is important to note that this location will only be available on your computer. You can’t go giving this URL to your friends. It isn’t complicated to allow that to happen (with a different URL specific to your PC), but it is beyond the scope of the article.</p>
<p>Congratulations, by just downloading one installer, you now have a fully functional local development server which you can use as a sandbox to learn PHP.</p>
<p>If there is significant interest in a manual installation and configuration process of the individual programs, I will also post a tutorial of that method. In most cases though, for development purposes on Windows, you won’t need to do this.  I should note that I personally use WAMP to quickly get myself setup with a server for development.</p>
]]></content:encoded>
			<wfw:commentRss>http://sorrowfulunfounded.com/blog/2009/07/15/introduction-to-php-installing-php-windows/feed/</wfw:commentRss>
		<slash:comments>32</slash:comments>
		</item>
		<item>
		<title>Introduction to PHP: Hello World + MySQL</title>
		<link>http://sorrowfulunfounded.com/blog/2009/06/30/introduction-to-php-hello-world-mysql/</link>
		<comments>http://sorrowfulunfounded.com/blog/2009/06/30/introduction-to-php-hello-world-mysql/#comments</comments>
		<pubDate>Tue, 30 Jun 2009 04:57:32 +0000</pubDate>
		<dc:creator>Chris Clarke</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://sorrowfulunfounded.com/?p=403</guid>
		<description><![CDATA[This tutorial builds on the previous tutorial &#8211; Introduction to PHP: Hello World and as such, you should take that tutorial first if you have not already. In this tutorial, we are going to take our previous Hello World script, and store the date in a database. We will then add the last access time [...]]]></description>
			<content:encoded><![CDATA[<p>This tutorial builds on the previous tutorial &#8211; Introduction to <a href="http://sorrowfulunfounded.com/2009/06/introduction-to-php-hello-world/">PHP: Hello World</a> and as such, you should take that tutorial first if you have not already.</p>
<p>In this tutorial, we are going to take our previous Hello World script, and store the date in a database. We will then add the last access time to our original script.<span id="more-403"></span> We are going to use <a href="http://mysql.com">MySQL</a> in this tutorial. I will assume that you already have MySQL and <a href="http://phpmyadmin.net">phpMyAdmin</a> (or equivalent) installed.</p>
<p>The state of our script at the end of the last tutorial was as follows:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Hello World. Today\'s date is '</span><span style="color: #339933;">,</span><span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'d/m/Y'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'.'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Our database table will consist of two fields, `ID`, and `current_timestamp`. Both will be of type, integer (INT). You should make `ID` a primary key, and auto_increment. `current_timestamp` can be left with the default options.</p>
<p>For your convenience, I will provide a SQL query to create the table.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #ff0000;">`last_access`</span> <span style="color: #66cc66;">&#40;</span>
<span style="color: #ff0000;">`ID`</span> <span style="color: #993333; font-weight: bold;">INT</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span> <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">,</span>
<span style="color: #ff0000;">`current_timestamp`</span> <span style="color: #993333; font-weight: bold;">INT</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span>
<span style="color: #66cc66;">&#41;</span> ENGINE <span style="color: #66cc66;">=</span> InnoDB</pre></td></tr></table></div>

<p>You can run the above query in the SQL section of phpMyAdmin when viewing your database.</p>
<p>To connect to our database, we use two functions, <a href="http://au.php.net/mysql_connect">mysql_connect</a>, and <a href="http://au.php.net/manual/en/function.mysql-select-db.php">mysql_select_db</a>.</p>
<p>The first function, mysql_connect takes three parameters or arguments. These parameters are the host name (most of the time, localhost), and a username and password. These should have been provided by your web host. If you installed a server on your own computer, it is likely that username is root, with no password. We are going to place the mysql_connect function in a variable called $connection (be sure to change the parameters to suite your own specific situation):</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$connection</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_connect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'localhost'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'USERNAME'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'PASSWORD'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Next, we need to use mysql_select_db to access our specific database. I&#8217;ve called mine date, but you may have called it something different. If you have, change as appropriate. mysql_select_db takes two parameters. These are the $connection variable we just set and the name of your database.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">mysql_select_db</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'date'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$connection</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>You can go ahead and place these functions at the top of our Hello World script.</p>
<p>Next, we are going to use the <a href="http://us.php.net/mysql_query">mysql_query</a> function to insert the current time into our new database table. mysql_query only takes one parameter and this is an SQL statement. SQL stands for structured query language and is how we interact with our database within our applications. I won&#8217;t go into detail about SQL at this time, but for now, <a href="http://www.w3schools.com">W3Schools</a> offers <a href="http://www.w3schools.com/sql/default.asp">a decent introduction</a>.</p>
<p>Within our query, we will also use PHP&#8217;s time function. This function takes no parameters. It returns the current UNIX time stamp which is the number of seconds since the start of 1970. </p>
<p>Place the following in your script after the connection functions, and run it.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'INSERT INTO `last_access` SET `current_timestamp` = \''</span><span style="color: #339933;">.</span><span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'\''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Now, we are going to use the mysql_query function again to retrieve the last access time. This time we will place the mysql_query function into a variable, so we can access the results.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'SELECT `current_timestamp` FROM `last_access` ORDER BY `current_timestamp` DESC LIMIT 1'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Now, we want to be able to retrieve the data our SELECT query is retrieving. We can do this with, mysql_fetch_assoc. It takes one parameter, and this is our query variable which we named $result. Like mysql_query, we will also place this into a variable.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Place the SELECT query before the INSERT query but after the connection information.</p>
<p>Now, all the pieces are in place to put our final script together.</p>
<p>Your script should look like this at this point:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000088;">$connection</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_connect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'localhost'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'USERNAME'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'PASSWORD'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">mysql_select_db</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'date'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$connection</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'SELECT `current_timestamp` FROM `last_access` ORDER BY `current_timestamp` DESC LIMIT 1'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'INSERT INTO `last_access` SET `current_timestamp` = \''</span><span style="color: #339933;">.</span><span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'\''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Hello World. Today\'s date is '</span><span style="color: #339933;">,</span><span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'d/m/Y'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'.'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Now, we are going to add the current hour, minute and second to our date function in our echo language construct. The letters for these are h (hour), i (i since m is reserved for month, the current minute) and s (seconds).</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Hello World. Today\'s date is '</span><span style="color: #339933;">,</span><span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'d/m/Y h:i:s'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'.'</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Next, we want to display the last access date. To do this, we are going to add another echo language construct. Since row only returned one piece of data due to LIMIT 1, we can use it just like we would an array (we will discuss arrays in a later tutorial). For now, all you need to know is that they are used like variables but can store more then one piece of information.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">' This script was last accessed at  '</span><span style="color: #339933;">,</span><span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'d/m/Y h:i:s'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'current_timestamp'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">''</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>You will notice this date function is taking a second parameter. This changes the output of date from the current time/date to the time and date which it was when the timestamp was recorded. </p>
<p>Our final script looks like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000088;">$connection</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_connect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'localhost'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'USERNAME'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'PASSWORD'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">mysql_select_db</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'date'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$connection</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'SELECT `current_timestamp` FROM `last_access` ORDER BY `current_timestamp` DESC LIMIT 1'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'INSERT INTO `last_access` SET `current_timestamp` = \''</span><span style="color: #339933;">.</span><span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'\''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Hello World. Today\'s date is '</span><span style="color: #339933;">,</span><span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'d/m/Y h:i:s'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'.'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">' This script was last accessed at  '</span><span style="color: #339933;">,</span><span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'d/m/Y h:i:s'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'current_timestamp'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>So that concludes this tutorial. As usual, comments, suggestions and most importantly, questions, are welcome.</p>
]]></content:encoded>
			<wfw:commentRss>http://sorrowfulunfounded.com/blog/2009/06/30/introduction-to-php-hello-world-mysql/feed/</wfw:commentRss>
		<slash:comments>81</slash:comments>
		</item>
		<item>
		<title>Introduction to PHP: Hello World</title>
		<link>http://sorrowfulunfounded.com/blog/2009/06/18/introduction-to-php-hello-world/</link>
		<comments>http://sorrowfulunfounded.com/blog/2009/06/18/introduction-to-php-hello-world/#comments</comments>
		<pubDate>Wed, 17 Jun 2009 16:35:41 +0000</pubDate>
		<dc:creator>Chris Clarke</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://sorrowfulunfounded.com/?p=380</guid>
		<description><![CDATA[This tutorial will introduce you to PHP with the customary Hello World script. You will be introduced to the echo language construct and the date() function. The goal of this tutorial is to show you how to output text, and display the current date. I will not explicitly say save this as &#8220;file.php&#8221;, upload to [...]]]></description>
			<content:encoded><![CDATA[<p>This tutorial will introduce you to PHP with the customary Hello World script. You will be introduced to the <a href="http://au.php.net/echo">echo</a> language construct and the <a href="http://au.php.net/manual/en/function.date.php">date()</a> function. The goal of this tutorial is to show you how to output text, and display the current date.<br />
<span id="more-380"></span><br />
I will not explicitly say save this as &#8220;file.php&#8221;, upload to your server, and access in your web browser, but it is strongly encouraged that you do so at each step.</p>
<p>To start, let&#8217;s output the text &#8220;Hello World&#8221; to the screen.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Hello World.'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Any text placed within the quotes will be outputted to your screen when you access the script in your web browser.</p>
<p>Now, we are going to display the current date in addition to our greeting. We can get the current date using the date function which in this example takes one parameter, but usually takes two. We will discuss that in a future tutorial, but the other parameter is a standard <a href="http://en.wikipedia.org/wiki/Unix_time">Unix timestamp</a> &#8211; the number of seconds since 00:00:00 UTC on January 1, 1970. We do not need to specify it here since the date function will assume I am after today&#8217;s date by default.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'d/m/Y'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>The above will output the current date to the screen. The one parameter taken is what defines the format in which our date is returned. The page for <a href="http://php.net/date">date()</a> in the PHP manual has all you need to know about the various ways in which you can format the date.</p>
<p>Now that we have learned how to output some text of our choice, and retrieve today&#8217;s date, let&#8217;s combine the two.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Hello World. Today\'s date is '</span><span style="color: #339933;">,</span><span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'d/m/Y'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'.'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>You should notice the backslash preceding the single quote in &#8220;Today&#8217;s&#8221;. This is called escaping and tells PHP to ignore the character and continue on. It will be discussed in much more detail in a tutorial later in this series.</p>
<p><strong>NOTE:</strong> These tutorials may seem like they are assuming too much knowledge at this point, but you will eventually see that this is by design, and that anything not covered and assumed will be covered in tutorials yet to be written or published. The goal is to have a series of related and dependant tutorials, but at this point I am not writing them in order. The <a href="http://sorrowfulunfounded.com/tutorials/">tutorial page</a> will always have the tutorials listed in the intended reading order.</p>
]]></content:encoded>
			<wfw:commentRss>http://sorrowfulunfounded.com/blog/2009/06/18/introduction-to-php-hello-world/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Introduction to PHP: Includes</title>
		<link>http://sorrowfulunfounded.com/blog/2009/06/11/introduction-to-php-includes/</link>
		<comments>http://sorrowfulunfounded.com/blog/2009/06/11/introduction-to-php-includes/#comments</comments>
		<pubDate>Thu, 11 Jun 2009 12:24:29 +0000</pubDate>
		<dc:creator>Chris Clarke</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[beginner]]></category>
		<category><![CDATA[include_once]]></category>
		<category><![CDATA[incude]]></category>
		<category><![CDATA[require]]></category>
		<category><![CDATA[require_once]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://sorrowfulunfounded.com/?p=358</guid>
		<description><![CDATA[This tutorial will introduce you to the PHP include statement, and its almost identical sibling, require. The include statement is used to insert the contents of one file into the contents of another. Usage: 1 2 3 &#60;?php include &#34;/path/to/file.php&#34;; ?&#62; The included file is essentially merged with the file in which the include takes place, [...]]]></description>
			<content:encoded><![CDATA[<p>This tutorial will introduce you to the <a href="http://au2.php.net/manual/en/function.include.php">PHP include</a> statement, and its almost identical sibling, require.</p>
<p>The include statement is used to insert the contents of one file into the contents of another.<br />
<span id="more-358"></span><br />
<strong>Usage:</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #b1b100;">include</span> <span style="color: #0000ff;">&quot;/path/to/file.php&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>The included file is essentially merged with the file in which the include takes place, and the result in effect is one file containing the contents of both.</p>
<p>For example, we have two files. One is called &#8220;functions.php&#8221;, and the other, &#8220;display.php&#8221;. We are going to include &#8220;functions.php&#8221; in to &#8220;display.php&#8221;.</p>
<p>The contents of each script will be as follows.</p>
<p><strong>functions.php:</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> nice_date<span style="color: #009900;">&#40;</span><span style="color: #000088;">$timestamp</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">return</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'d/m/y'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$timestamp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p><strong>display.php:</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #b1b100;">include</span> <span style="color: #0000ff;">'functions.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Today\'s Date is: '</span><span style="color: #339933;">,</span>nice_date<span style="color: #009900;">&#40;</span><span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Essentially, what we are saying is that the contents of display.php should actually be:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/* including functions.php */</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> nice_date<span style="color: #009900;">&#40;</span><span style="color: #000088;">$timestamp</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">return</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'d/m/y'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$timestamp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">/* finished including functions.php */</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Today\'s Date is: '</span><span style="color: #339933;">,</span>nice_date<span style="color: #009900;">&#40;</span><span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>and the output is as so:</p>
<blockquote><p>Today&#8217;s Date is 11/06/09.</p></blockquote>
<p>You will notice that the PHP opening and closing tags (&lt;?php and ?&gt;) were maintained.PHP automatically closes its tags before and opens them after the inclusion has taken place. This is because PHP does not assume that the file included only contained PHP, for example it may only contain HTML or CSS. </p>
<p>The code within &#8220;functions.php&#8221; was executed just as if it had been in the PHP file containing the include statement in the first place.  &#8220;display.php&#8221; has access to any methods/functions, variables and other things defined in &#8220;functions.php&#8221;, and &#8220;functions.php&#8221; has access to any of those which are specified in &#8220;display.php&#8221; or other files it might include prior to &#8220;functions.php&#8221; inclusion.</p>
<p><strong>How does the require statement differ from the include statement?</strong></p>
<p>With include, if the file does not exist, your PHP script will not halt, and will continue to execute until it reaches the end. In the case of require, the script will cease execution and a fatal error will occur if the file cannot be found. You should use require to include any file you absolutely depend on like a &#8220;config.php&#8221; file containing your database access information.</p>
<p>In usage, require only differs in the text used. It is otherwise identical to include.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #b1b100;">require</span> <span style="color: #0000ff;">&quot;/path/to/file.php&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p><strong>What about require_once and include_once? How are they different?</strong></p>
<p>Appending _once to either require or include will only allow the file to be included once. Future include statements referencing said file will not get included again.</p>
<p>That is the conclusion of my first tutorial &#8211; an introduction to PHP includes. If you have any questions, corrections, suggestions for tutorials, or general comments, please feel free to comment.</p>
]]></content:encoded>
			<wfw:commentRss>http://sorrowfulunfounded.com/blog/2009/06/11/introduction-to-php-includes/feed/</wfw:commentRss>
		<slash:comments>31</slash:comments>
		</item>
	</channel>
</rss>

