<?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>Code Perspective &#187; Tutorial</title>
	<atom:link href="http://codeperspective.ca/tag/tutorial/feed/" rel="self" type="application/rss+xml" />
	<link>http://codeperspective.ca</link>
	<description>Coding is awesome</description>
	<lastBuildDate>Sun, 28 Mar 2010 16:00:46 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Iphone Programming Part 2 &#8211; Intro to Views</title>
		<link>http://codeperspective.ca/2010/03/27/iphone-programming-part-2-intro-to-views/</link>
		<comments>http://codeperspective.ca/2010/03/27/iphone-programming-part-2-intro-to-views/#comments</comments>
		<pubDate>Sat, 27 Mar 2010 19:32:45 +0000</pubDate>
		<dc:creator>Code Perspective Admin</dc:creator>
				<category><![CDATA[Apple]]></category>
		<category><![CDATA[Iphone]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[XCode]]></category>

		<guid isPermaLink="false">http://codeperspective.ca/?p=235</guid>
		<description><![CDATA[Welcome to iphone programming part 2, introduction to views. In this tutorial I'll go over creating an application from scratch that transitions between 2 different views using the iphone's built in UINavigationController and the navigation stack. If you haven't done so already, I suggest reading through my first iphone article. If you already have a basic understanding of how to create an iphone application and hooking up IBOulets and IBActions in interface builder then you can head straight into this article. So let's jump into it, and as always if you have any questions feel free to leave a comment and I'll get back to you.]]></description>
			<content:encoded><![CDATA[<p>Welcome to iphone programming part 2, introduction to views. In this tutorial I&#8217;ll go over creating an application from scratch that transitions between 2 different views using the iphone&#8217;s built in UINavigationController and the navigation stack. If you haven&#8217;t done so already, I suggest reading through my first iphone article. If you already have a basic understanding of how to create an iphone application and hooking up IBOulets and IBActions in interface builder then you can head straight into this article. So let&#8217;s jump into it, and as always if you have any questions feel free to leave a comment and I&#8217;ll get back to you.</p>
<p>Let&#8217;s start out by opening up XCode and creating a new window based application from the standard iphone OS templates. Save the file to wherever you save your iphone files to and create.</p>
<p style="text-align: center;"><a href="http://www.codeperspective.ca/images/ViewTransitionDemo/createNewWindow.png"><img class="aligncenter" title="CreateNewWindowApplication" src="http://www.codeperspective.ca/images/ViewTransitionDemo/createNewWindow.png" alt="" width="287" height="217" /></a>Once you create and save your new window-based application, open up the classes and resources folders in the XCode folder explorer. Open up the ViewDemoAppDelegate.h and insert the following code&#8230;</p>
<p style="text-align: left;">

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//</span>
<span style="color: #666666; font-style: italic;">//  ViewDemoAppDelegate.h</span>
<span style="color: #666666; font-style: italic;">//  ViewDemo</span>
<span style="color: #666666; font-style: italic;">//</span>
<span style="color: #666666; font-style: italic;">//  Created by Mark Hazlett on 10-03-26.</span>
<span style="color: #666666; font-style: italic;">//  Copyright Code Perspective 2010. All rights reserved.</span>
<span style="color: #666666; font-style: italic;">//</span>
&nbsp;
<span style="color: #339933;">#import &lt;UIKit/UIKit.h&gt;</span>
@interface ViewDemoAppDelegate <span style="color: #339933;">:</span> NSObject
<span style="color: #009900;">&#123;</span>
    UIWindow <span style="color: #339933;">*</span>window<span style="color: #339933;">;</span>
    UINavigationController <span style="color: #339933;">*</span>navController<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
@property <span style="color: #009900;">&#40;</span>nonatomic<span style="color: #339933;">,</span> retain<span style="color: #009900;">&#41;</span> IBOutlet UIWindow <span style="color: #339933;">*</span>window<span style="color: #339933;">;</span>
&nbsp;
@end</pre></div></div>

<p>All we&#8217;re doing here is creating a UINavigationController object so that we can access it in our application delegate implementation file. Let&#8217;s go ahead and open up our ViewDemoAppDelegate.m and add the following code to the application did finish launching method.</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span><span style="color: #009900;">&#41;</span>applicationDidFinishLaunching<span style="color: #339933;">:</span><span style="color: #009900;">&#40;</span>UIApplication <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>application
<span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">//Allocate and instatiate a new UINavigationController Object</span>
	navController <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#91;</span>UINavigationController alloc<span style="color: #009900;">&#93;</span> init<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">//Allocate and initialize a First View Controller Object</span>
	FirstViewController <span style="color: #339933;">*</span>firstViewController <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#91;</span>FirstViewController alloc<span style="color: #009900;">&#93;</span>init<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">//Push the first view controller onto the view controller stack without animation</span>
	<span style="color: #009900;">&#91;</span>navController pushViewController<span style="color: #339933;">:</span>firstViewController animated<span style="color: #339933;">:</span>NO<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">//Some memory management</span>
	<span style="color: #009900;">&#91;</span>firstViewController release<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">//Make it visible</span>
	<span style="color: #009900;">&#91;</span>window	addSubview<span style="color: #339933;">:</span>navController.<span style="color: #202020;">view</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Override point for customization after application launch</span>
    <span style="color: #009900;">&#91;</span>window makeKeyAndVisible<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The first thing we&#8217;re doing in this function is instantiating the UINavigationController that we declared in our header. We pass it an alloc and init to allocate the appropriate amount of memory for it as well as an init to initialize it properly. The next thing we&#8217;re going to do is create a new FirstViewController object. Now you may be asking what a FirstViewController object is? That&#8217;s because we haven&#8217;t created it yet. So let&#8217;s go ahead and go to &#8220;File &gt; New File&#8221; and add a new file that&#8217;s of the type UIViewController subclass. Make sure you check the little check box that says &#8220;With XIB for user interface&#8221;. This will give you an XIB file to work with instead of producing your interface programmatically. Now that we have our FirstViewControllerObject the only thing we have to do is import the header into the ViewDemoAppDelegate. So make sure to declare #import &#8220;FirstViewController.h&#8221; at the top of your code. Now onto the rest of the code in our application did finish launching method. The first thing we do with our allocated view controller is push it onto the navigation stack. Now because this is our first view we&#8217;re pushing onto the navigation stack we want to make sure to pass &#8220;NO&#8221; to the animated parameter because we don&#8217;t want to animate the root view when it&#8217;s pushed onto the stack. Now for some memory management. Once the FirstViewController is pushed onto the navigation stack the retain count on the object moves up to 2, therefore we can perform a release on the FirstViewController object so that the retain count stays at 1. Last thing we do in this method is add the view so our window so that it&#8217;s actually visible when we load the application.</p>
<p>Now we&#8217;re going to get into some interface builder stuff. Let&#8217;s start by opening up our FirstViewController.xib file in Interface Builder(or by just double clicking on it). You should see just a plain white view. Now in order to transition between views we need a button to press to actually transition between views. So let&#8217;s go ahead and add a UIButton to the view. Label the button anything you would like to to transition between views. Go ahead and save that file for now(we&#8217;ll come back to it later to connect the pointers). Next we&#8217;re going to create a new file in our project that&#8217;s a subclass of UIViewController. Again we&#8217;re going to make sure that &#8220;use XIB for user interface&#8221; is selected and press create. We&#8217;re going to name this view controller &#8220;SecondViewController&#8221; as it&#8217;s the one that we transition to from pressing the button on our first view controller. Once you create the new View Controller you should see it appear on the left hand side in your package explorer. Let&#8217;s go into our FirstViewController.h and add the following code.</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#import &lt;UIKit/UIKit.h&gt;</span>
@interface FirstViewController <span style="color: #339933;">:</span> UIViewController
<span style="color: #009900;">&#123;</span>
	IBOutlet UIButton <span style="color: #339933;">*</span>transitionButton<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span>IBAction<span style="color: #009900;">&#41;</span> pushViewContoller<span style="color: #339933;">:</span><span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span> send<span style="color: #339933;">;</span>
&nbsp;
@end</pre></div></div>

<p>All we do here is create an IBOutlet for the UIButton we added to the XIB file and call it transition button. Then we create an IBAction method that will get called whenever the user presses inside the UIButton. Let&#8217;s open up our FirstViewController.m implementation file and implement the IBAction method like so&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#import &quot;FirstViewController.h&quot;</span>
<span style="color: #339933;">#import &quot;SecondViewController.h&quot;</span>
&nbsp;
@implementation FirstViewController
&nbsp;
<span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span>IBAction<span style="color: #009900;">&#41;</span> pushViewContoller<span style="color: #339933;">:</span><span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span> send
<span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">//Allocate and instantiate the second view controller object</span>
	SecondViewController <span style="color: #339933;">*</span>secondView <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#91;</span>SecondViewController alloc<span style="color: #009900;">&#93;</span> init<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">//Push the secondViewController onto the navigation stack</span>
	<span style="color: #009900;">&#91;</span>self.<span style="color: #202020;">navigationController</span> pushViewController<span style="color: #339933;">:</span>secondView animated<span style="color: #339933;">:</span>YES<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">//Some memory management</span>
	<span style="color: #009900;">&#91;</span>secondView release<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now as you can see, we first provide our implementation with an import of the SecondViewController.h that we created earlier. We then proceed to allocate and instantiate our SecondViewController object. This time we&#8217;re going to push it onto our navigation stack and passing the YES parameter to our animated option because this time we do want it to be be animated when we press the button. After that we just provide some basic memory management to bring the retain count of the SecondViewController object back down to 1. That&#8217;s al the necessary code we need right now, however if you run this program right now you&#8217;ll notice that if you press the button on the first view controller it won&#8217;t really do anything. So let&#8217;s open back up our FirstViewController.xib file and hook up our IBOutlet and IBAction to our button that we created. After you&#8217;re finished hooking up these actions in the File&#8217;s owner connections inspector, the file&#8217;s owner connections inspector should look something like this..</p>
<p><a href="http://www.codeperspective.ca/images/ViewTransitionDemo/completedConnectionInspec.png"><img class="aligncenter" title="Completed Connections Inspec" src="http://www.codeperspective.ca/images/ViewTransitionDemo/completedConnectionInspec.png" alt="" width="301" height="247" /></a>Now you should be able to run the application and press the button and see the transition to the second view controller(which will be blank unless you added something to the nib) and press the back button in the navigation controller and transition back to the first screen.</p>
<p>If you&#8217;re having some troubles, here is the project and all the code. <a title="View Demo Download" href="http://codeperspective.ca/ViewDemo.zip" target="_blank">Download</a></p>
<p>Also again, if you have any questions feel free to leave them in the comments section.</p>
]]></content:encoded>
			<wfw:commentRss>http://codeperspective.ca/2010/03/27/iphone-programming-part-2-intro-to-views/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Iphone Programming Part 1 &#8211; Intro to Xcode and IB</title>
		<link>http://codeperspective.ca/2010/03/01/iphone-programming-part-1-intro-to-xcode-and-ib/</link>
		<comments>http://codeperspective.ca/2010/03/01/iphone-programming-part-1-intro-to-xcode-and-ib/#comments</comments>
		<pubDate>Mon, 01 Mar 2010 23:40:01 +0000</pubDate>
		<dc:creator>Code Perspective Admin</dc:creator>
				<category><![CDATA[Apple]]></category>
		<category><![CDATA[Mac OS X Hints]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Interface Builder]]></category>
		<category><![CDATA[Iphone]]></category>
		<category><![CDATA[XCode]]></category>

		<guid isPermaLink="false">http://codeperspective.ca/?p=217</guid>
		<description><![CDATA[First off before we start, here are a couple of things you will need to get started:
1. Mac running the latest version of xcode &#8211; This can be found on your install disc for leopard or snow leopard.
2. Latest version of the iphone SDK &#8211; This can be found at developer.apple.com
Now we can get started. [...]]]></description>
			<content:encoded><![CDATA[<p>First off before we start, here are a couple of things you will need to get started:</p>
<p>1. Mac running the latest version of xcode &#8211; This can be found on your install disc for leopard or snow leopard.</p>
<p>2. Latest version of the iphone SDK &#8211; This can be found at <a title="Apple Developer Connection" href="http://developer.apple.com" target="_blank">developer.apple.com</a></p>
<p>Now we can get started. Once you have xcode and the iphone SDK installed, open up xcode(Macintosh HD &gt; developer &gt; Applications). Once you have xcode installed, from the menu bar select file &gt; new project. You should see a windows like the this one.</p>
<p><a href="http://codeperspective.ca/images/XCodePart1/CreateNewProjectXcode.png"><img class="aligncenter" title="Create New XCode Project" src="http://codeperspective.ca/images/XCodePart1/CreateNewProjectXcode.png" alt="" width="716" height="641" /></a></p>
<p>Select window based application from the iphone application option on the left hand side of the window. Once you select the window based application, xcode will prompt you to save the project. I named my project &#8220;LabelTester&#8221; and saved it under my personal iphone projects directory. Once you save the project, an xcode window should open up and look something like this.</p>
<p style="text-align: center;"><a href="http://codeperspective.ca/images/XCodePart1/initialXcodeView.png"><img class="aligncenter" title="Initial XCode View" src="http://codeperspective.ca/images/XCodePart1/initialXcodeView.png" alt="" width="713" height="550" /></a></p>
<p>You should see a combination of files here in the browser in the code browser. The first should be the CoreGraphics.framework. This is just a set of files provided by Apple that have all the code relevant to the core graphics libraries in Cocoa. The second is the foundation.framework. This is the library set for the foundation framework which is basically all the classes like NSArray, NSString and other foundation objects. The next item should be LabelTester.plist. The plist, or preference list, is a set of XML attributes that control how the application is handled. For the sake of this tutorial, you won&#8217;t need to open the plist. The next item should be the LabelTester.app file, which is the executable for your application. Once your application is deployed, all files and resources in your project will be located inside your app&#8217;s .app file. The next file is the LabelTester.pch file. This file is the prefix header for your application. The LabelTesterAppDelegate.h file is the next file in the list. This file is the header file for your application delegate. This is the starting for your application, the app delegate is what&#8217;s run by your main.m file when you app is launched. The .h file is of course the header file, which is basically the interface file that the .m file will implement. The next file is of course the LabelTesterAppDelegate.m. This file is the implementation file for the app delegate header. Next is the main.m file. This file is the application launcher for your app. It basically runs the app delegate. The next file is the Main Window.xib file. Anything with a nib/xib file extension is classified as an interface builder file and you open these files in the Interface builder application. Finally, the UIKit.framework. This library is used to correspond to all the UI elements on the iphone(text fields, buttons, etc).</p>
<p>Let&#8217;s start by opening up our Main Window.xib to setup our interface. Start by dragging a button from the libraries window onto your main View. Rename that button by double clicking on the button and change it to say &#8220;Update Label!&#8221;. Next, grab a text field and drag it just above the button. Expand the text field to fit the majority of the screen. Select the textfield and go into the attributes tab and enter into the placeholder field, &#8220;Enter Text to update Label&#8221;. Once that&#8217;s finished, drag a label onto the main window and change the text from &#8220;label&#8221; to &#8220;Label to Update&#8221;. Once that&#8217;s done you can save the interface builder file by pressing (command + s) and switch your view back to your xcode project.</p>
<p><a href="http://codeperspective.ca/images/XCodePart1/IBIntroPage.png"><img class="aligncenter" title="IB Intro Page" src="http://codeperspective.ca/images/XCodePart1/IBIntroPage.png" alt="" width="707" height="710" /></a><a href="http://codeperspective.ca/images/XCodePart1/IBAddItems.png"><img class="aligncenter" title="IB Add Items" src="http://codeperspective.ca/images/XCodePart1/IBAddItems.png" alt="" width="400" height="582" /></a></p>
<p>Open up the LabelTesterAppDelegate.h file and change the file to reflect the following code.</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#import </span>
&nbsp;
@interface LabelTesterAppDelegate <span style="color: #339933;">:</span> NSObject
<span style="color: #009900;">&#123;</span>
    UIWindow <span style="color: #339933;">*</span>window<span style="color: #339933;">;</span>
	IBOutlet UITextField <span style="color: #339933;">*</span>textField<span style="color: #339933;">;</span>
	IBOutlet UIButton <span style="color: #339933;">*</span>button<span style="color: #339933;">;</span>
	IBOutlet UILabel <span style="color: #339933;">*</span>label<span style="color: #339933;">;</span>
	IBAction <span style="color: #339933;">*</span>updateButtonPressed<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
@property <span style="color: #009900;">&#40;</span>nonatomic<span style="color: #339933;">,</span> retain<span style="color: #009900;">&#41;</span> IBOutlet UIWindow <span style="color: #339933;">*</span>window<span style="color: #339933;">;</span>
@property <span style="color: #009900;">&#40;</span>nonatomic<span style="color: #339933;">,</span> retain<span style="color: #009900;">&#41;</span> IBOutlet UITextField <span style="color: #339933;">*</span>textField<span style="color: #339933;">;</span>
@property <span style="color: #009900;">&#40;</span>nonatomic<span style="color: #339933;">,</span> retain<span style="color: #009900;">&#41;</span> IBOutlet UILabel <span style="color: #339933;">*</span>label<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #339933;">-</span><span style="color: #009900;">&#40;</span>IBAction<span style="color: #009900;">&#41;</span>updateButtonpressed<span style="color: #339933;">:</span><span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span> sender<span style="color: #339933;">;</span>
&nbsp;
@end</pre></div></div>

<p>The first lines inside the curly braces will declare variables that we will be hooking up in Interface builder later. After the curly braces are closed, we have these property tags. These will allow us to have access to the objects and their data in the implementation file. After that we have an IBAction method declaration. This will eventually be hooked up to our button to run every time that our button is pressed. You can think of an IBAction method as an event handler in OBjective-C. Now let&#8217;s move onto our LabelTesterAppDelegate.m file and add the following method directly below out import statement.</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#import &quot;LabelTesterAppDelegate.h&quot;</span>
&nbsp;
@implementation LabelTesterAppDelegate
&nbsp;
@synthesize window<span style="color: #339933;">;</span>
@synthesize textField<span style="color: #339933;">;</span>
@synthesize label<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #339933;">-</span><span style="color: #009900;">&#40;</span>IBAction<span style="color: #009900;">&#41;</span>updateButtonpressed<span style="color: #339933;">:</span><span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span> sender
<span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">//allocate a string and initialize it with the data from the text field</span>
	NSString <span style="color: #339933;">*</span><span style="color: #993333;">string</span> <span style="color: #339933;">=</span> textField.<span style="color: #202020;">text</span><span style="color: #339933;">;</span>
&nbsp;
	label.<span style="color: #202020;">text</span> <span style="color: #339933;">=</span> <span style="color: #993333;">string</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Basically what we&#8217;re doing here is just creating that IBAction method that we declared in our interface file. Inside that method, we allocate a string to the value of our textField.text and then set the label equal to the string that we get from the textfield. That&#8217;s all the code we&#8217;ll need for now. Don&#8217;t forget to synthesize your objects below the implementation declaration in order to have access to the data you want. If you try and run this now, the application will run but will not function because the pointers in Interface Builder are not hooked up. So let&#8217;s go ahead and do that next. Save all your xcode files and open back up your Main Window.xib file in Interface Builder.</p>
<p>In order to setup a pointer in Interface builder, start by clicking on the application delegate and then the connections manager should appear. Beside each of the items that you created you should see a tiny circle. For each, click on the circle and drag the pointer to each of the elements in your window. This will create a pointer to that element. Now click on the button and then open it&#8217;s connection manager. Find the &#8220;touch up inside&#8221; connection and drag that pointer to the &#8220;file&#8217;s owner&#8221; section in the library and select &#8220;updateButtonPressed&#8221; when it appears. Once this is completed, you should see the connection screen look something like this.</p>
<p><a href="http://codeperspective.ca/images/XCodePart1/IBConnections.png"><img class="aligncenter" title="IB Add Connections" src="http://codeperspective.ca/images/XCodePart1/IBConnections.png" alt="" width="301" height="259" /></a>That&#8217;s all folks, now if you run the application and enter some text into the text field, it will update the label to whatever text you entered. To download the whole project click <a title="XcodeProject For Label Tester" href="http://codeperspective.ca/LabelTester.zip" target="_self">here</a>.</p>
<p style="text-align: center;"><a style="text-decoration: none;" href="http://codeperspective.ca/images/XCodePart1/iphoneFinish.png"><img class="aligncenter" title="Iphone Finished Product" src="http://codeperspective.ca/images/XCodePart1/iphoneFinish.png" alt="" width="290" height="539" /></a></p>
<p style="text-align: center;">Cheers everyone and stay tuned for part 2!</p>
]]></content:encoded>
			<wfw:commentRss>http://codeperspective.ca/2010/03/01/iphone-programming-part-1-intro-to-xcode-and-ib/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Intro to PHP &#8211; Creating a Basic Login System</title>
		<link>http://codeperspective.ca/2009/12/13/intro-to-php-creating-a-basic-login-system/</link>
		<comments>http://codeperspective.ca/2009/12/13/intro-to-php-creating-a-basic-login-system/#comments</comments>
		<pubDate>Mon, 14 Dec 2009 04:43:18 +0000</pubDate>
		<dc:creator>Code Perspective Admin</dc:creator>
				<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://codeperspective.ca/?p=181</guid>
		<description><![CDATA[Using a little knowledge of PHP and HTML you can build a simple log in system for your site. In order to correctly follow this tutorial you may want to have a good idea of basic HTML/PHP syntax as well as knowing some CSS. You should also have a basic understanding of how to create and use sessions($_SESSIONS) in PHP.]]></description>
			<content:encoded><![CDATA[<p>Using a little knowledge of PHP and HTML you can build a simple log in system for your site. In order to correctly follow this tutorial you may want to have a good idea of basic HTML/PHP syntax as well as knowing some CSS. You should also have a basic understanding of how to create and use <a title="Intro to PHP - Using Sessions and cookies" href="http://codeperspective.ca/2009/12/09/intro-to-php-intro-to-sessions-and-cookies/" target="_blank">sessions($_SESSIONS) in PHP</a>.</p>
<p>Alright let&#8217;s get started by just creating a basic log in window and place it in the center of the screen. First the log in window.</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #00bbdd;">&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01//EN&quot; &quot;http://www.w3.org/TR/html4/strict.dtd&quot;&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">title</span>&gt;</span>Sample Log In<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&gt;</span>
&nbsp;
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">link</span> <span style="color: #000066;">rel</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;stylesheet&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;loginCSS.css&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/css&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;outerLimit&quot;</span>&gt;</span>
			<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;container&quot;</span>&gt;</span>
				<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;innerLimit&quot;</span>&gt;</span>
					<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;loginBox&quot;</span>&gt;</span>
						<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">form</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;login&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;login&quot;</span> <span style="color: #000066;">method</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;post&quot;</span> <span style="color: #000066;">action</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;userAuth.php&quot;</span>&gt;</span>
							<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">label</span> <span style="color: #000066;">for</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;username&quot;</span>&gt;</span>Username:<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">label</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">br</span> <span style="color: #66cc66;">/</span>&gt;</span>
							<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;username&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;username&quot;</span> <span style="color: #000066;">size</span><span style="color: #66cc66;">=</span><span style="color: #cc66cc;">40</span> <span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">br</span> <span style="color: #66cc66;">/</span>&gt;</span>
							<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">label</span> <span style="color: #000066;">for</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;password&quot;</span>&gt;</span>Password:<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">label</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">br</span> <span style="color: #66cc66;">/</span>&gt;</span>
							<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;password&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;password&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;password&quot;</span> <span style="color: #000066;">size</span><span style="color: #66cc66;">=</span><span style="color: #cc66cc;">40</span> <span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">br</span> <span style="color: #66cc66;">/</span>&gt;</span>
&nbsp;
							<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Log In&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
						<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">form</span>&gt;</span>
						<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">br</span> <span style="color: #66cc66;">/</span>&gt;</span>
					<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
				<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
			<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span></pre></div></div>

<p>Next let&#8217;s move into a bit of the CSS. Basically we just want to create a window with a gray background so the fields are extremely easy to see. I called the CSS file in this case loginCSS.css but you can call it whatever you like as long as it&#8217;s referenced properly in the HTML  section.</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;">body
<span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span><span style="color: #993333;">white</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">min-height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">468px</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">min-width</span><span style="color: #00AA00;">:</span><span style="color: #933;">552px</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">font-family</span><span style="color: #00AA00;">:</span>Helvetica<span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
a
<span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">font-size</span><span style="color: #00AA00;">:</span>small<span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #cc00cc;">#loginBox</span>
<span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#e4e4e4</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span><span style="color: #993333;">relative</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">top</span><span style="color: #00AA00;">:</span><span style="color: #933;"><span style="color: #cc66cc;">25</span>%</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">:</span><span style="color: #933;"><span style="color: #cc66cc;">25</span>%</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span><span style="color: #933;">300px</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span><span style="color: #933;">15px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #cc00cc;">#outerLimit</span>
<span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span><span style="color: #933;"><span style="color: #cc66cc;">100</span>%</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span><span style="color: #933;"><span style="color: #cc66cc;">100</span>%</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span>table<span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">vertical-align</span><span style="color: #00AA00;">:</span><span style="color: #993333;">middle</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #cc00cc;">#container</span>
<span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span><span style="color: #993333;">relative</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">vertical-align</span><span style="color: #00AA00;">:</span><span style="color: #993333;">middle</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">display</span><span style="color: #3333ff;">:table-</span>cell<span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span><span style="color: #933;">468px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #cc00cc;">#innerLimit</span>
<span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span><span style="color: #933;">552px</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span><span style="color: #933;">468px</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">margin-left</span><span style="color: #00AA00;">:</span><span style="color: #993333;">auto</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">margin-right</span><span style="color: #00AA00;">:</span><span style="color: #993333;">auto</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>As you can see, I just styled the page a little bit to make it a little bit easier for the user to use. Now that we have a basic page for the user to log in, we&#8217;ll also need a page, such as a dashboard, for the user to access when they enter in correct login criteria. In this case we&#8217;re just going to use a page that has a basic header telling the user they have logged in correctly. The page can look something like this.</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;">&nbsp;
<span style="color: #00bbdd;">&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01//EN&quot; &quot;http://www.w3.org/TR/html4/strict.dtd&quot;&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">title</span>&gt;</span>Sample Log In<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&gt;</span>
&nbsp;
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">link</span> <span style="color: #000066;">rel</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;stylesheet&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;loginCSS.css&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/css&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">h1</span>&gt;</span>You are now logged in!<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">h1</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;logout.php&quot;</span>&gt;</span>Log Out Here<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span></pre></div></div>

<p>As you can see I have also included a log out link just below the h1 tag. If a user logs into the system we also want them to be able to log out. Next we&#8217;re going to create the SQL for our database. For this tutorial I&#8217;ll be using MySQL, however Oracle or SQL Server could also be used.</p>
<p>Note: If you&#8217;re going to use a different database then the code to connect to the database will be slightly different.</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">DROP</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #993333; font-weight: bold;">IF</span> <span style="color: #993333; font-weight: bold;">EXISTS</span> <span style="color: #ff0000;">'testDB'</span><span style="color: #66cc66;">.</span><span style="color: #ff0000;">'user'</span>
&nbsp;
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #ff0000;">'testDB'</span><span style="color: #66cc66;">.</span><span style="color: #ff0000;">'user'</span>
<span style="color: #66cc66;">&#40;</span>
	<span style="color: #ff0000;">'username'</span> VARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
	<span style="color: #ff0000;">'password'</span> VARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> <span style="color: #ff0000;">'testDB'</span><span style="color: #66cc66;">.</span><span style="color: #ff0000;">'user'</span>
<span style="color: #66cc66;">&#40;</span>username<span style="color: #66cc66;">,</span> password<span style="color: #66cc66;">&#41;</span>
<span style="color: #993333; font-weight: bold;">VALUES</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'admin'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'password'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'guest'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'password'</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>This is just a very simple database table that contains only a username and password. This script could obviously be modified to accept further fields of to encrypt the password field to increase the security of your system. Things like that will be out of the scope of this tutorial however.</p>
<p>Now that we have our environment set up we can start to use PHP to get data from the forms and compare them with the database. For the this tutorial we will only be using the POST method of retrieving data from forms. Using GET is often not recommended when getting sensitive data such as usernames and passwords. As you can see from the log in window, we have used the POST method to get the data, and when the form is submitted we are calling the userAuth.php file. Let&#8217;s go ahead and create that file now.</p>
<p>This block of code we will be using to check to see if the fields are blank. If they are we want to send them back to the login.html file to log back in again using the header() function. Next we want to enter all the configuration information to connect to the database and use some PHP functions to protect our site from SQL injection errors.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">/*Create the variables to hold the database information*/</span>
&nbsp;
<span style="color: #000088;">$dbName</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;testDB&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/*Table name that we're checking against*/</span>
&nbsp;
<span style="color: #000088;">$dbTable</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;user&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/*Database username*/</span>
&nbsp;
<span style="color: #000088;">$dbUsername</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;DBUsername&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/*Database Password*/</span>
&nbsp;
<span style="color: #000088;">$dbPassword</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;DBPassword&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$username</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$password</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/*This next block is to prevent SQL injection hacks*/</span>
<span style="color: #000088;">$username</span> <span style="color: #339933;">=</span> <span style="color: #990000;">stripslashes</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$username</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$password</span> <span style="color: #339933;">=</span> <span style="color: #990000;">stripslashes</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$password</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$username</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$username</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$password</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$password</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>I have put in default values into the variables. Variables such as $dbTable and $dbUsername will be subject to change based on your database configuration. These values will need to be filled in with correct values from your system. Next we&#8217;re going to be connecting to the database and opening a connection.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">/*Connect to the Database*/</span>
&nbsp;
<span style="color: #000088;">$connection</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><span style="color: #990000;">mysql_connect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;localhost&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$dbUsername</span><span style="color: #339933;">,</span> <span style="color: #000088;">$dbPassword</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">mysql_error</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: #666666; font-style: italic;">/*Select the database you want to access*/</span>
&nbsp;
<span style="color: #000088;">$db</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><span style="color: #990000;">mysql_select_db</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dbName</span><span style="color: #339933;">,</span> <span style="color: #000088;">$connection</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">mysql_error</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: #666666; font-style: italic;">/*Select statement from the database to see if the user is in the system*/</span>
&nbsp;
<span style="color: #000088;">$sqlQuery</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT COUNT(*) FROM <span style="color: #006699; font-weight: bold;">$table_name</span> WHERE username = '<span style="color: #006699; font-weight: bold;">$username</span>'
AND password = password('<span style="color: #006699; font-weight: bold;">$password</span>')&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/*Create a variable to hold the results of the SQL query*/</span>
&nbsp;
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sqlQuery</span><span style="color: #339933;">,</span> <span style="color: #000088;">$connection</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">mysql_error</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: #666666; font-style: italic;">/*Check the number of rows returned from the query*/</span>
&nbsp;
<span style="color: #000088;">$num</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_num_rows</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></div></div>

<p>Now we&#8217;re getting to the user authentication section. We&#8217;re now going to see if the number of rows that was selected is anything but 0. In theory it should ever only come back with 1 or 0 rows, however in systems where users can multiple accounts it&#8217;s good to keep it to != 0.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">/*If the number of rows is not equal to 0 then authenticate the user*/</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$num</span> <span style="color: #339933;">!=</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #990000;">session_start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">//Create a session for the username and password</span>
	<span style="color: #990000;">session_register</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$username</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">session_register</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$password</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">session_is_registered</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$username</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;location:login.html&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">else</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">//because the user is authenticated move them to the dashboard</span>
		<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Location: home.html&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">else</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Location: login.html&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">exit</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>We then register a session and store the username and password in the session. If the session has been registered correctly then you can send the user to the home screen. If the session is not registered correctly for some reason we don&#8217;t want the users accessing the system, so we send them back to the login screen to attempt to log in again.</p>
<p>That&#8217;s it, an entire login system. Of course this is just meant to give you the idea of the steps and should by no means be used in a real-world system as is. However you should have a firm understanding of how a basic login system works using PHP/MySQL and a little bit of HTML and CSS. As always if you have any questions/concerns feel free to leave a comment and I&#8217;ll get back to you asap.</pre>
]]></content:encoded>
			<wfw:commentRss>http://codeperspective.ca/2009/12/13/intro-to-php-creating-a-basic-login-system/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Intro to PHP &#8211; Intro to Sessions and Cookies</title>
		<link>http://codeperspective.ca/2009/12/09/intro-to-php-intro-to-sessions-and-cookies/</link>
		<comments>http://codeperspective.ca/2009/12/09/intro-to-php-intro-to-sessions-and-cookies/#comments</comments>
		<pubDate>Wed, 09 Dec 2009 17:27:58 +0000</pubDate>
		<dc:creator>Code Perspective Admin</dc:creator>
				<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://codeperspective.ca/?p=177</guid>
		<description><![CDATA[Cookies and Sessions are extremely useful to a PHP programmer, however they could be considerably confusing to a beginner. Cookies and sessions are used to store pieces of information for a period of time until it is destroyed. This information inside of a cookie or a session can be used to store things like log in information, URL information and much much more.]]></description>
			<content:encoded><![CDATA[<p>Cookies and Sessions are extremely useful to a PHP programmer, however they could be considerably confusing to a beginner. Cookies and sessions are used to store pieces of information for a period of time until it is destroyed. This information inside of a cookie or a session can be used to store things like log in information, URL information and much much more.</p>
<h2>Cookies</h2>
<p>Cookies are pieces of information stored on the client&#8217;s machine. The disadvantage here is that cookies can be disabled or ignored from the client&#8217;s end. So if you&#8217;re using cookies, try not to use them for anything that is crucial to the system functioning(this is what sessions are for&#8230; more on that later). The syntax for creating a cookie looks like this&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//A cookie can be equal to a string</span>
<span style="color: #000088;">$_COOKIE</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'nameOfCookie'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'What information you want to store here'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//or a cookie can be equal to a variable</span>
<span style="color: #000088;">$_COOKIE</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'nameOfCookie'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$username</span><span style="color: #339933;">;</span></pre></div></div>

<p>According to PHP variable declaration rules, you can name the cookie whatever you would like as long as it doesn&#8217;t break any of the PHP rules. Data from created cookies can then be used anywhere you like just like any variable such as&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//print the value of the cookie</span>
<span style="color: #b1b100;">print</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_COOKIE</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'nameOfCookie'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Alternately, you can use the isset() method to check to see if a specific cookie has been set. This is more useful when using sessions but we&#8217;ll get to that later.</p>
<h2>Sessions</h2>
<p>Session differ from cookies in that they are stored on the server as opposed to the client. Sessions offer one major advantage over cookies, they cannot be disabled by the user, thus making sessions much more secure than cookies. When dealing with things like log in information and secure data, session are a much better choice than cookies. Sessions are declared using the same conventions as cookies&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//a session can be equal to a string</span>
<span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'nameOfSession'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'What information you want to store in the session'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//or a session can be equal to a variable</span>
<span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'nameOfSession'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$username</span><span style="color: #339933;">;</span></pre></div></div>

<p>This will declare a session variable, however there is one key function that needs to be created prior to using a session variable. The session_start() function needs to be called before any data is sent from the server. Even if a spare white space is sent, the session_start() function will most likely send an error. Whenever you want to use a session variable in a PHP file/function, you must call the session_start() function at the very beginning of the page in order to use a session variable that has been created. This needs to occur even prior to sending the DOCTYPE to the client. To use a session variable the syntax is as follows&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//start the session</span>
<span style="color: #990000;">session_start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//print the session variable</span>
<span style="color: #b1b100;">print</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'nameOfSession'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The syntax is similar to using a cookie and alternatively you can use the isset() method to check if a session variable has been created or not. This becomes particularly useful when it comes to creating login systems that keeps users logged in using a session.</p>
<p>In full, you can use sessions or cookies if you like but keep in mind that sessions are stored on the server and cannot be disabled by a user, thus making them much more secure and reliable. In order to create a session, remember to call the session_start() method at the very start of the document or you may not be able to use the session variables.</p>
]]></content:encoded>
			<wfw:commentRss>http://codeperspective.ca/2009/12/09/intro-to-php-intro-to-sessions-and-cookies/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>JQuery Part 3 &#8211; Intro to Functions</title>
		<link>http://codeperspective.ca/2009/10/31/jquery-part-3-intro-to-functions/</link>
		<comments>http://codeperspective.ca/2009/10/31/jquery-part-3-intro-to-functions/#comments</comments>
		<pubDate>Sun, 01 Nov 2009 00:24:00 +0000</pubDate>
		<dc:creator>Code Perspective Admin</dc:creator>
				<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[JQuery]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[Web Design]]></category>

		<guid isPermaLink="false">http://codeperspective.ca/?p=150</guid>
		<description><![CDATA[Often when writing JQuery code(especially for plugins) there are times when you need to execute the same block of code multiple times. Instead of writing them all out individually and making our code really terrible process wise, we can use functions and function calls to call the same block of code over and over.]]></description>
			<content:encoded><![CDATA[<p>This is part 3 in a series of JQuery tutorials. I strongly recommend at least reading <a href="http://codeperspective.ca/2009/10/28/introduction-to-jquery-part-1/">Part 1</a> before continuing with this tutorial. <a href="http://codeperspective.ca/2009/10/29/jquery-part-2-basic-animation/">Part 2</a> is optional but would also be a great background before we get started. As for this tutorial, I will be going over creating an executing functions to make your code more efficient.</p>
<p>Often when writing JQuery code(especially for plugins) there are times when you need to execute the same block of code multiple times. Instead of writing them all out individually and making our code really terrible process wise, we can use functions and function calls to call the same block of code over and over.</p>
<h3>Creating Functions</h3>
<p>You can create a function in 2 different places. The first is in the same script tags, but not inside the document ready function. Or second, you could create a brand new script file and then reference that script file in your header. Inside this script file you could create your method and it would be able to be accessed by the main file.</p>
<p>To create a function, the syntax looks like this&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> nameOfFunction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #006600; font-style: italic;">//your code here</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This tells the browser that there is a function called nameOfFunction that is not being passed any parameters. Passing parameters is something I&#8217;ll get into a little bit later. Now say you wanted to alert the user with a javascript pop-up box every time this function was run. You could add some code into the function to allow a dialog box to appear like so.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> nameOfFunction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;We are now alerting the user!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h3>Executing Functions</h3>
<p>Now that we have a function created, we need to somehow call that function to execute. That&#8217;s where our javascript method calls come in. Let&#8217;s say we wanted to execute the previous function. The code would look like this.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	nameOfFunction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Of course this would call the function immediately after the page was ready. You can use a function call in JQuery in any function. So if you wanted to add it inside of a .click function you could do that as well. That&#8217;s all you need to do to execute functions in JQuery.</p>
<h3>Passing Arguments to Functions</h3>
<p>Let&#8217;s say you wanted a function to work with a piece of data from a form, but you didn&#8217;t want the function to retrieve that piece of data inside the function. You can use a piece of code called an argument to pass to the function. This allows you to pass values to a function so they can be used. To create a function with an argument then all you need to do is add the argument into the brackets in the function header. It looks like this.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> nameOfFunction<span style="color: #009900;">&#40;</span>argument<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #006600; font-style: italic;">//your code here</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now obviously since we have that argument in the argument ready to do something with it, we may also want to return a value back to the original function so that we can maybe display the result of a calculation or something like that. If we want to return a value all we have to do is insert a return value into our function.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> nameOfFunction<span style="color: #009900;">&#40;</span>argument<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> total <span style="color: #339933;">=</span> <span style="color: #CC0000;">10</span> <span style="color: #339933;">+</span> argument<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">return</span> total<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This function will now accept a value, add 10 to that value and return it to the original function call. This way if we need to manipulate some data quite a few times, we could use a function to do that display the result.</p>
<h3>Function Calls with Arguments</h3>
<p>Now that we have a function made that&#8217;s accepting an argument and returning a value, we can use a function call and pass an argument to the function so it can do the calculation and we can then display the result. The function call looks something like this.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;The total is = &quot;</span> <span style="color: #339933;">+</span> nameOfFunction<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>In our JQuery ready function we are now just displaying an alert. Inside the alert we are actually making the function call and passing a value of 10 to our function. The function is then going to add 10 to our passed argument and return a value of 20. The alert therefore is going to display &#8211; &#8220;The total is = 20&#8243;. And that&#8217;s all for basic functions and function calls.</p>
]]></content:encoded>
			<wfw:commentRss>http://codeperspective.ca/2009/10/31/jquery-part-3-intro-to-functions/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>JQuery Part 2 &#8211; Basic Animation</title>
		<link>http://codeperspective.ca/2009/10/29/jquery-part-2-basic-animation/</link>
		<comments>http://codeperspective.ca/2009/10/29/jquery-part-2-basic-animation/#comments</comments>
		<pubDate>Fri, 30 Oct 2009 01:05:22 +0000</pubDate>
		<dc:creator>Code Perspective Admin</dc:creator>
				<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[JQuery]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://codeperspective.ca/?p=146</guid>
		<description><![CDATA[Welcome to JQuery part 2, using basic animation techniques. You should already have a basic understanding of JQuery and it's elements to complete this tutorial. If you don't, please refer to my JQuery Introduction post found <a href="http://codeperspective.ca/2009/10/28/introduction-to-jquery-part-1/">here</a>. Now to get started...]]></description>
			<content:encoded><![CDATA[<p>Welcome to JQuery part 2, using basic animation techniques. You should already have a basic understanding of JQuery and it&#8217;s elements to complete this tutorial. If you don&#8217;t, please refer to my JQuery Introduction post found <a href="http://codeperspective.ca/2009/10/28/introduction-to-jquery-part-1/">here</a>. Now to get started&#8230;</p>
<p>Let&#8217;s start by creating a basic HTML page and call it index.html in the root of our web servers directory and add a basic page structure to that file. Let&#8217;s also make sure that we have the JQuery libraries imported and ready to use.</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #00bbdd;">&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01//EN&quot; &quot;http://www.w3.org/TR/html4/strict.dtd&quot;&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">title</span>&gt;</span>JQuery Animations<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&gt;</span>
&nbsp;
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;lib/jquery.js&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>	
&nbsp;
	<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span></pre></div></div>

<p>Now, in order to complete animation we will need to display an element to animate. The easiest way to do this is to use some basic CSS. So for the purpose of this tutorial let&#8217;s create a style.css file and put it in the root of our directory. Let&#8217;s then link it to our index.html file so that we can use it.</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #00bbdd;">&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01//EN&quot; &quot;http://www.w3.org/TR/html4/strict.dtd&quot;&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">title</span>&gt;</span>JQuery Animations<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&gt;</span>
&nbsp;
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;lib/jquery.js&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
&nbsp;
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">link</span> <span style="color: #000066;">rel</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;stylesheet&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;style.css&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/css&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
&nbsp;
	<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span></pre></div></div>

<p>Now that we have our CSS file linked to our HTML we&#8217;re going to need to add an element to animate. For this demonstration we&#8217;re just going to use a red box that&#8217;s 100px by 100px inside a div. Let&#8217;s add that to our CSS file.</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;">#redBox
{
	width:100px;
	height:100px;
	background-color: red;
}</pre></div></div>

<p>Now we&#8217;ll add the appropriate div tags to our HTML file so that our red box shows up. We&#8217;re also going to add an anchor tag directly below our red box to toggle the box between showing and disappearing.</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #00bbdd;">&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01//EN&quot; &quot;http://www.w3.org/TR/html4/strict.dtd&quot;&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">title</span>&gt;</span>JQuery Animations<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&gt;</span>
&nbsp;
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;lib/jquery.js&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
&nbsp;
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">link</span> <span style="color: #000066;">rel</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;stylesheet&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;style.css&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/css&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;redBox&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#&quot;</span>&gt;</span>Show/Hide<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span></pre></div></div>

<p>Now we&#8217;re all ready to start coding our JQuery. If you run the HTML file right now you should see a red box sitting at the top left hand corner of the page. Right now the red box shouldn&#8217;t do anything as we haven&#8217;t coded the JQuery. So let&#8217;s start out by using the document ready function. and adding a function to all of our anchor tags.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script<span style="color: #339933;">&gt;</span>
	$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></div></div>

<p>Now we&#8217;re ready to be using some of the animation methods found in the JQuery library. First, since the box is by default showing up in the browser we&#8217;re going to make it hide by using the .hide() function. The .hide() function can be used without any parameters being passed or you can pass a speed and an optional callback(). The callback is a function to be executed when the animation has been completed. For the sake of this tutorial we&#8217;re only going to cover the speed parameter.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script<span style="color: #339933;">&gt;</span>
	$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
			$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#redBox'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">hide</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'slow'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></div></div>

<p>Now, if you include the script section in the header of your index.html document and run it, when you click the anchor tag you should see the red box that we created disappear. However now the red box is gone and we can&#8217;t get it back. We&#8217;re going to need to change this so that we can get it back by clicking on the anchor tag again. In order to get this we&#8217;re going to need to add another function and pass it as a parameter to our .click() function for our anchor tags.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script<span style="color: #339933;">&gt;</span>
	$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
			$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#redBox'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">hide</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'slow'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#redBox'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">show</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'slow'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></div></div>

<p>Since we&#8217;re adding the new function as another parameter to the click function, we need to terminate the current function before starting the new one. This is why we need to close the curly braces from the previous function before we start another. If we didn&#8217;t close the curly braces and just put it inside the first function then every time we clicked on the anchor tag, it would disappear and then show again without pressing the anchor tag. Now when we put that all together and run it, whenever we click the anchor tag the box should disappear and then re-appear when we click it again. Here is the complete code.</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">title</span>&gt;</span>JQuery Animations<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&gt;</span>
&nbsp;
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;lib/jquery.js&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
&nbsp;
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">link</span> <span style="color: #000066;">rel</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;stylesheet&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;style.css&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/css&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
&nbsp;
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
	$(document).ready(function(){
		$('a').click(function({
			$('#redBox').hide('slow');
		},function(){
			$('#redBox').show('slow');
		});
	});
	<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
&nbsp;
	<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;redBox&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#&quot;</span>&gt;</span>Show/Hide<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span></pre></div></div>

<p>In the tutorial I just used 2 of the more basic animation functions in the JQuery library. For a full reference to the JQuery animation API, please check out <a href="http://docs.jquery.com/Effects/">http://docs.jquery.com/Effects/</a>. Stay tuned for JQuery part 3 <img src='http://codeperspective.ca/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> .</p>
]]></content:encoded>
			<wfw:commentRss>http://codeperspective.ca/2009/10/29/jquery-part-2-basic-animation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Introduction to JQuery: Part 1</title>
		<link>http://codeperspective.ca/2009/10/28/introduction-to-jquery-part-1/</link>
		<comments>http://codeperspective.ca/2009/10/28/introduction-to-jquery-part-1/#comments</comments>
		<pubDate>Thu, 29 Oct 2009 00:51:02 +0000</pubDate>
		<dc:creator>Code Perspective Admin</dc:creator>
				<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[JQuery]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://codeperspective.ca/?p=136</guid>
		<description><![CDATA[JQuery is a javascript library that simplifies javascript&#8217;s interaction with HTML to speed up javascript coding.
Benefits of JQuery
	- Lightweight Footprint
	- CSS3 Compliant
	- Cross-Browser compatibility
What you will need
	- Javascript enabled browser
	- JQuery Library
	- An application to code in(Coda, Aptana, or even notepad)
Starting out
To start out coding with the JQuery library you will need to import the [...]]]></description>
			<content:encoded><![CDATA[<p>JQuery is a javascript library that simplifies javascript&#8217;s interaction with HTML to speed up javascript coding.</p>
<h3>Benefits of JQuery</h3>
<p>	- Lightweight Footprint<br />
	- CSS3 Compliant<br />
	- Cross-Browser compatibility</p>
<h3>What you will need</h3>
<p>	- Javascript enabled browser<br />
	- <a href="http://jquery.com/">JQuery Library</a><br />
	- An application to code in(Coda, Aptana, or even notepad)</p>
<h3>Starting out</h3>
<p>To start out coding with the JQuery library you will need to import the library. Usually I like to structure my site with a /lib folder at the root of the directory. This way I can include all of my javascript libraries in one central location so they don&#8217;t replicate down the road. So for this project i&#8217;ll setup my directory structure with my JQuery files inside the lib folder. Next we&#8217;ll create an index.html file inside the root directory. This will be our testing file for our JQuery code. Alright, let&#8217;s get started, let&#8217;s start by adding a basic page structure to your index.html file.</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #00bbdd;">&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01//EN&quot; &quot;http://www.w3.org/TR/html4/strict.dtd&quot;&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">title</span>&gt;</span>JQuery Test<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span></pre></div></div>

<p>Next we need to add in our JQuery script so that we can use it. Add in the following code inside your header tags. Use the src tag to point to your JQuery javascript file.</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;lib/jquery.js&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span></pre></div></div>

<h3>Making Sure the document is ready</h3>
<p>First off, any JQuery code that we want to include goes into script tags. This is because JQuery runs on top of javascript, so in order for the browser to understand what JQuery is doing, it needs to be included inside the script tags.</p>
<p>Now, the first thing that we need to do is make sure that the document is ready to be used. We do this to make sure that no changes occur before the user is ready to see it. So if the page isn&#8217;t loaded or the user is on a slow connection, the JQuery code will not start executing until the document is loaded and ready. To do this we use the document ready function in JQuery.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script<span style="color: #339933;">&gt;</span>
$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></div></div>

<h3>Adding a click event to a link</h3>
<p>Now that our document is ready for our JQuery code we can add some functionality to our site. The first thing we&#8217;ll deal with is adding a link to our index.html file. That way we have something to manipulate when we go to code our JQuery. Let&#8217;s add a link to our index.html file now.</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #00bbdd;">&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01//EN&quot; &quot;http://www.w3.org/TR/html4/strict.dtd&quot;&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">title</span>&gt;</span>JQuery Test<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&gt;</span>
&nbsp;
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;lib/jquery.js&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
&nbsp;
	<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
&nbsp;
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://www.google.ca/&quot;</span>&gt;</span>Link<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span>
&nbsp;
	<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span></pre></div></div>

<p>Once we have our link added we can start to code the JQuery. What we&#8217;re going to do in this instance is instead of taking the user to google, we&#8217;re going to just display a simple javascript alert instead.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script<span style="color: #339933;">&gt;</span>
	$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Obviously you're still here and not over at google&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			event.<span style="color: #660066;">preventDefault</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></div></div>

<p>Alright, so that probably looks a little confusing right off the bat, especially if you&#8217;re used to regular javascript coding. Let&#8217;s break it down into a little more detail. To start off, $(&#8217;a'). This means that we&#8217;re taking the element &#8220;a&#8221; from the html. If we wanted to use the id of say a form(id=&#8221;test&#8221;) then we could just do $(&#8217;#test&#8217;) instead. Then after we have a function. This will allow us to do something with the element that we retrieved from the HTML. In this case, we&#8217;re going to use the &#8220;.click()&#8221; function to allow us to process an event when any element with an attribute of &#8220;a&#8221; is clicked. Now because we want to do something with that event whenever it&#8217;s clicked, we can use a function inside of the click function. That&#8217;s where the .click(function(){}) section comes from. Now in order to actually accomplish something with the event that was created we need to put the rest of our code inside of the curly braces of the function. In this case we&#8217;re just creating a plain javascript alert box with a message in it. The next line, &#8220;event.preventDefault();&#8221; is just a function in JQuery that disables the default behavior of the element. In this case the default behavior is to send the user to google, so we&#8217;re going to make the user stay at our page and just simply display a javascript alert.</p>
<p>Since .click is a function we will need to close our the function using a &#8220;;&#8221;. That&#8217;s it, you just wrote your first function and created your first event. All together the code looks like this.</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #00bbdd;">&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01//EN&quot; &quot;http://www.w3.org/TR/html4/strict.dtd&quot;&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">title</span>&gt;</span>JQuery Test<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&gt;</span>
&nbsp;
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;lib/jquery.js&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>			
&nbsp;
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
			$(document).ready(function(){
				$('a').click(function({
					alert(&quot;Obviously you're still here and not over at google&quot;);
					event.preventDefault();
				));
			});
		<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
&nbsp;
	<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
&nbsp;
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://www.google.ca/&quot;</span>&gt;</span>Link<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span>
&nbsp;
	<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span></pre></div></div>

<p>For more information on JQuery or more tutorials you can visit the jquery site at <a href="http://jquery.com">http://jquery.com/</a>. If you have any questions or comments feel free to leave a comment and I&#8217;ll respond as quickly as possible.</p>
<p>Cheers</p>
]]></content:encoded>
			<wfw:commentRss>http://codeperspective.ca/2009/10/28/introduction-to-jquery-part-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Installing Wordpress using Coda</title>
		<link>http://codeperspective.ca/2009/07/27/installing-wordpress-using-coda/</link>
		<comments>http://codeperspective.ca/2009/07/27/installing-wordpress-using-coda/#comments</comments>
		<pubDate>Mon, 27 Jul 2009 21:12:17 +0000</pubDate>
		<dc:creator>Code Perspective Admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[coda]]></category>
		<category><![CDATA[Mac OS X]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[WORDPRESS]]></category>

		<guid isPermaLink="false">http://codeperspective.ca/blog/?p=93</guid>
		<description><![CDATA[This being my first site I&#8217;ve set up from top to bottom I wasn&#8217;t able to find an install guide for Wordpress using the coda IDE for Mac OS X, so i figured I would write one. To follow this tutorial you will need a full/trial version of coda, the latest Wordpress CMS release, a [...]]]></description>
			<content:encoded><![CDATA[<p>This being my first site I&#8217;ve set up from top to bottom I wasn&#8217;t able to find an install guide for Wordpress using the coda IDE for Mac OS X, so i figured I would write one. To follow this tutorial you will need a full/trial version of coda, the latest Wordpress CMS release, a web server up and running(remote or local). If you&#8217;re running a local web server make sure it has all the requirements installed to use Wordpress. That&#8217;s it, let&#8217;s start it up&#8230;</p>
<p>1. The first step is to install Wordpress, if this isn&#8217;t done then go ahead and install Wordpress now(a tutorial will be coming on how to do this a bit later <img src='http://codeperspective.ca/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> ).<br />
2. With coda, create a new site&#8230; To do this click on the site&#8217;s icon in the menu bar and click &#8220;Add Site&#8221;).</p>
<div id="attachment_94" class="wp-caption alignnone" style="width: 548px"><a href="http://codeperspective.ca/wp-content/uploads/2009/07/sites-pic.png" rel=”lightbox”><img class="size-full wp-image-94" title="site's pic" src="http://codeperspective.ca/wp-content/uploads/2009/07/sites-pic.png" alt="Click on the site's icon then the add site button" width="538" height="79" /></a><p class="wp-caption-text">Click on the site&#39;s icon then the add site button</p></div>
<p>3. Once you have done that and filled out all the information that coda requires to set up your site within the site&#8217;s area, you should now see a preview of your site within the site&#8217;s area of coda.<br />
4. Double click on your site and it should connect to the web server and display your web server&#8217;s file system. It should look something like the picture below(may not be exact depending on your web server&#8217;s specifications).<br />
<a href="http://codeperspective.ca/wp-content/uploads/2009/07/Picture-5.png" rel=”lightbox”><img class="alignnone size-full wp-image-96" title="Coda Web Server View" src="http://codeperspective.ca/wp-content/uploads/2009/07/Picture-5.png" alt="Coda Web Server View" width="234" height="527" /></a><br />
5. Once you can view your site then you should see the root folder /www/ in your server&#8217;s root directory. This is where you will be installing Wordpress. Drag the Wordpress folder you downloaded into the /www/ folder. With coda you can directly drag the folder over from your desktop. Once that&#8217;s finished you should see a folder in the root of your directory called &#8220;Wordpress&#8221; or whatever the name of your Wordpress folder was.<br />
6. Rename the Wordpress folder to &#8220;blog&#8221; or whatever you want the Wordpress CMS to be a part of.<br />
<a href="http://codeperspective.ca/wp-content/uploads/2009/07/rename.png" rel=”lightbox”><img class="alignnone size-full wp-image-98" title="rename Wordpress folder" src="http://codeperspective.ca/wp-content/uploads/2009/07/rename.png" alt="rename Wordpress folder" width="296" height="465" /></a><br />
7. Once you have it renamed you should see a bunch of php/css files inside your now renamed Wordpress folder.<br />
8. Wordpress should now be installed for your site. You can access it by going to http://www.your-url.com/blog/. And if you want to make changed as the administrator then access the admin panel by going to your-url/blog/wp-admin/.</p>
<p>That&#8217;s it, you can now take advantage of the power of the Wordpress CMS. In a later tutorial i&#8217;ll explain how to install and use the JQuery framework within your Wordpress theme.</p>
<p>Cheers</p>
]]></content:encoded>
			<wfw:commentRss>http://codeperspective.ca/2009/07/27/installing-wordpress-using-coda/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Creating an Application Specific Console in QT Jambi</title>
		<link>http://codeperspective.ca/2009/03/16/creating-an-application-specific-console-in-qt-jambi/</link>
		<comments>http://codeperspective.ca/2009/03/16/creating-an-application-specific-console-in-qt-jambi/#comments</comments>
		<pubDate>Tue, 17 Mar 2009 04:22:25 +0000</pubDate>
		<dc:creator>Code Perspective Admin</dc:creator>
				<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[GUI]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[QT]]></category>
		<category><![CDATA[Software]]></category>

		<guid isPermaLink="false">http://codeperspective.ca/blog/?p=30</guid>
		<description><![CDATA[I am currently working on a project that required a console with a very specific purpose. It needed to communicate with a custom back-end protocol for use in a client/server environment. This sort of specific tutorial is not surprisingly unavailable online. The first step when setting up an application specific console is actually setting up [...]]]></description>
			<content:encoded><![CDATA[<p>I am currently working on a project that required a console with a very specific purpose. It needed to communicate with a custom back-end protocol for use in a client/server environment. This sort of specific tutorial is not surprisingly unavailable online. The first step when setting up an application specific console is actually setting up the back-end. This is something that will be potentially done as a future tutorial. Anyways back to building the console window. The components included in the console window will be one QTextEdit, and one QLineEdit. This can be constructed in any way you like. The QTextEdit can be on the bottom and the QLineEdit on top or the other way around, that&#8217;s up to you. </p>
<p>Next, we will need to be adding a custom action listener to the QLineEdit so that when the enter key is pressed inside the QLineEdit the command will be sent to the server and then displayed in the QTextEdit window. As a future feature implementation, syntax highlighting could be implemented for an easier display inside the QTextEdit window. The custom action listener to add to the QLineEdit bar looks like this&#8230;</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">QLineEdit.<span style="color: #006633;">returnPressed</span>.<span style="color: #006633;">connect</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>, <span style="color: #0000ff;">&quot;NameOfActionListenerMethod()&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>This will ensure that everytime the enter button is pressed, the string that the user entered will be able to be transfered to the server, or wherever else in the program you would like it to go. Now for the action listener method. The method should be declared as public with a return type of void.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> terminalWindowEnterPressed<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        terminalWindow.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>enterArea.<span style="color: #006633;">text</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        enterArea.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Protocol Name &gt; &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Then once you have the action listener built to just send the data to the window, you can do the same with sending it across a network.</p>
<p>All Together the code looks like this. All you have to do is copy/paste the code into your program and use it at will.</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
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #008000; font-style: italic; font-weight: bold;">/**
     *
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> buildTerminal<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        terminalFrame <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> QFrame<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        terminalFrame.<span style="color: #006633;">setWindowTitle</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Window Title&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        terminalWindow <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> QTextEdit<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        terminalWindow.<span style="color: #006633;">setReadOnly</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        enterArea <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> QLineEdit<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        enterArea.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Protocol Name &gt; &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        enterLabel <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> QLabel<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Enter Command&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        terminalFrame.<span style="color: #006633;">setMinimumSize</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">600</span>, <span style="color: #cc66cc;">400</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        enterArea.<span style="color: #006633;">returnPressed</span>.<span style="color: #006633;">connect</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>, <span style="color: #0000ff;">&quot;terminalWindowEnterPressed()&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        QGridLayout layout <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> QGridLayout<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        layout.<span style="color: #006633;">addWidget</span><span style="color: #009900;">&#40;</span>terminalWindow,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">10</span>,<span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        layout.<span style="color: #006633;">addWidget</span><span style="color: #009900;">&#40;</span>enterArea,<span style="color: #cc66cc;">11</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">9</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        layout.<span style="color: #006633;">addWidget</span><span style="color: #009900;">&#40;</span>enterLabel, <span style="color: #cc66cc;">11</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        terminalFrame.<span style="color: #006633;">setLayout</span><span style="color: #009900;">&#40;</span>layout<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        terminalFrame.<span style="color: #006633;">show</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     *
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> terminalWindowEnterPressed<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        terminalWindow.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>enterArea.<span style="color: #006633;">text</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        enterArea.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Protocol Name &gt; &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Cheers</p>
<p><!--digg--></p>
]]></content:encoded>
			<wfw:commentRss>http://codeperspective.ca/2009/03/16/creating-an-application-specific-console-in-qt-jambi/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
