CodeIgniter From Scratch: Day 2

CodeIgniter From Scratch: Day 2

Jul 27th in Screencasts by Jeffrey Way

Continuing on from day 1, today, I'll teach you five different ways to write select statements for your database. If you haven't watched the first entry in this video series, don't worry; each video can function on its own as a single tutorial. Having said that, I highly recommend that you watch each screencast.

PG

Author: Jeffrey Way

Hi, I'm Jeff. I'm the editor of Nettuts+, and the Site Manager of Theme Forest. I spend too much time in front of the computer and find myself telling my fiance', "We'll go in 5 minutes!" far too often. I just can't go out to dinner while I'm still producing FireBug errors...drives me crazy. During my free time, I sporadically write articles for my own personal blog. If it will keep you in the good graces of the church, follow us on Twitter.

Catch Up

Day 2


Related Posts

Check out some more great tutorials and articles that you might like

Enjoy this Post?

Your vote will help us grow this site and provide even more awesomeness

Plus Members

Source Files, Bonus Tutorials and
More for $9 a month for all TUTS+
sites in one subscription.

Join Now

User Comments

( ADD YOURS )
  1. PG

    Zac July 27th

    This is really inspiring me to look into frameworks more.

    ( Reply )
  2. PG

    Luke July 27th

    If I’d known when this was coming out I would have been counting the days. Instead it was a more frenzied daily check… but either way thank you very much for putting the time in to create this very approachable series.

    ( Reply )
  3. PG

    ibrahim benzer July 27th

    i was waiting for that for a long time… thanks jeff

    ( Reply )
  4. PG

    Brian Klepper July 27th

    Nice work on this post. CI is next on my to do list.

    ( Reply )
  5. PG

    Mark Jones July 27th

    Good tutorial as usual jeffrey, easy to follow and it was good to see the different ways of doing the same (kind of) task. Can’t wait for this series to start getting indepth. Are you planning on doing one a week with this one?

    ( Reply )
  6. PG

    Kyle July 27th

    Great tutorial. FYI This was on itunes last week.

    ( Reply )
    1. PG

      Nathan Ledet July 27th

      yeh I downloaded it and watched it last Tuesday…someone must have forgot to hit the “publish” button..oops :P

      hopefully tomorrow the 3rd will be out :D

      ( Reply )
      1. PG

        Jeffrey Way July 27th

        Yeah – I was on vacation last week; So I moved the video to my lap. What I forgot was that my laptop doesn’t have my mov -> flv program. So when I uploaded it to blip, the quality was poor. I decided to wait until today for that reason. :)

      2. PG

        adrusi July 27th

        have you made part three yet, I’ve been waiting ever since I watched part 2 on iTunes

    2. PG

      David Rojas July 27th

      It’s not showing on my iTunes yet, I don’t know why. The latest is part 1.

      ( Reply )
      1. PG

        Jeffrey Way July 27th

        It can take 30 minutes or so for iTunes to update.

      2. PG

        Yoosuf July 27th

        its named as CI:Day 2

      3. PG

        David Rojas July 27th

        By the way, the last 3 screencasts don’t work on iPod Touch/iPhone (resolution is too high). I don’t know if I’m the only one, but I usually watch screencasts in my iPod.

      4. PG

        Max July 28th

        It never crossed my mind that w/ iTunes you could sync this to a iPhone or iPod Touch, and then watch it (DISCLAIMER: I do not use iTunes or Apple products for that matter very much).

        I would love to be able to watch these on my android phone… Any ideas? Are they available in mp4? Or some other format which I could then convert to an android compatible version?

        Portable programming video tutorials, FTW!

  7. PG

    Tom July 27th

    I think I might have to dig out part one and have a go when I get chance – part one got some good feedback.

    Something else to add to the list!

    ( Reply )
  8. PG

    Declan July 27th

    Great tutorial. CodeIgniter is sounding much more interesting by the day.

    ( Reply )
  9. PG

    Yoosuf July 27th

    i am gonna watch, guess yet another rokinf @jeff tut

    ( Reply )
  10. PG

    SNaRe July 27th

    Nice video i downloaded this from itunes 5 days ago :D

    ( Reply )
  11. PG

    Felix Boyeaux July 27th

    Thanks Jeff! Too bad CodeIgniter doesn’t support object-relational mapping as an option though… I find it way more convenient to work with than all those foreach loops and stuff.

    ( Reply )
    1. PG

      Dave Kennedy July 27th

      Use CI with Doctrine then…

      ( Reply )
      1. PG

        IRV July 28th

        Thanks for the tip, I was looking for an ORM like Hibernate but for PHP ^^

  12. PG

    eques July 27th

    well this covered the essentials indeed; I read al this in about 160 pages (2 books) about CI.
    I must say you summarize is very well. I’m looking forward to see part3. Cause i don’t have time this week to read the books :)

    good job

    ( Reply )
  13. PG

    psdmed July 27th

    Thanks jeff i was waiting for this !

    ( Reply )
  14. PG

    Sergio Masellis July 27th

    Im very excited to go through this tutorial series. its so good so far.

    ( Reply )
  15. PG

    fil July 27th

    Based on your 1st tutorial I tried CI and loved it! Been working with it ever since. Thanks for the great info!

    ( Reply )
  16. PG

    iMatt July 27th

    Hello there!

    I would like to know, where did i mess it up, because when i load a class (eg. index.php/gallery/show) it doesnt load the images anymore and none of the JS scripts. How should i determine the path to all the scripts and images?

    Tnx, Matt

    ( Reply )
    1. PG

      Paul Chater July 27th

      Hey Matt,

      The best way to do this would setting a rule in your .htaccess file explicitly saying “let codeigniter access these files”. It would go something a little bit like this:

      RewriteEngine on

      RewriteCond $1 !^(index\.php|static|robots\.txt)

      RewriteRule ^(.*)$ index.php/$1 [L]

      then link your images / files like so:
      <link rel="stylesheet" href="static/styles/main.css" type="text/css" media="screen,projector" />

      Hope this helps,

      Paul.

      ( Reply )
      1. PG

        Paul Chater July 27th

        Sorry I forgot to put < in for the href.

        It should be like this: href=”<?=base_url();?>static/styles/main.css”

  17. PG

    Yablko July 27th

    Hi. Just wanted to say, that when using active record, you can also chain your commands like this:

    $select = $this->db->select(’username, email’)
    ->from(’meta’)
    ->join(’users’, ‘users.id = meta.user_id’)
    ->where(’meta.user_id’, $id)
    ->limit(1)
    ->get();

    (I hope the formatting doenst get too messed up so you can still get the idea…)

    also, I have to add that its not really necessary to use a Model
    sure you should do it, but sometimes, when doing smaller projects, you can save time doing the database manipulation in your Controllers (sure its against the standards, I just wanted to point out that CodeIgniter doesnt force you to use them)

    and another thing – I believe, that when using active record, CI does the escaping for you :)

    ( Reply )
    1. PG

      Dan Harper July 28th

      Just a note: chaining the commands will only work in PHP5.

      And yeah, CI escapes data automatically.

      ( Reply )
  18. PG

    Monty July 27th

    Hi, I’m really enjoying these screencast tutorials, especially the jQuery for Beginners one! Although, I’m a bit sad that some of that series are missing from iTunes podcasts (I think series 8 and 9 and another one are missing) because I like viewing them on the iPhone while commuting.

    Great Job, Jeff! Keep up the good work!

    ( Reply )
  19. PG

    Monty July 27th

    Jeff, I forgot to mention: Another tutorial series I’d love to see is PHP OOP for Beginners. I’ve been programming with PHP for 8 years, but, just could never understand OOP as many times as I tried. So, I still do procedural programming, but, am finding that OOP seems to be more popular now with PHP coders.

    Thanks!

    ( Reply )
    1. PG

      Prabhjeet August 1st

      go with Lynda.com PHP with.MySQL Beyond the Basics

      you will get definitly how oop works.

      ( Reply )
  20. PG

    stradivarius July 27th

    I’m collecting this and i will watch them when all 10 are published!

    ( Reply )
  21. PG

    Enatom July 27th

    I love jeff way so much, but the “lets get alittle bit more REAL-ESTATE” phrase bugs me so much, … Jeff its called SPACE not “real estate” … anyway, i still love you .

    ( Reply )
    1. PG

      Jeffrey Way July 27th

      Actually, it’s correct to use “real estate” that way. Thanks.

      ( Reply )
    2. PG

      Paul Chater July 27th

      Don’t diss the Jeff-meister, he’s the Pro he knows what he’s talking about xD Besides, Jeffers is right… it is called “Real Estate”.

      ( Reply )
  22. PG

    Soner Gönül July 27th

    Thanks man!

    It’s really great!

    ( Reply )
  23. PG

    Soner Gönül July 27th

    Wow :)

    ( Reply )
  24. PG

    Wade July 27th

    Hey Jeff,

    Thanks for the great tut again. Just a quick question though in the final method you used where you used active records to form the sql statement ie:

    $this->db->select(’title, content’);
    $this->db->from(’data’);
    $this->db->where(’id’, 2);

    Would it be necessary to escape the data here or does codeigniter handle this for you, say if your getting this from an input form from a user? (like your previous method…)

    Thanks.

    ( Reply )
    1. PG

      Dan Harper July 28th

      CI’s ActiveRecord automatically escapes data before processing it in the database.

      However I’m sure data ISN’T escaped when running a raw SQL command eg. $this->db->query(’SELECT * FROM `data` WHERE `id`=2′) as it is not being processed through ActiveRecord.

      ( Reply )
      1. PG

        Wade August 5th

        Thanks Dan for the response!

  25. PG

    leo rapirap July 27th

    Jeff helped me to get started with php and jquery
    and I learned a lot from him.

    great teacher!

    codeigniter here i come.

    ( Reply )
  26. PG

    Paul Chater July 27th

    Jeff,

    I’ve got to hand it to you dude. You are the schitt. I can’t wait for more of these screencasts to be published because I always get quite confused with PHP. I don’t know why though haha, but every time I get confused I just tend to watch / read one of your screencasts / tutorials.

    The way you tend to explain code is top notch and I think if you put a baby next to a computer watching your screencasts then having him/her code he/she’d be able to do it in an instant, because that’s how helpful it is. So understandable even a baby can do it!

    Please do keep this series coming. It’s a shame my Plus Subscription isn’t getting used by this. Haha.

    - Paul.

    ( Reply )
    1. PG

      Jeffrey Way July 28th

      Thanks, Paul! I appreciate that. I’ve actually read/watched some of your tuts on Codr.eu as well.

      I think the key with these screencasts is to be very casual about them. I’m sure you guys have noticed, but I do virtually no preparation for these videos. That way, when I do make my numerous mistakes (as you all will too), you’ll learn how to fix them.

      I was never a big fan of the video tutorials that had all of the slides. I’d much prefer the casual route — almost as if the teacher is having a cup of coffee with you while teaching.

      ( Reply )
  27. PG

    Arnold July 28th

    JW thanks! again!…when will the part 3 will come?
    keep it up with the good work youve done.

    Nettuts is on fire.!!!.

    ( Reply )
  28. PG

    Johniefp July 28th

    Is this for Plus members only? I cannot see the video.

    ( Reply )
  29. PG

    Ejaz July 28th

    HI Jeff,
    Great tutorial. There is one ting bothering me i.e.
    Why you did no used closing php tag “?>” for classes? Or I have overlooked something.

    ( Reply )
    1. PG

      Abulafio July 28th

      “PHP Closing Tag

      The PHP closing tag on a PHP document ?> is optional to the PHP parser. However, if used, any whitespace following the closing tag, whether introduced by the developer, user, or an FTP application, can cause unwanted output, PHP errors, or if the latter are suppressed, blank pages. For this reason, all PHP files should OMIT the closing PHP tag, and instead use a comment block to mark the end of file and it’s location relative to the application root. This allows you to still identify a file as being complete and not truncated.”

      from the CI userguide :
      http://codeigniter.com/user_guide/general/styleguide.html

      Bye!

      ( Reply )
    2. PG

      Jeffrey Way July 28th

      It’s considered a best practice. That way, you don’t have to worry about any white space issues.

      ( Reply )
    3. PG

      Ejaz August 4th

      Thanks Abulafio and Jeffrey for the great tip.

      ( Reply )
  30. PG

    jeanv July 28th

    very useful, thanks mate !

    ( Reply )
  31. PG

    Jorge Mudry July 28th

    JW thanks! Great tutorial.

    A small tip:

    In your first getAll function you can avoid an unnecessary loop doing this:

    function getAll(){
    return $this->db->query(’Your Query Here)->result();
    }

    It will return an array with all records (or an empty array). This way you get the same result and is a little more efficient.

    ( Reply )
  32. PG

    SNaRe July 28th

    When will you release new screencast?

    ( Reply )
    1. PG

      Jeffrey Way July 28th

      I’m shooting for Friday (central US time).

      ( Reply )
  33. PG

    jodu38 July 28th

    Very easy but so well explained, it’s a pleasure to watch screencasts when you feel that the author wants really to learn you something. NICE JOB Jeff !

    So when for the next CI tuts ?? ;-)

    ( Reply )
    1. PG

      Jeffrey Way July 28th

      This Friday. :)

      ( Reply )
  34. PG

    IRV July 28th

    Thanks for the screencast!
    One question: when you write “$this->db->where(’id’,1);”
    it’s the same as the SQL: “WHERE id=1″
    But what if I want for example “WHERE id>1″, what should I write in that case?

    ( Reply )
  35. PG

    Pierce Moore July 28th

    Jeff, I am absolutely Ga-Ga about this series already. I will be watching this screencast tonight, as I have literally been checking for this video frantically every day (sometimes more than once). I am teaching myself codeigniter, and sincerely appreciate your time in creating this series. I even downloaded a new FF Add-On that gave me the ability to download flash video from the web, so I could download your Day 1 screencast from Blip.Tv (which is about to happen again for this screencast) so I could have it on my personal computer.

    Great work, as always!

    ( Reply )
    1. PG

      Jeffrey Way July 29th

      Thanks! But don’t forget, you can download all of the videos from iTunes. Just search for “Nettuts.” :)

      ( Reply )
      1. PG

        Monty July 29th

        Jeff, not all the videos are available on iTunes. For example, the jQuery for Beginners series is missing Days 8 and 9. Day 10 is repeated twice (but one of them isn’t actually a jQuery tutorial) and Day 12 is missing the second part, even though there are two, they are both identical.

        It would be great if that could be fixed, I’m really enjoying the jquery tutorials and would love to see all of them on iTunes.

        Thanks!

      2. PG

        adrusi August 3rd

        Day 3 on itunes is just a copy of day 2

  36. PG

    Marcelo July 29th

    JW,

    i’ve made a CMS with CI at work and they asked me to implement start and expiration dates for posts. Can you demonstrate how to do this with CI and mysql?

    As aways, great tut!

    ( Reply )
  37. PG

    JanHamburg July 29th

    Hi Jeff, first, thanks a lot for this awesome tutorial.

    Now I got a question:

    On 18:45 you change the id that is passed to the query to 4.
    After executing this we face an error and you say thats because nothing had returned from the query.

    Whats then the meaning of line
    46: if ($q->num_rows() > 0) {

    Thanks for clearing this up to me :)

    ( Reply )
    1. PG

      Ben July 31st

      This lines meaning is to handle the case in which there are no entries. Though it doesn’t handle it enough, if I can say.

      But the tutorial wasn’t about PHP best practices.

      Here you should put an else statement which returns false. This way you can write in your controller :

      $entries = $this->modelNameIForgot->getAll();
      if($entries){ // do something }

      Also, Jeff should note that Code Igniter is not camelcased! That’s part of why I love it so much ;)

      ( Reply )
  38. PG

    Ali Baba July 30th

    Good Tutorials

    I am interested in clean URLs. The URLs in CI is not really clean looking http://www.example.com/index.php/welcome

    Is there going to be explanation how to make them more SEO friendly and cleaner.

    ( Reply )
    1. PG

      benoa August 1st

      You can remove the /index.php/ section from CI urls by creating a .htaccess file with the following instructions :

      RewriteEngine on
      RewriteCond $1 !^(index\.php|images|robots\.txt)
      RewriteRule ^(.*)$ index.php/$1 [L]

      ( Reply )
  39. PG

    Ben July 30th

    Hi Jeffrey, thank you for this great tutorial again! I’m working with CI since a few months now, and enjoy every minute of it.

    There’s something I’d like you to digg in for us into one of your next screencasts… It would be authentication and how to implement it into the controllers. I think I’m not doing it the best way, I’m sure you can enlight me on this point. Maybe it could be the occasion to show people how to integrate Zend Framework components into CI (auth and acl)…?

    Thank you again man, your screencasts are always so good to watch.

    ( Reply )
  40. PG

    Ben July 30th

    I just discovered this awesome post by Jamie Rumbelow here -> http://www.codr.eu/codeigniters-hidden-gems

    He’s giving out lots of tips and tricks about CodeIgniter ;)

    ( Reply )
  41. PG

    Danijel July 31st

    when part 3 is coming?

    ( Reply )
  42. PG

    Danijel August 1st

    When part 3 is coming out? I can’t wait to see it…
    Will it be out by tomorrow?

    ( Reply )
  43. PG

    Prabhjeet August 1st

    This series rocking. keep it up.

    ( Reply )
  44. PG

    Joel Zayas August 3rd

    Awesome tutorial Jeff. I’m still learning the ropes to ci. I was wondering if in the future you can touch on using embedded views with ci (using one template/view that shells other views for header, nav, type elements). Seems like a highly sought out topic to ci newbies.

    ( Reply )
  45. PG

    Naim Jakson August 9th

    Hi JW. Am new to PHP. How do I use the user Input and save it into the db, and what if I want to use a specific User data i.e. a data belonging to a particular USER?

    I hope am clear aww, english!!!

    ( Reply )
  46. PG

    steevenz September 18th

    hi all..
    first of all for Jeff.. very very.. great Tutorial.. four thumbs up for u.. ^___^

    i have a question.. related with this tutorial..

    if i have database table named article and have 3 column (id, title_id, title_en)
    i usually echo them by getting parameter from the url.. like index.php?lang=id or index.php?lang=en and in the PHP file..

    in codeigniter.. how to do that.. or maybe a different way.. when display something from database by url parameter..

    thanks.. Jeff..

    ( Reply )
  47. PG

    Olivier October 8th

    Nice Works

    ( Reply )
  48. PG

    DonCoryon October 12th

    Thank you for this tutorial. Later I will move on to day 3. I’m learning so much, this type of tutorial is exactly what I have been looking for.

    ( Reply )
  49. PG

    Jeffrey Way July 27th

    Vacation. Going over all tutorial submissions today. :)

    ( Reply )
  50. PG

    Yoosuf July 27th

    yeah its named as CI:Dat 2

    ( Reply )
  51. PG

    Nate July 27th

    What? VACATION? Good gawd. *sigh*

    *mumbles*….vacation….

    :P

    ( Reply )
  1. Arrow
    Gravatar

    Your Name
    July 27th