Build your First Web 2.0 App with PHP, Ajax and CodeIgniter

CodeIgniter is an awesome tool if you want to learn PHP or start to use object oriented programming (OOP) with your new website. CodeIgniter is flexible with a small footprint and offers tons of features already built in, such as session management, active record use with mysql databases. The best part is it lets you use Model-View-Controller (MVC) programming. This way, your styling (view) is separate from your content (generated by controller) and lets you manipulate your raw data with a model.

There are two awesome video tutorials on CodeIgniter to show you how powerful and easy CodeIgniter can be when starting a website; however the first time I tried setting up an application I had trouble linking to images, css, and javascripts and setting up an htaccess file for friendly URLs. I have included a set of directions to help with these issues in your first CodeIgniter app.

I am working on a mac so if you have one also the tutorial may be a bit easier to follow. This tutorial also assumes you have a running apache web server with PHP and MySQL installed.

You will need a few files to start with your CodeIgniter app. You can download them from their websites below or download the zip file full I have compiled with everything you need (including CodeIgniter 1.5.4 and full compiled mootools). If you download my zip file with all the necessary components, just drag the files to a new directory in your server folder and you can skip right to developing a controller and view.

  • If you don’t have CodeIgniter already, download it at codeigniter.com.
  • If you want to use ajax, download the javascript file at mootools.net. There are also some really cool demos of mootools in action. The best part is mootools is object oriented just like CodeIgniter is! It really is a simpler way to program.
  • If you want to use friendly URLs, such as http://your-host-name/blog or http://your-host-name/contact, you will need an htaccess file, which helps in redirecting browser queries to the right place. Otherwise, your URLs will have index.php stuck in the middle of them (http://your-host-name/index.php/contact).

Now that you have CodeIgniter, drag the index.php file and system folder to a new project folder in your hosting root. I named my project folder c3. In this folder, I have created a few folders: images, _js, css, and uploads. Remember the names of these folders because you will need them for your .htaccess file.

CodeIgniter Project View in Mac OS X

In order to use friendly URLs with CodeIgniter, you need to set up an .htaccess file in your project folder. On my Mac and the web server I use, the file is invisible, but if you open your project folder with textmate you will be able to create the file and access it later. Inside this is what I have successfully used on my mac for rerouting to work.

Options +FollowSymLinks
RewriteEngine on
#RewriteCond $1 !^(index\.php|images|css|_js|uploads|robots\.txt)
#RewriteRule ^(.*)$ /~danielerrante/bizwidgets/index.php/$1 [L]

Creating an .htaccess file will solve a lot of headaches later down the road. This enables your website to get photos, javascripts and css files from the appropriate folders while still re-routing page calls.

htaccess file in textmate

Next, you want to create a controller. This will handle any processing you may require before sending a view to your visitor. Here, I have a controller called home.php

You need to create a class that extends Controller like this:

class Home extends Controller {

function Home() {
parent::Controller();
}

function index() {
$this->load->view('home_view', $data);
}

}

Now, you have a working controller. Next we will make a view for the home controller. Since we want to load the view ‘home_view’, we are going to create home_view.php in the views folder.

View in CodeIgniter

As you can see in the top of the HTML code, I like to create a header (and footer further down) for each one of my pages. Then I call the header and footer at the top and bottom of each page. This way, If I need to change something in the header, such as the page title or meta tags, I can do it on the header file once.

If you visit http://your-host-name/home your page should be working.

And, that’s it. Have fun with CodeIgniter. The forums are a great resource when you get stuck. Also check out the forum on mootools. The user guide included with CodeIgniter will also help you understand MVC programming and give you more instructions on how to use CI to its full advantage.

Thanks for reading! Tell me what you think!

Come back soon for Part 2 on MooTools.



148 Responses to “Build your First Web 2.0 App with PHP, Ajax and CodeIgniter”

  1. I’m a huge lover of your blog! Look at my website, You can analyze your blog there!

  2. Gran articulo, puedo linkearlo desde mi sitio web ?

  3. Clayton Fox says:

    I can’t believe Cameroon is out. That was a quick exit. I really expected that they had a good chance to do well in this years world cup. Maybe in four years. Maybe its time to jump on the Argentina bandwagon. Looks like Demichelis has already scored. Go Argentina. To make me feel better from that devistating loss by Cameroon, I have been watching some funny jokes.. This joke is really funny: http://www.youtube.com/watch?v=N3j7uSbccSc

  4. Dear admin, thnx for sharing this blog post. I found it wonderful. Best regards, Victoria…

  5. blogregator says:

    Great post! Thank you!

  6. for sure a great blog. have a blessed one

  7. If someone doesn’t come in and help clean this place up, it’s online status will deteriorate most defintely. I would hate to see that happen.

  8. Awesome Post, thanks for the useful Post. I will come back soon ? Great information about boiler repair: Boiler Breakdown London

  9. Grafik says:

    hi wazzup… i just wanted to say that my firefox is crashing when I click on the pics… are you using some js or something?

  10. i read a lot just about that in the last few month and i think it might be true. Eventhough i believe everyone is responsible for himself. No Offense, Just my opinion…

  11. Dylan Titlow says:

    Thank you for posting this great blog. Visit my very own!

  12. Nearly all plastic surgeons and patients concur that the new cohesive silicone gel implants have a more natural feel and are less likely to produce rippling than saline breast implants. The cohesive gel implant, however will require a slightly larger incision to allow placement.

  13. 熱水器 says:

    I realize the 2worry;I am very y sad to hear that you got your Supra fixed; I remember reading about some of your questions2x !.

  14. Stranger says:

    Daniel Errante, you are doing a good job here. But, man! Do something about this SPAM, maybe you can integrate Akismet. SPAM makes things look awful here. Cheers!

  15. Great design may be excellent for some, but to some others, it’s garbage. Trust us, we have suffered our share of differing opinions on our very own design work…

  16. astravans says:

    lowrider astravan, thats what its about

  17. Livejasmine says:

    I believe everyone really should go through this post , this really is just wonderful update info and additional : I wish you are going to write far more!

  18. Dog says:

    Solid information…1st time to this website. Thank you for sharing. I am going to bookmark this blog. I am way interested in kitten characteristics. I believe cats are really smart little critters. I guess we shall see!! Thank you again.

  19. I was hoping to see more how they handle the dispute of seeking to break in and live a regular life which I think up to the first major crisis it does deal with to some extent. But after that life outward to the mangaka cosmos seems to be discounted. Perhaps thats the answer, that this universe is all consuming.

  20. Nilsa Madge says:

    Fantastic reading, but is that truly the way it works? What about this snow sled snowmobile helmet headgear

  21. Fantastic details with regard to upload files for having myself personally get moving. I will keep this specific website link and return to this.

  22. Excellent points with regard to upload files for having myself personally get started. Most definitely i’ll keep this specific website link and revisit it.

  23. Thanks mate. Nice article you have here. Have some extra sites to point to with a bit more stuff like this?

  24. It’s a good idea, I love it.

  25. bathroom vanities need not be expensive, there are many bathroom vanities that can be bought at a bargain price ‘*-

  26. Lcd led hdtv says:

    Grand post! this will absolutely support me.

  27. I genuinely like your angle that you have on the subject. Certainly wasn’t thinking on this at the time I begun searching for tips. Your ideas was totally easy to get. Im glad to find out that there’s an person here that obviously understands on the spot what its is talking about.

  28. thank you for your beneficial article the following informations extremely usefull also i passionately advise this web site who wants to fix laptop computer self on this web page provides variaty of laptop repair video lessons Click Here

  29. Interesting idea. My coworkers had experienced the same when they were looking for Wicked Tickets. I mentioned to them to Buy Wicked Tickets from BuyWickedTickets . net

  30. Hack any FB account in seconds!

  31. Ola Escareno says:

    Definitely, what a splendid site and educative posts, I will bookmark your site.Best Regards!

  32. I can’t yet agree. I always was going to write in my site something such as that but I guess you’r faster.

  33. Good website, exactly where did you arrive up because of the info in this chunk? I’m pleased I identified it regardless that, ill be checking back soon to find what other content articles maybe you have.

  34. I have been scouring the Internet for such information and just wanted to thank you for this post. By the way, just off topic, how can i get a copy of this theme? – Regards

  35. here says:

    A low semen analysis examines some characteristics of a male’s semen and the amount of sperm that is contained in the low semen. It may be done while investigating a couple’s infertility problems or maybe after a vasectomy to verify that the procedure was successful. It is also used for testing some donors for ejaculation fluid donation. Lately it’s really possible to increase semen fluid with absolutely natural ways including buying online herbal pills from the many online stores. Ejaculation volume is the amount of semen concentration of sperms in our seminal fluid. Many factors are taken into consideration to really measure the semen count of a each man such as the true length of time between ejaculations, semen sample analysis, how the sample is kept when being transported to the lab. Pills for more semen volume is best completely herbal formula that without doubt will improve the volume of sperm volume by no less than 500%. The amazingly leading herbal medication contains a lot of native American herbs, minerals and vitamins.

Leave a Reply