CodeIgniter From Scratch: Day 2
videos

CodeIgniter From Scratch: Day 2

This entry is part 2 of 17 in the CodeIgniter From Scratch Session
« PreviousNext »

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.

Catch Up

Day 2


Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • http://www.zacvineyard.com Zac

    This is really inspiring me to look into frameworks more.

  • Luke

    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.

  • http://letsgetcool.com ibrahim benzer

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

  • http://www.briannotbryan.com Brian Klepper

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

  • http://www.bluebit.co.uk Mark Jones

    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?

  • Kyle

    Great tutorial. FYI This was on itunes last week.

    • http://nathanledet.com Nathan Ledet

      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

      • http://www.jeff-way.com Jeffrey Way
        Author

        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. :)

      • http://adrusi.com/ adrusi

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

    • http://blog.davidrojas.net David Rojas

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

      • http://www.jeff-way.com Jeffrey Way
        Author

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

      • http://eyoosuf.blogspot.com/ Yoosuf

        its named as CI:Day 2

      • http://blog.davidrojas.net David Rojas

        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.

      • Max

        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!

  • http://www.tomstill.com Tom

    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!

  • Declan

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

  • http://eyoosuf.blogspot.com/ Yoosuf

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

  • http://www.kaydetsene.com SNaRe

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

  • Felix Boyeaux

    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.

    • Dave Kennedy

      Use CI with Doctrine then…

      • IRV

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

  • eques

    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

  • psdmed

    Thanks jeff i was waiting for this !

  • http://www.sergiomasellis.com Sergio Masellis

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

  • fil

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

  • http://www.TechNation.si iMatt

    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

    • http://www.paulchater.co.uk Paul Chater

      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.

      • http://www.paulchater.co.uk Paul Chater

        Sorry I forgot to put < in for the href.

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

  • Yablko

    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 :)

    • http://danharper.me Dan Harper

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

      And yeah, CI escapes data automatically.

  • Monty

    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!

  • Monty

    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!

    • http://www.webeventures.com Prabhjeet

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

      you will get definitly how oop works.

  • stradivarius

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

  • http://www.enatom.com Enatom

    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 .

    • http://www.jeff-way.com Jeffrey Way
      Author

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

    • http://www.paulchater.co.uk Paul Chater

      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”.

  • http://www.sonergonul.com/blog/ Soner Gönül

    Thanks man!

    It’s really great!

  • http://www.sonergonul.com/blog/ Soner Gönül

    Wow :)

  • Wade

    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.

    • http://danharper.me Dan Harper

      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.

      • Wade

        Thanks Dan for the response!

  • leo rapirap

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

    great teacher!

    codeigniter here i come.

  • http://www.paulchater.co.uk Paul Chater

    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.

    • http://www.jeff-way.com Jeffrey Way
      Author

      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.

  • http://twitter.com/arnold_c Arnold

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

    Nettuts is on fire.!!!.

  • Johniefp

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

  • Ejaz

    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.

    • Abulafio

      “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!

    • http://www.jeff-way.com Jeffrey Way
      Author

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

    • Ejaz

      Thanks Abulafio and Jeffrey for the great tip.

  • jeanv

    very useful, thanks mate !

  • Jorge Mudry

    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.

  • http://www.kaydetsene.com SNaRe

    When will you release new screencast?

    • http://www.jeff-way.com Jeffrey Way
      Author

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

  • jodu38

    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 ?? ;-)

    • http://www.jeff-way.com Jeffrey Way
      Author

      This Friday. :)

  • IRV

    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?

  • https://www.refreshedweb.com Pierce Moore

    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!

    • http://www.jeff-way.com Jeffrey Way
      Author

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

      • Monty

        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!

      • http://adrusi.com/ adrusi

        Day 3 on itunes is just a copy of day 2

  • Marcelo

    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!

  • http://twitter.com/janhamburg JanHamburg

    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 :)

    • http://www.benoitvilliere.com Ben

      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 ;)

  • Ali Baba

    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.

    • http://www.benoitvilliere.com benoa

      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]

  • http://www.benoitvilliere.com Ben

    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.

  • http://www.benoitvilliere.com Ben

    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 ;)

  • Danijel

    when part 3 is coming?

  • Danijel

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

  • http://www.webeventures.com Prabhjeet

    This series rocking. keep it up.

  • http://www.jazfx.com Joel Zayas

    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.

  • Naim Jakson

    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!!!

  • http://www.steevenz.com steevenz

    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..

  • Olivier

    Nice Works

  • http://brasspoker.com DonCoryon

    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.

  • gerb inajada

    hie jeff…

    is there a way to download all videos?

  • gerb inajada

    hie jeff!

    is there a way to download all the videos?