<?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>Michael Forbes - User Experience Engineer &#187; code igniter</title>
	<atom:link href="http://mrforbes.com/blog/tag/code-igniter/feed/" rel="self" type="application/rss+xml" />
	<link>http://mrforbes.com/blog</link>
	<description></description>
	<lastBuildDate>Mon, 20 Feb 2012 04:23:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Setting Up a Local Code Igniter Dev Environment (OSX)</title>
		<link>http://mrforbes.com/blog/2009/02/setting-up-a-local-code-igniter-dev-environment-osx/</link>
		<comments>http://mrforbes.com/blog/2009/02/setting-up-a-local-code-igniter-dev-environment-osx/#comments</comments>
		<pubDate>Wed, 11 Feb 2009 20:37:28 +0000</pubDate>
		<dc:creator>michael</dc:creator>
				<category><![CDATA[tutorials]]></category>
		<category><![CDATA[code igniter]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.mrforbes.com/?p=153</guid>
		<description><![CDATA[Where I explain how to prepare a locally developed Code Igniter application. <a href="http://mrforbes.com/blog/2009/02/setting-up-a-local-code-igniter-dev-environment-osx/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I recently downloaded the latest version of CI and began setting it up &#8211; when I realized the setup may be helpful to describe to newcomers to the PHP framework.</p>
<p>While this tutorial is geared towards Mac users, most of the steps should be similar for Windows users.</p>
<p><span id="more-153"></span></p>
<h2>Step 1</h2>
<h3>Download and Install MAMP</h3>
<p><a title="MAMP" href="http://www.mamp.info/en/index.html">MAMP</a> is the OSX version of the one-click Apache-MySQL-PHP install.  If you&#8217;re on a PC, you want to try out <a href="http://www.apachefriends.org/en/xampp.html">XAMPP</a> instead (XAMPP also comes in an OSX flavor, but I haven&#8217;t personally tried it).  In any case, you&#8217;ll want to download one of these two programs and follow the instructions for installation.</p>
<h2>Step 2</h2>
<h3>Download Code Igniter</h3>
<p>Code Igniter is an MVC based PHP framework created by the makers of Expression Engine &#8211; a CMS/blogging platform.  What (I think) sets CI apart from other frameworks like Cake or Zend is its soft learning curve and strong documentation.  The involved community doesn&#8217;t hurt either.  You&#8217;ll want to <a href="http://codeigniter.com/">download it</a> in order to continue to step 3.</p>
<h2>Step 3</h2>
<h3>Install Code Igniter</h3>
<p>Once you&#8217;ve downloaded and unzipped CI, you&#8217;ll want to copy the folder over to your server root.  If you&#8217;re using MAMP &#8211; the default is /Applications/MAMP/htdocs.  At that point, you should change the folder name from CodeIgniter_1.7.1 to whatever you want to use to describe your site.  I changed my folder name to &#8216;ci&#8217;.   That&#8217;s it for installation.</p>
<h2>Step 4</h2>
<h3>Setup Your MySQL database</h3>
<p>Odds are, you&#8217;re following this tutorial so you can build a dynamic site in PHP.  Dynamic nearly always means database, so you&#8217;ll have to create one.  You can do this in MAMP by switching over to your running instance and clicking on &#8216;Open start page&#8217;.  From the links on top, you can select &#8216;phpMyAdmin&#8217;.  Otherwise, you can probably just copy and paste this: http://localhost:8888/MAMP/frame.php?src=%2FphpMyAdmin%2F%3Flang%3Den-iso-8859-1&#038;language=English.  From there, create a new database (I&#8217;ve called mine &#8216;ci&#8217;).</p>
<p>A note here:  CI can handle sessions either with or without the database.  My personal preference is to use the database, because it provides greater functionality, and you don&#8217;t have to worry about hitting your 4k cookie size limit.  Enabling database sessions is fairly simple in CI, and we&#8217;ll get to that further down.  However, you&#8217;ll first need to add the sessions table to the database.  The default name of the sessions table is &#8216;ci_sessions&#8217;  &#8211; you can change this here and in the settings, but remember to do it in both places or this won&#8217;t work.  In this case, I&#8217;m going to keep it the same.  Code Igniter provides the SQL you&#8217;ll need here, so you can click on the &#8216;SQL&#8217; tab in phpMyAdmin and copy/paste the code in.  Here it is:</p>
<pre lang="sql">
CREATE TABLE IF NOT EXISTS  `ci_sessions` (
session_id varchar(40) DEFAULT '0' NOT NULL,
ip_address varchar(16) DEFAULT '0' NOT NULL,
user_agent varchar(50) NOT NULL,
last_activity int(10) unsigned DEFAULT 0 NOT NULL,
user_data text NOT NULL,
PRIMARY KEY (session_id)
);
</pre>
<h2>Step 5</h2>
<h3>Edit your Code Igniter Settings</h3>
<p>From this point, you&#8217;ll want to edit your CI settings to match your environment.  The first thing we&#8217;ll do is open up the system->application->config folder.  In here are all the files that run the configuration settings.</p>
<h4>config.php</h4>
<p>On line 14, you&#8217;ll see this:</p>
<pre lang="php">
$config['base_url']	= "http://example.com/";
</pre>
<p>You&#8217;ll want to change it to your own install directory.  You can also edit your vhosts file to have it match your live url&#8230; but that&#8217;s a more advanced procedure and not part of this tutorial. So in this case, I&#8217;ve changed it to:</p>
<pre lang="php">
$config['base_url']	= "http://localhost:8888/ci/";
</pre>
<p>The next portion of code you might need to change is from lines 234-242, and it looks like this:</p>
<pre lang="php">
$config['sess_cookie_name']		= 'ci_session';
$config['sess_expiration']		= 7200;
$config['sess_encrypt_cookie']	= FALSE;
$config['sess_use_database']	= FALSE;
$config['sess_table_name']		= 'ci_sessions';
$config['sess_match_ip']		= FALSE;
$config['sess_match_useragent']	= TRUE;
$config['sess_time_to_update'] 	= 300;
</pre>
<p>This is where you can setup your sessions to use the database, as outlined above.  Just change $config['sess_use_database'] to true.</p>
<p>There are other settings in config.php you can modify to suit the framework to your needs, but those are the only ones I typically use.</p>
<h4>database.php</h4>
<p>This is the file where we&#8217;ll set our database access &#8211; location, username, password, and database name.  It looks like this:</p>
<pre lang="php">
$active_group = "default";
$active_record = TRUE;

$db['default']['hostname'] = "localhost:8889";
$db['default']['username'] = "root";
$db['default']['password'] = "root";
$db['default']['database'] = "ci";
$db['default']['dbdriver'] = "mysql";
$db['default']['dbprefix'] = "";
$db['default']['pconnect'] = TRUE;
$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>MAMP automatically sets your username and password to root, and your MySQL port to 8889, so can copy and paste the above for working locally.  You can also create a second set of these variables called $db['production'] for example, with your live settings in it.  When you are ready to move the site, all you&#8217;ll need to do is change your $active_group variable to &#8216;production&#8217;.</p>
<h4>autoload.php</h4>
<p>Code Igniter gives you the ability to add only the libraries and functionality you need for your project.  The autoload.php file lets you name which files you want across the application.  You can read more about it on the CI site, but one thing you&#8217;ll likely want to do right away is enable your database and session libraries to load.  This is done on line 42:</p>
<pre lang="php">
$autoload['libraries'] = array('database','session');
</pre>
<p>Odds are you&#8217;ll also want to load the URL helper, that allows you to enter shortcut functions for some common locations (like the base url).  You can do this on line 54:</p>
<pre lang="php">
$autoload['helper'] = array('url');
</pre>
<h4>.htaccess</h4>
<p>CI doesn&#8217;t come with an .htaccess file, but odds are you&#8217;ll want to add additional folders to your root &#8216;ci&#8217; folder while keeping the rest of your site secure by redirecting most requests through the main index.php file.  So, create a file named .htaccess in your main &#8216;ci&#8217; folder.  Within it, you&#8217;ll want to do this:</p>
<pre>
RewriteEngine on
RewriteCond $1 !^(index\.php?|images|css|js)
RewriteRule ^(.*)$ http://localhost:8888/ci/index.php?/$1 [L]
</pre>
<p>What this does in tell the Apache URL-rewrite module to redirect any request that isn&#8217;t in the above list to the index.php file of our &#8216;ci&#8217; directory.  I&#8217;ve added images,css, and js folders to the &#8216;ci&#8217; directory so I can start adding assets there.  When you want to link to your files, you&#8217;ll be able to do so like this:</p>
<pre lang="php">
<img src="<?=base_url()?>images/xxxxxxx.jpg" />
</pre>
<p>That&#8217;s all there is to it.  At this point, you should have all of the basic resources you need to start developing a sweet app in Code Igniter.  When you are ready to move your site to a production server, just remember to change your database settings, base url in config.php, and the RewriteRule in your .htaccess file, and you&#8217;ll be up and running in no time.</p>
]]></content:encoded>
			<wfw:commentRss>http://mrforbes.com/blog/2009/02/setting-up-a-local-code-igniter-dev-environment-osx/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>A Quick Code Igniter and JQuery Ajax Tutorial</title>
		<link>http://mrforbes.com/blog/2009/01/a-quick-code-igniter-and-jquery-ajax-tutorial/</link>
		<comments>http://mrforbes.com/blog/2009/01/a-quick-code-igniter-and-jquery-ajax-tutorial/#comments</comments>
		<pubDate>Thu, 29 Jan 2009 04:18:01 +0000</pubDate>
		<dc:creator>michael</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[code igniter]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.mrforbes.com/?p=138</guid>
		<description><![CDATA[Where I explain how to use the Code Igniter php framework and jquery to do some ajax. Re-post of 8/07 article. <a href="http://mrforbes.com/blog/2009/01/a-quick-code-igniter-and-jquery-ajax-tutorial/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><em>This tutorial assumes a basic working knowledge of Code Igniter.  If you have never used CI before, please refer to the <a href="http://codeigniter.com/user_guide/">framework documentation</a></em></p>
<p>In the old days (2 years ago), working the Javascript magic to create a cool AJAX based event took a fairly decent working knowledge of the mechanisms behind the process.<br />
<span id="more-138"></span><br />
With the increasing popularity of Javascript libraries however, this type of functionality became available to the web site hobbyist, and was made much easier for the web site professional.</p>
<p>The following step-by-step tutorial will show you how to combine the power of <a href="http://www.jquery.com">JQuery</a> (a javascript library that weighs in at about 20k) with <a href="http://www.codeigniter.com">Code Igniter</a> (a PHP framework based on the MVC design pattern) to quickly and painlessly pass a record ID through the javascript and over to the server, where it will be passed to a mysql database, used to retrieve some data, and sent back to the page for display.</p>
<p><strong>Step 1</strong><br />
We begin by assuming that you have a div with an id of content, which is where you would like your freshly retrieved data to display, once it has been freshly retrieved.  For this exercise, you have already taken an action to call your javascript function with a record ID parameter.</p>
<p>The first thing you need to do, is make sure JQuery is being loaded, and to create a function for your AJAX request.</p>
<pre lang="php">
<script language="javascript" src="/path_to_jquery/jquery.js" ></script>
<script language="javascript">
 function get_record_id(record_id)
     {
     }
</script>
</pre>
<p><strong>Step 2:</strong><br />
Next, youll use the JQuery function load, and attach it to your content div:</p>
<pre lang="php">
function get_record_id(record_id) {
     $('#content').load()
}
</pre>
<p><strong>Step 3:</strong><br />
The load function accepts three arguments.  The page to be called on the other side of the HTTPRequest, the array to pass through the POST, and a callback function.  It looks like this:</p>
<pre lang="php">
function get_record_id(record_id) {
     $('#content').load(/controller/method,p,function(str){

	 });
}
</pre>
<p>Lets go back to that.  Code Igniter URLs are created by calling the name of your controller, followed by the function inside the controller class that will handle your request.  If your server does not support mod-rewrite, you may also need to append an index.php to the beginning.  The str inside the callback function is the results of your AJAX request.  There isnt much use for the str when using the .load function, but it does come in handy using the other JQuery AJAX functions &#8211; $.post and $.get, which I assume are self explanatory.</p>
<p><strong>Step 4</strong></p>
<pre lang="php">
var p = {}; //instantiate the array
p[record_id] = record_id //assign your record_id variable to it.
</pre>
<p>Thats all there is to it.  Your final javascript function looks like this:</p>
<pre lang="php">
function get_record_id(record_id) {
     var p = {};
     p[record_id] = record_id
     $('#content').load(/controller/method,p,function(str){

     });
}
</pre>
<p><strong>Step 5</strong><br />
On the CI side, you have a controller and method setup something like this:</p>
<pre lang="php">
class Controller
{
   function Controller()
   {
       parent::CI;
   }

   function method()
   {
   }
}
</pre>
<p>The important part is the method() function, as it will contain some of the code we need to make things happen.</p>
<p><strong>Step 6</strong><br />
The first thing you need to do on the CI side is retrieve the value passed through the request object.  This is simple enough, using $_POST[record_id].  You also want to load up your database model so you can get the record out of your database.  So, well load the database library, and then load the actual model.  Then, we want to send the record ID to the database, get the resulting data, and pass it back out to the request.  our function starts to look like its doing something useful pretty quickly.</p>
<pre lang="php">
function method()
{
   $record_id = $_POST[record_id];
   //set the record ID
   $this->load->library(database);
   //load the database library to connect to your database
   $this->load->model(records);
   //inside your system/application/models folder, create a model based on the procedure
   //outlined in the CI documentation
   $results = $this->records->get_record($record_id);
   //get the record from the database
}
</pre>
<p><strong>Step 7</strong><br />
At this point, we need to go into our records.php file in the model folder.  Since Code Igniter uses a Model-View-Controller structure, database activity, server-side processing, and client-side display should be as separate from one another as possible.  You dont NEED to do this for Code Igniter to do its thing, but its good practice.</p>
<p>Inside the records.php file, well create a method called get_record to match the method referenced above.  Well use it to get a record by its primary key of ID, put the resulting data into an array, and send it back to the controller, out to the view, and ultimately into the content div we started with.</p>
<pre lang="php">
function get_record($record_id)
{
   $this->db->where(ID,$record_id);
   //we want the row whose ID matches the value were passing in
   $query = $this->db->get(record_table);
   //get the table and put it into an object named $query
   $row = $query->row();
   //gets the first row of the resulting dataset.  In this case, only 1 row will ever be returned
   $results[record][$row->ID][name] = $row->name;
   //here, we create a multi-dimensional array holding the returned values
   //based on the key.
   return $results;
   //send the record back to the controller
}
</pre>
<p>The trickiest part of this section is the array.  It seems pretty complex from here, but youll see soon enough how it breaks down into something more manageable as we go along.</p>
<p><strong>Step 8</strong><br />
Were back to the controller again, and we have one more line to add &#8211; this time to pass the resulting data into a view to be formatted and printed to the content div.  The whole method() function now looks like this:</p>
<pre lang="php">
function method()
{
   $ID = $_POST[record_id];  //set the record ID
   $this->load->library(database);
   //load the database library to connect to your database
   $this->load->model(records);
   //inside your system/application/models folder, create a model based on
   //the procedure outlined in the CI documentation
   $results = $this->records->get_record($record_id);
   //get the record from the database
   $this->load->view(AJAX_record,$results);
}
</pre>
<p><strong>Step 9</strong><br />
The AJAX_record.php file should be in your system/application/views folder.  Keep in mind, that when you pass an array to a view (in this case the $results array), it will be exploded inside the view.  So, the path to your record is now $record, instead of $results[record].  Also inside will be your standard HTML markup, and something like this:</p>
<pre lang="php">
< ?php foreach($record as $id=>$value) { ?>
     The name associated with this record is: < ?php print $value[name];?>
< ?php } ?>
</pre>
<p>This output is what php is sending to the request object, and is also what gets loaded into the content div.  Code Igniter and JQuery make it that easy to dynamically load data using AJAX.</p>
]]></content:encoded>
			<wfw:commentRss>http://mrforbes.com/blog/2009/01/a-quick-code-igniter-and-jquery-ajax-tutorial/feed/</wfw:commentRss>
		<slash:comments>58</slash:comments>
		</item>
	</channel>
</rss>

