<?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>Query7 &#187; Code Igniter</title>
	<atom:link href="http://query7.com/category/code-igniter/feed" rel="self" type="application/rss+xml" />
	<link>http://query7.com</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Fri, 09 Apr 2010 09:36:13 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Building a Todo List using Code Igniter</title>
		<link>http://query7.com/building-a-todo-list-using-code-igniter</link>
		<comments>http://query7.com/building-a-todo-list-using-code-igniter#comments</comments>
		<pubDate>Sat, 18 Apr 2009 02:06:59 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Code Igniter]]></category>

		<guid isPermaLink="false">http://query7.com/?p=422</guid>
		<description><![CDATA[<p>A Query7 reader <a href="http://query7.com/getting-started-with-code-igniter/#comments">requested</a> this tutorial to be written. It&#8217;s aimed to be a general guide to build &#8216;something&#8217; using Code Igniter. It uses Models, Views, Controllers, Helpers and Libraries and will give you a good idea how Code&#8230;</p>]]></description>
			<content:encoded><![CDATA[<p>A Query7 reader <a href="http://query7.com/getting-started-with-code-igniter/#comments">requested</a> this tutorial to be written. It&#8217;s aimed to be a general guide to build &#8216;something&#8217; using Code Igniter. It uses Models, Views, Controllers, Helpers and Libraries and will give you a good idea how Code Igniter works as a whole. I will be posting more advanced Code Igniter tutorials in the future.</p>
<h2>Setup</h2>
<p>Install and do some basic configuration (URL, database settings) of Code Igniter. If you don&#8217;t know how to do this, read <a href="http://query7.com/getting-started-with-code-igniter/">here</a>. Autoload the <em>database</em> library and <em>url</em> helper as we will be using them.</p>
<p>The user will be able to post new items and delete current items and the app itself will be made up of:</p>
<ul>
<li>One Controller &#8211; todo.php</li>
<li>One Model &#8211; todo_model.php</li>
<li>One View &#8211; todo_index.php</li>
</ul>
<h2>PHP4 Compatibility</h2>
<p>Because Code Igniter supports both PHP4 and PHP5, you are forced to use PHP4 style OOP. Your constructor must be the same name as the class, and it needs to contain <em>parent::Controller();</em> or <em> parent::Model();</em><br />
The naming conventions for controllers and models are very strict. The name of both class and constructor must be the same as the file name, but capitalized. In our case of our controller, the file name is <em>todo.php</em> and the name of the class and constructor is called <em>Todo</em>.</p>
<h2>Model</h2>
<p>The model in Code Igniter is a place where you write your application&#8217;s SQL queries (It doesn&#8217;t map your schema like in Django/RoR). In our application we specify 3 methods, which can then be called upon in the controller. You will notice we are using the Code Igniter database class but not Code Igniter&#8217;s full ORM. This is just personal preference. In get_all_todo_items() we call <em>$q-&gt;result_array()</em> . Think of it as an equivalent to<em> mysql_fetch_array()</em>.</p>
<pre lang="php">class Todo_model extends Model {

    function Todo_model()

    {

        parent::Model();

    }

	function new_todo($content)

	{
		$date = date('l jS \of F Y h:i:s A');

		$q = $this-&gt;db-&gt;query("INSERT INTO items (date,content) VALUES ('$date','$content')");

	}

	function get_all_todo_items()
	{
		$q = $this-&gt;db-&gt;query("SELECT id,date,content FROM items");
		return $q-&gt;result_array();
	}

	function delete_todo_item($id)
	{
		$q = $this-&gt;db-&gt;query("DELETE FROM items WHERE id = '$id'");
	}

}</pre>
<h2>Controller</h2>
<blockquote><p>/system/application/controllers/todo.php</p></blockquote>
<p>You can also see in the Controller that we are loading the <em>form</em> helper and <em>todo_model</em> model. Notice there are no file extensions being used. In Code Igniter URLs are structured like: <em>www.foo.com/ci/index.php/controller/method/</em>. If no method is specified then Code Igniter will automatically run the <em>index()</em> method.</p>
<p><strong>index_page()</strong><br />
We assign the results of <em>get_all_todo_items()</em> to <em>$info['item']</em> . We then pass the $info array (which contains all of our todo items) to our view which we&#8217;ll create soon.<br />
<strong>add_item()</strong><br />
Rather than using <em>$_POST['content']</em> to access a property, you should use <em>$this-&gt;input-&gt;post(&#8216;content&#8217;)</em>. This utilises Code Igniter&#8217;s input class which automatically filters out SQL injections and XSS attacks (<em>line 266 /application/config/config.php</em>)<br />
We then pass the result from that to our new_todo() method in our model and redirect the user back to the index page.<br />
<strong>delete_item()</strong><br />
We can use the <em>segment()</em> method in the <em>uri</em> class to grab bits of information from the URL. Because our URI will be <em>/todo/delete_item/id/</em>, by grabbing the 3rd segment we get the id. We can then feed that do our <em>delete_todo_item()</em> method we defined in the model and redirect the user to the main page.</p>
<pre lang="php">class Todo extends Controller {

	function Todo()
	{
		parent::Controller();
                $this-&gt;load-&gt;helper('url');
		$this-&gt;load-&gt;helper('form');
		$this-&gt;load-&gt;model('todo_model');
	}

	function index()
	{
		$this-&gt;index_page();
	}

	function index_page()
	{
		$info['item'] = $this-&gt;todo_model-&gt;get_all_todo_items();
		$this-&gt;load-&gt;view('todo_index.php', $info);
	}

	function add_item()
	{
		$content = $this-&gt;input-&gt;post('content');
		$this-&gt;todo_model-&gt;new_todo($content);

		redirect('');
	}

	function delete_item()
	{
		$this-&gt;todo_model-&gt;delete_todo_item($this-&gt;uri-&gt;segment(3));

		redirect('');
	}
}</pre>
<p>Finally we need to write our view.</p>
<blockquote><p>/system/application/views/todo_index.php</p></blockquote>
<p>On line 5 you can see the form_open() function being used. This creates our opening form tag. The <em>action</em> attribute is specified by the parameter that we passed. Code Igniter automatically prepends your site URL to this parameter it ends up as <em>action=&#8221;http://path_to_site.com/URI_passed&#8221;</em>. This is useful because if we move the site to another domain name, the form will continue to post to the correct URL. Between lines 12 and 17 we are iterating through the todo items posted.</p>
<pre>&lt;h1&gt;Todo List&lt;/h1&gt;

&lt;hr /&gt;

&lt;?php echo form_open('todo/add_item'); ?&gt;

	&lt;textarea cols="40" rows="10" name="content"&gt;&lt;/textarea&gt;
	&lt;input type="submit" value="Add new item" /&gt;
&lt;/form&gt;

&lt;ul&gt;
&lt;?php
for($i = 0; $i &lt; sizeof($item); ++$i)
{
    echo '&lt;li&gt;' . $item[$i]['content'] . ' - &lt;i&gt;' . $item[$i]['date'] . '&lt;/i&gt; - &lt;a href="' . site_url() . '/todo/delete_item/' . $item[$i]['id'] . '"&gt;Delete&lt;/a&gt;&lt;/li&gt;';
}
?&gt;
&lt;/ul&gt;

&lt;hr /&gt;
&lt;p&gt;Created by &lt;a href="http://www.query7.com"&gt;Query7&lt;/a&gt;&lt;/p&gt;</pre>
<p>Feel free to post any questions or comments you have.</p>
]]></content:encoded>
			<wfw:commentRss>http://query7.com/building-a-todo-list-using-code-igniter/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Getting started with Code Igniter</title>
		<link>http://query7.com/getting-started-with-code-igniter</link>
		<comments>http://query7.com/getting-started-with-code-igniter#comments</comments>
		<pubDate>Sun, 29 Mar 2009 21:05:13 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Code Igniter]]></category>

		<guid isPermaLink="false">http://query7.com/?p=394</guid>
		<description><![CDATA[<h2>What is Code Igniter?</h2>
<p>Code Igniter is a PHP web framework which assists in creating complex websites. Unlike other web frameworks, Code Igniter doesn&#8217;t force you to use all of it&#8217;s components. You select and load them as you require.&#8230;</p>]]></description>
			<content:encoded><![CDATA[<h2>What is Code Igniter?</h2>
<p>Code Igniter is a PHP web framework which assists in creating complex websites. Unlike other web frameworks, Code Igniter doesn&#8217;t force you to use all of it&#8217;s components. You select and load them as you require. The only feature you have to use is the URL structuring (<em>/controller/method/</em>) which is very helpful anyway.</p>
<p>Code Igniter follows the Model, View, Controller (MVC) principle. All of your database queries are stored in models, HTML/XML output in Views and the logic code in Controllers. You aren&#8217;t forced to do this, however it does keep your code organized and reusable.</p>
<h2>Helpers and Libraries</h2>
<p>Like most frameworks, Code Igniter comes with many classes that help assist development. They are split into <em>Libraries</em> (AKA <em>Classes</em>) and <em>Helpers</em>. Libraries contain code that you could use on every page of your application &#8211; Database ORM, Image Manipulation, Pagination and Benchmarking. Helpers are things like Relative URLs, Forms and Email.</p>
<p>Using helpers and libraries is very easy. If you want it included throughout the site see below (autoloads). If you want it included for every method in a Controller, then you can add it to that Controller&#8217;s constructor. If you just want it included in one method, add the code inside the method.</p>
<pre>$this-&gt;load-&gt;helper('helper_name');
$this-&gt;load-&gt;library('library_name');</pre>
<h2>Installation</h2>
<p>Installing Code Igniter is alot easier than installing other PHP web frameworks. There is no need for command line access, nor the need to write your database schema in XML. You only need to edit a few configuration files.</p>
<p>Download and Install the latest version off the Code Igniter <a href="http://codeigniter.com/downloads/">download page</a>. Extract it and upload it to your web server. You may notice the directory <em>/user_guide/</em> . This only contains documentation so it can be deleted safely. Up to date documentation can always be found at <a href="http://www.codeigniter.com/user_guide/">www.codeigniter.com/user_guide/</a>.</p>
<p>Now that Code Igniter has been &#8216;installed&#8217; we need to configure our settings. Open..</p>
<blockquote><p><em>/system/application/config/config.php</em></p></blockquote>
<p>On line 14 we need to set the path to our website. Make sure it has a trailing slash.</p>
<pre>$config['base_url']	= "http://www.website.com/path/to/codeigniter/";</pre>
<p>I also put global XSS filtering on. By default it is not active. Replace line 267 with..</p>
<pre>$config['global_xss_filtering'] = TRUE;</pre>
<p>If your application uses a database, now is a good time to set it up. Open..</p>
<blockquote><p><em>/system/application/config/database.php</em></p></blockquote>
<pre>$db['default']['hostname'] = "localhost"; //usually localhost
$db['default']['username'] = "usernamet";  //database username
$db['default']['password'] = "password";  //password to user set above
$db['default']['database'] = "database";  //database name
$db['default']['dbdriver'] = "mysql";  //database driver - mysql/postgre/sqlite etc..
$db['default']['dbprefix'] = "";
$db['default']['pconnect'] = FALSE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = "";
$db['default']['char_set'] = "utf8";
$db['default']['dbcollat'] = "utf8_general_ci";</pre>
<p>When you call the database class, Code Igniter will connect to the database with the information you set in <em>$db['default']</em> array.</p>
<pre>$this-&gt;load-&gt;database();</pre>
<p>You can then interact with the database through</p>
<pre>$this-&gt;db-&gt;method();</pre>
<p>If your application uses more than one database, you can set another array in the same <em>database.php</em> file but call it a seperate name. eg <em>$db['remote']</em> (copy and paste the <em>$db['default']</em> array and change where necessary). You can then connect that database with..</p>
<pre>$remote_db = $this-&gt;load-&gt;database('remote', TRUE);</pre>
<p>And execute database methods through..</p>
<pre>$remote_db-&gt;method();</pre>
<p>I also recommend you set up autoloads..</p>
<blockquote><p><em>/system/application/config/autoload.php</em></p></blockquote>
<p>Here you can specify classes and helpers that will be available in every file throughout your project. This is useful because you won&#8217;t need to call them every time you make a new controller. I recommend <em>autoload</em>ing the database class and the URL helper. You will find the URL helper very useful in your Views, being able to use relative URLs instead of hard coding full <em>http://www.website.com/path/to/codeigniter/</em> URLs is very useful. You just need to add the name of the library/helper in the appropriate array..</p>
<pre>$autoload['libraries'] = array('database');
$autoload['helper'] = array('url');</pre>
<p>Thats it.. Feel free to ask any questions.<br />
In the next couple of weeks I&#8217;ll be writing a tutorial showing how to get a fully featured authentication system up and running with Code Igniter.</p>
]]></content:encoded>
			<wfw:commentRss>http://query7.com/getting-started-with-code-igniter/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
