<?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>Jesper Christiansen &#187; Development</title>
	<atom:link href="http://www.jesperchristiansen.dk/category/development/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jesperchristiansen.dk</link>
	<description>Thoughts about the internet and usability</description>
	<lastBuildDate>Sun, 10 Jan 2010 12:28:15 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Update RubyGems on Ubuntu 9.04 (Jaunty)</title>
		<link>http://www.jesperchristiansen.dk/2010/01/update-rubygems-on-ubuntu-9-04-jaunty/</link>
		<comments>http://www.jesperchristiansen.dk/2010/01/update-rubygems-on-ubuntu-9-04-jaunty/#comments</comments>
		<pubDate>Sun, 10 Jan 2010 12:28:15 +0000</pubDate>
		<dc:creator>Jesper Christiansen</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://www.jesperchristiansen.dk/?p=90</guid>
		<description><![CDATA[Happy new year everyone! It has been a bit silent from my side lately &#8211; due to being extremely busy with new job and so on. Yesterday I took the freedom to setup a new Linode on their recently added London datacenters. I&#8217;m from Europe myself so I thought that using a EU host would [...]]]></description>
			<content:encoded><![CDATA[<p>Happy new year everyone! It has been a bit silent from my side lately &#8211; due to being extremely busy with new job and so on. Yesterday I took the freedom to setup a new <a title="Linode Hosting" href="http://www.linode.com" target="_self">Linode</a> on their recently added London datacenters. I&#8217;m from Europe myself so I thought that using a EU host would probably give me a lot better performance. I was right.</p>
<p>I then decided to check out Ubuntu 9.04 Jaunty. But ran in to a problem when I wanted to update the RubyGems installation. But after playing around a little I found the following solution:</p>
<pre>gem install rubygems-update
cd /var/lib/gems/1.8/bin
./update_rubygems

That solved the problem :)</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.jesperchristiansen.dk/2010/01/update-rubygems-on-ubuntu-9-04-jaunty/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>18 Impressive Web Form Validation Scripts</title>
		<link>http://www.jesperchristiansen.dk/2009/11/18-impressive-web-form-validation-scripts/</link>
		<comments>http://www.jesperchristiansen.dk/2009/11/18-impressive-web-form-validation-scripts/#comments</comments>
		<pubDate>Wed, 18 Nov 2009 13:43:42 +0000</pubDate>
		<dc:creator>Jesper Christiansen</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[user experience]]></category>
		<category><![CDATA[validation]]></category>

		<guid isPermaLink="false">http://www.jesperchristiansen.dk/?p=88</guid>
		<description><![CDATA[This post is related to my jQuery tutorial about simple form validation I wrote the other day. dzineblog has a post about 18 Impressive Web Form Validation Scripts to Enhance User Experience. I thought that that would be worth mentioning, since I can see I get a lot of hits on form validation these days.
]]></description>
			<content:encoded><![CDATA[<p>This post is related to my jQuery tutorial about simple form validation I wrote the other day. <a title="dzineblog" href="http://dzineblog.com" target="_blank">dzineblog </a>has a post about <a title="18 Impreese Web Form Validation Scripts" href="http://dzineblog.com/2009/11/18-impressive-web-form-validation-scripts-to-enhance-user-experience.html" target="_blank">18 Impressive Web Form Validation Scripts to Enhance User Experience</a>. I thought that that would be worth mentioning, since I can see I get a lot of hits on form validation these days.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jesperchristiansen.dk/2009/11/18-impressive-web-form-validation-scripts/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>jQuery: Clientside Form validation</title>
		<link>http://www.jesperchristiansen.dk/2009/11/jquery-clientside-form-validation/</link>
		<comments>http://www.jesperchristiansen.dk/2009/11/jquery-clientside-form-validation/#comments</comments>
		<pubDate>Tue, 10 Nov 2009 23:28:45 +0000</pubDate>
		<dc:creator>Jesper Christiansen</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Usability]]></category>

		<guid isPermaLink="false">http://www.jesperchristiansen.dk/?p=71</guid>
		<description><![CDATA[This is the first tutorial of many, which covers jQuery. My favorite javascript library. In this post we will cover how to create a simple form client-side validation script.
First we will create a simple form with two input fields, name and email. I will give it a little bit of styling to make it look [...]]]></description>
			<content:encoded><![CDATA[<p>This is the first tutorial of many, which covers <a title="jQuery javascript library" href="http://www.jquery.com" target="_blank">jQuery</a>. My favorite javascript library. In this post we will cover how to create a simple form client-side validation script.</p>
<p><span id="more-71"></span>First we will create a simple form with two input fields, name and email. I will give it a little bit of styling to make it look more attractive than just a plain white form, but that is outside the scope of this tutorial (and actually it isn&#8217;t that attractive at all &#8211; don&#8217;t expect girls standing in line at least).</p>
<pre class="brush: html">

&lt;div id=&quot;container&quot;&gt;
&lt;h1&gt;jQuery Form Validation&lt;/h1&gt;
&lt;form action=&quot;form&quot;&gt;

&lt;p&gt;
&lt;label for=&quot;name&quot;&gt;Name&lt;/label&gt;&lt;input type=&quot;text&quot; name=&quot;name&quot; value=&quot;&quot; id=&quot;name&quot;/&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;label for=&quot;email&quot;&gt;Email&lt;/label&gt;&lt;input type=&quot;text&quot; name=&quot;email&quot; value=&quot;&quot; id=&quot;email&quot;/&gt;
&lt;/p&gt;

&lt;p&gt;&lt;input type=&quot;submit&quot; value=&quot;Submit form&quot; id=&quot;submit&quot;&gt;&lt;/p&gt;
&lt;/form&gt;
&lt;/div&gt;
</pre>
<p>This gives the following form</p>
<div id="attachment_80" class="wp-caption aligncenter" style="width: 463px"><img class="size-full wp-image-80" title="jquery-form-validate-1" src="http://www.jesperchristiansen.dk/wp-content/uploads/2009/11/jquery-form-validate-12.PNG" alt="jquery-form-validate-1" width="453" height="273" /><p class="wp-caption-text">The form used for validation</p></div>
<p>To be able to make the powers of jQuery come alive, we have to include it in our page. We take advantage of Google&#8217;s servers and include it from theirs</p>
<pre class="brush: html">
&lt;script type=&quot;text/javascript&quot; src=&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js&quot;&gt;&lt;/script&gt;
</pre>
<p>Now it&#8217;s time for the fun part! Let&#8217;s write that javascript that handles the validation. We can chose to include this from a separate file or simply add it directly in the page. For simplicity and since this is a small tutorial, I will just add it in a script tag on the page.</p>
<p>Let us add a function named form_validate() as the first thing. This is the function that has our validate logic. I like to name my functions with the element name first, so that it is easy for me to see where the specific function is used, instead of me guessing. This especially comes handy in large javascript files. But this is totally up to you :)</p>
<pre class="brush: javascript">

function form_validate() {

}
</pre>
<p>To get this function running at all, we need to include it in the $(document).ready(function(){ } which is run once the page is done loading. So lets create that as well in our script block in our page.</p>
<pre class="brush: javascript">

$(document).ready(function(){

form_validate();

});
</pre>
<p>To make sure this is working and getting run as intended, lets add a alert() to our form_validate()</p>
<pre class="brush: javascript">

function form_validate() {

alert(&quot;Yup! It&#039;s working&quot;);

}

$(document).ready(function(){

form_validate();

});
</pre>
<p>Once we confirm that that is working perfect. Let&#8217;s continue and add something to our form_validate function.</p>
<p>First we will bind an event to our submit-button. This is done by the following piece of javascript in our form_validate() function.</p>
<pre class="brush: javascript">

$(&quot;#submit&quot;).bind(&quot;click&quot;, function() { alert(&quot;Test&quot;); return false; }
</pre>
<p>Again we add in an alert() to make sure that this works. If it for some reason doesn&#8217;t work, then make sure that you have added an id attribute to the submit-button (the # in front of submit, denotes that it looks after an id value on the page, like a css-selector) and that you have added the form_validate() function to the document-ready-function. The return false prevents the form from submitting and then reloading the page.</p>
<p>Lets delete that content for now and add something else.</p>
<pre class="brush: javascript">

$(&quot;#submit&quot;).bind(&quot;click&quot;, function() {

// Will get all inputs in form (3 inputs)
var allInputs = $(&quot;:input&quot;);

}
</pre>
<p>That will find all inputs on our page. Since this is a simple example, it is ok to write it like this. If we had a bigger page, we would probably like to stay within our form and we would then add an id attribute to the form itself and then use that together with the :input in the selector. Like this #formid:input. But now we just stick with :input for the sake of simplicity.</p>
<p>We then extend it further with the following line</p>
<pre class="brush: javascript">

$(&quot;#submit&quot;).bind(&quot;click&quot;, function() {

// Will get all inputs in form (3 inputs)
var allInputs = $(&quot;:input&quot;);

$(allInputs).each(function(i){

});

}
</pre>
<p>This loops through all of the inputs on the page. The next step is to add some more logic to check if the value in the inputs meet our criteria</p>
<pre class="brush: javascript">

$(&quot;#submit&quot;).bind(&quot;click&quot;, function() {

// Will get all inputs in form (3 inputs)
var allInputs = $(&quot;:input&quot;);

$(allInputs).each(function(i){

var value = $(this).attr(&quot;value&quot;);
var id = $(this).attr(&quot;id&quot;);
var element = $(this);

if(id == &quot;email&quot;)
{
if(!value.match(/^\w[\w|\.|\-]+@\w[\w|\.|\-]+\.[a-zA-Z]{2,4}$/))
{
element.attr(&quot;class&quot;,&quot;&quot;).addClass(&quot;invalid&quot;);
}
else
{
element.attr(&quot;class&quot;,&quot;&quot;).addClass(&quot;valid&quot;);
}
}

});

}
</pre>
<p>The above might look a bit complicated but it&#8217;s actually pretty straightforward once you get the idea.</p>
<p>We loop through all of the inputs. For each input in the form, we save the value in the value variable, id in the id variable and so on. This is simply done not to clutter up the javascript. We then do a check if the id equals email (if we are looking at the email form-field). If that is the case, we match the value against a simple email-validation regular expression and if it doesnt match, we add an &#8220;invalid&#8221;-class to our email input-field and if it matches we add a &#8220;valid&#8221;-class. This will show the user which fields fails and wich are ok. So let&#8217;s add the styling to the page:</p>
<pre class="brush: css">

&lt;style type=&quot;text/css&quot; media=&quot;screen&quot;&gt;
.invalid {
border: 2px solid red;
}

.valid {
border: 2px solid green;
}
&lt;/style&gt;
</pre>
<p>This will add a red border around the input field if it didn&#8217;t validate and a green if it did.</p>
<p>Let&#8217;s add the same functionality for the name field.</p>
<pre class="brush: javascript">

if(id == &quot;name&quot;)
{
if(value == &quot;&quot;)
{
element.attr(&quot;class&quot;,&quot;&quot;).addClass(&quot;invalid&quot;);
}
else
{
element.attr(&quot;class&quot;,&quot;&quot;).addClass(&quot;valid&quot;);
}
}
</pre>
<p>This gives the following result on submit</p>
<div id="attachment_84" class="wp-caption aligncenter" style="width: 546px"><img class="size-full wp-image-84" title="jquery-form-validate-2" src="http://www.jesperchristiansen.dk/wp-content/uploads/2009/11/jquery-form-validate-2.PNG" alt="It gives an error because the email isn't correct" width="536" height="294" /><p class="wp-caption-text">It gives an error because the email isn&#39;t correct</p></div>
<p>We are almost done. The only problem is, that the form won&#8217;t submit when the data is correct, cause it returns false. Let&#8217;s add a variable to the function</p>
<pre class="brush: javascript">

var error = false;
</pre>
<p>And let&#8217;s set that to true if there is an error. Both in the email and name conditions.</p>
<pre class="brush: javascript">

if(id == &quot;name&quot;)
{
if(value == &quot;&quot;)
{
error = true;
element.attr(&quot;class&quot;,&quot;&quot;).addClass(&quot;invalid&quot;);
}
else
{
error = false;
element.attr(&quot;class&quot;,&quot;&quot;).addClass(&quot;valid&quot;);
}
}
</pre>
<p>We will then add a if-statement at the bottom, to check if the variable is set.</p>
<pre class="brush: javascript">

if(!error)
{
return true;
}
else
{
alert(&quot;Form validation failed! Please correct the errors marked in red&quot;);
}
</pre>
<p>At the same time we show an alert when the form validation failed, so that the user know what those red lines around the input fields mean.</p>
<p>That&#8217;s it :)</p>
<p>The source can be found <a href="/tutorial/jquery/form-validation.html" target="_blank">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jesperchristiansen.dk/2009/11/jquery-clientside-form-validation/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>PHP: Send mail the simple way</title>
		<link>http://www.jesperchristiansen.dk/2009/10/php-send-mail-pa-den-simple-made/</link>
		<comments>http://www.jesperchristiansen.dk/2009/10/php-send-mail-pa-den-simple-made/#comments</comments>
		<pubDate>Sat, 31 Oct 2009 21:22:35 +0000</pubDate>
		<dc:creator>Jesper Christiansen</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[beginner]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[mail]]></category>

		<guid isPermaLink="false">http://www.jesperchristiansen.dk/?p=50</guid>
		<description><![CDATA[Simple introduction to how to send emails using PHP]]></description>
			<content:encoded><![CDATA[<p>One of the fun things of doing web programming, is when you make something that isn&#8217;t just visual appealing &#8211; but also gives you responds and integrate with other things. In this post I will give a tiny very simple example on how to send a basic text email using PHP.</p>
<p><span id="more-50"></span>To send a mail using PHP you can use less than 10 lines of code. Create a php file containing the following code.</p>
<pre class="brush: php">

&lt;?php
$recipient = &quot;tutorial@jesperchristiansen.dk&quot;;
$subject = &quot;This is sent from the tutorial&quot;;
$body = &quot;Hi,\n\nIs the tutorial any helpful?&quot;;

if (mail($recipient, $subject, $body))
{
echo(&quot;&lt;p&gt;We can&#039;t stop smiling! The email was sent successfully! :)&lt;/p&gt;&quot;);
}
else
{
echo(&quot;&lt;p&gt;My eyes are in tears, something wen&#039;t wrong :/&lt;/p&gt;&quot;);
}
?&gt;
</pre>
<p>The above example is pretty straight forward. Vi declare a couple of variables, which is done so that we don&#8217;t clutter the mail() function with massive strings of text. The variables consist of recipient, subject and body. Vi use these as arguments in the mail()-function, which returns true if the mail is sent successfully from the PHP code and false if everything explodes and it goes wrong. That is why I have encapsulated it in an if, to make a check whether it went good or not &#8211; and display that information to the user.</p>
<p>Simple :)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jesperchristiansen.dk/2009/10/php-send-mail-pa-den-simple-made/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP: Data Access Objects part 1</title>
		<link>http://www.jesperchristiansen.dk/2009/10/php-data-access-objects-part-1/</link>
		<comments>http://www.jesperchristiansen.dk/2009/10/php-data-access-objects-part-1/#comments</comments>
		<pubDate>Wed, 28 Oct 2009 19:00:17 +0000</pubDate>
		<dc:creator>Jesper Christiansen</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.jesperchristiansen.dk/?p=5</guid>
		<description><![CDATA[In this post I will cover the use of Data Access Objects (DAO) in PHP. The primary goal by doing so is greater abstraction by seperating the layers as well as covering some of the new techniques of PHP5. It will also come as a great help for you who create massive database web-applications which [...]]]></description>
			<content:encoded><![CDATA[<p>In this post I will cover the use of Data Access Objects (DAO) in PHP. The primary goal by doing so is greater abstraction by seperating the layers as well as covering some of the new techniques of PHP5. It will also come as a great help for you who create massive database web-applications which often result in a lot of duplicate code.</p>
<p><span id="more-5"></span></p>
<h1>Introduction</h1>
<p>Many years ago when I started playing around with PHP programming I discovered the joy of saving and retrieving data from a database. From that worldbreaking discovery it didn&#8217;t take long before I started doing smaller web-applications which consisted of many different pages and parts &#8211; which all (almost) fetched data in the database. This resulted in a lot of copy-and-paste code, and the same create-connection code to be written again and again and again&#8230; and again!</p>
<p>Let&#8217;s see if we can do something clever and avoid the above problem and make our PHP code more object-oriented than database oriented and at the same time separate logic and HTML.</p>
<h2>Example without the use of DAO&#8217;s</h2>
<p>Let&#8217;s create a simple users table, which most of us have probably tried before.</p>
<pre class="brush: php">

CREATE TABLE `users` (
`user_id` int(11) NOT NULL auto_increment,
`username` varchar(20) NOT NULL,
`name` varchar(20) NOT NULL,
`email` varchar(100) NOT NULL,
PRIMARY KEY (`user_id`),
UNIQUE KEY `user_id` (`user_id`),
KEY `user_id_2` (`user_id`)
)
</pre>
<p>Normalle we will write code as seen below to connect to the database, query it and display the result.</p>
<pre class="brush: php">
&lt;?php

$db_username = &quot;dbtest&quot;;
$db_password = &quot;dbpassword&quot;;
$db_hostname = &quot;localhost&quot;;
$db_name = &quot;database&quot;;
$query = &quot;SELECT * FROM users&quot;

$connection = @mysql_connect($db_hostname, $db_username,$db_password) or die(&quot;Cannot connect to database&quot;);

if (! mysql_selectdb($db_name, $connection)) die(&quot;Cannot open table&quot;);

if (!($result = @ mysql_query($strsql, $connection))) die(&quot;Cannot select record&quot;);

echo &quot;&lt;h1&gt;Alle users&lt;/h1&gt;&quot;;

while($row = mysql_fetch_array($result)){
$row = mysql_fetch_array($result);
echo &quot;UserId: &quot;.$row[&#039;user_id&#039;].&quot;&lt;br/&gt;&quot;;
echo &quot;Username: &quot;.$row[&#039;username&#039;].&quot;&lt;br/&gt;&quot;;
echo &quot;Name: &quot;.$row[&#039;name&#039;].&quot;&lt;br/&gt;&quot;;
echo &quot;Email: &quot;.$row[&#039;email&#039;].&quot;&lt;br/&gt;&quot;;
}
?&gt;
</pre>
<p>It is not that the above example is horrible in any way. I just think that we can do it smarter and more simple. Especially if we have an application where you have to fetch data from the database on many different pages. It can easily get redundant and cluttered, especially because PHP and HTML tend to be mixed if you dont watch out when you make web applications. Of course this can&#8217;t be avoided totally &#8211; but we can atleast do something, so that the PHP in the HTML will only be view-related.</p>
<p>Let&#8217;s take a closer look at DAO&#8217;s.</p>
<h2>Data Access Objects</h2>
<p>First we will make 2 classes, which will handle the database connection and some of the database operations. The first one we give the name class-database.php with the following content (of course you have to replace some of the values so that it matches your host, username, password etc)</p>
<pre class="brush: php">
&lt;?php
class Database {
private $host = &quot;databasehost&quot;;
private $username = &quot;databaseusername&quot;;
private $pwd = &quot;databaseadgangskode&quot;;
private $db = &quot;databasenavn&quot;;

public function GetDBConn()
{
return mysqli_connect($this-&gt;host, $this-&gt;username, $this-&gt;pwd, $this-&gt;db);
}
}
?&gt;
</pre>
<p>This class will be used to get the database connection from the other objects we create.</p>
<p>Now it is time to create a class with a bit more functionality, the class-resultset.php.</p>
<pre class="brush: php">

&lt;?php

class ReadOnlyResultSet {

private $rs;

function __construct($rs)
{
$this-&gt;rs = $rs;
}

// Get the next object
function getNext($dataobject)
{
$row = mysqli_fetch_array($this-&gt;rs);
// Get the properties of the object
$class = new ReflectionObject($dataobject);

$properties = $class-&gt;getProperties();

// Loop through the properties
for ($i = 0; $i &lt; count($properties); $i++) {
$prop_name = $properties[$i]-&gt;getName();
$dataobject-&gt;$prop_name = $row[$prop_name];
}

return $dataobject;

}

// Reset the pointer back to the beginning

function reset()
{
mysqli_data_seek($this-&gt;rs, 0);
}

function rowCount()
{
return mysqli_num_rows($this-&gt;rs);
}

}

?&gt;
</pre>
<p>Now we have created the class that has the functions we will use on the queried resultsets.</p>
<p>The next thing on the list, is to create the DAO&#8217;s themself. This is an object which looks like the table in the database. This means an object with the same properties as the fields you want to fetch/save from the database.</p>
<p>Den klasse vi lige har oprettet, indeholder funktioner, som bruges på de recordsets vi hiver ud.</p>
<p>Lets create one that matches our newly created users table.</p>
<pre class="brush: php">

&lt;?php
require_once &#039;class-database.php&#039;;

class User extends Database {
public $user_id;
public $username;
public $name;
public $email;

public function insert()
{
$dbc = $this-&gt;getDBConn();
$sql = &quot;INSERT INTO users (name, username, email)VALUES(&#039;$this-&gt;name&#039;, &#039;$this-&gt;username&#039;, &#039;$this-&gt;email&#039;)&quot;;
$rs = mysqli_query($dbc,$sql);
$this-&gt;user_id = mysqli_insert_id($dbc);
}
}
?&gt;
</pre>
<p>We have created public variables matching the data in the database. As well as an insert() function. We will use this when we want to save a user in the database. Basically it is just an INSERT-query, which takes the values from our user-object. We extends our user object with the Database-class, so we can access the database-connection by calling the getDBConn()-function. After a succesful insertion of the new user, the primary-key is saved in the user_id variable.</p>
<p>Below is an example of how to create a new user in the database.</p>
<pre class="brush: php">

$user = new User();
$user-&gt;username = &#039;jesper&#039;;
$user-&gt;name = &#039;Jesper Christiansen&#039;;
$user-&gt;email = &#039;mig@jesperchristiansen.dk&#039;
$user-&gt;insert();
</pre>
<p>Easy! :)</p>
<p>Let&#8217;s create a new method so that we can also update an existing user in the database.</p>
<pre class="brush: php">

public function update()
{

$dbc = $this-&gt;getDBConn();

$sql = &quot;UPDATE users SET name = &#039;$this-&gt;name&#039;, username = &#039;$this-&gt;username&#039;, email = &#039;$this-&gt;email&#039; WHERE user_id = $this-&gt;user_id&quot;;

$rs = mysqli_query($dbc,$sql);

}
</pre>
<p>The update function isn&#8217;t any fun if we have to write all of the values that are already in the database ourselves and then update these. So let&#8217;s create a function to get an existing user.</p>
<pre class="brush: php">

public function find()

{

$sql = &quot;SELECT * FROM users&quot;;

$where = array();

$class = new ReflectionClass(&#039;user&#039;);

$properties = $class-&gt;getProperties();

for ($i = 0; $i &lt; count($properties); $i++) {
$name = $properties[$i]-&gt;getName();

if ($this-&gt;$name != &#039;&#039;) {
$where[] = &quot;`&quot; . $name . &quot;`=&#039;&quot;. mysql_escape_string($this-&gt;$name) . &quot;&#039;&quot;;

}

}

// If we have a where clause, then create it

if (count($where) &gt; 0){
$sql .= &quot; WHERE &quot; . implode(&#039; AND &#039;, $where);
}

$dbc = $this-&gt;getDBConn();

$rs = mysqli_query($dbc,$sql);

include_once(&#039;class-resultset.php&#039;);

return new ReadOnlyResultSet($rs);

}
</pre>
<p>We simply make a SELECT-statement on behalf of the properties on the user object. If the user doesn&#8217;t have any data in its properties, the select-statement will retrieve all users. If we create a users object with a name, and then call the find() method &#8211; then all users with the given name will be fetched from the database and is returned as a resultset by using our resultset-class.</p>
<h3>Example that will fetch all users with the name &#8220;Jesper&#8221;</h3>
<pre class="brush: php">

// Find all users with the name Jesper
$user = new User();
$user-&gt;name = &quot;Jesper&quot;;
$rs = $user-&gt;find();
</pre>
<p>To display these users we do the following</p>
<pre class="brush: php">

for($i = 0; $i &lt; $rs.rowCount(); $i++) {

$foundUser = $rs.getNext(new User());
echo &quot;Name: &quot;.$foundUser-&gt;name.&quot;&lt;br/&gt;&quot;;

}
</pre>
<p>First we get the total rowCount in a for-loop. We then call the getNext()-function to get the next row in the resultset. Since it is a user object we are getting, we need to pass that as an argument to the getNext-function. Then we simply echo the properties on the user-object.</p>
<h2>Conclusion</h2>
<p>The possibilities with DAO&#8217;s are endless. In my eyes it gives a more abstract representation of the data in the database and you assure better seperation of the layers &#8211; compared tih inline SQL in your presentation-layer (HTML). This might not be the best approach on this and can be done in many different ways. Please write a comment if you found this article useful or have any objections :)</p>
<p>Take care!</p>
<p><a class="downloadexample" href="http://www.jesperchristiansen.dk/tutorial/php/php-data-access-objects-part-1/php-data-access-objects-part-1.zip" target="_blank">Sourcecode can be downloaded here!</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.jesperchristiansen.dk/2009/10/php-data-access-objects-part-1/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
