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
  • Jubic Wong

    Hey Jeff,

    I think I noticed an error in your third getAll() method:

    Instead of:

    function getAll() {
    $this->db->select(‘title’, ‘contents’);
    $q = $this->db->get(‘data’);
    if ($q->num_rows() > 0) {
    foreach ($q->result() as $row) {
    $data[] = $row;
    }
    return $data;
    }
    }

    It should be:

    function getAll() {
    $q = $this->db->get(‘data’);
    $this->db->select(‘title’, ‘contents’);
    if ($q->num_rows() > 0) {
    foreach ($q->result() as $row) {
    $data[] = $row;
    }
    return $data;
    }
    }

    I assume the logic is that you need to get the db table first before you can get the columns.

    • http://zawaruddin.blogspot.com zawaruddin
    • Matanya

      it’s not an error.
      the Select method should logically precede the Get method as the former is not doing the actual query but defines it, which should obviously occur before it is executed by the latter.

  • http://alexconcepts.com alex

    i know i’m kinda late here, but i was wondering about the error you get at about 18:51 “Invalid argument supplied for foreach()”. I understand it’s because the query doesn’t return anything, but then again this basically renders the if statement useless. How can $q->num_rows() > 0 be true there? Can you please clarify this?

    • http://sumnercreations.com Ammon

      I believe it’s because of how he has the if statement setup. He is returning $data whether or not the foreach code is run. So if the num_rows() returns 0 then he returns an empty or NULL array and therefore receives the error. In order to avoid this, you would need to have an else that sets the data array to some sort of error message such as “Your query returned no results. Please try again.”

      I hope that makes sense.

  • Spawn

    Hi,

    No disrespect meant, but this is really sloppy, and I can’t help feeling that i’m learning bad practices by learning from these tutorials. I had higher expectations from net tuts, to be honest. You’re just making so many mistakes and having to correct yourself, that i’m finding it hard to concentrate.

    I know it would take longer to produce the video, but if you re-did bits where you made mistakes, i think it would be much more impressive.

    Thanks for making the effort anyway.

    Spawn

  • Jake

    Pardon my ignorance on the matter but what’s the point of using active records? Isn’t it far more succinct to write one line of SQL instead of 3 lines of AR code:

    $this->db->query(” SELECT title, contents FROM data WHERE id = 2 “);

    Instead of:

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

    It just seems more verbose to me but then again I know nothing about AR so I could be missing something. Would really like to know what the benefits are.

    • http://twitter.com/gabbsmo Gabriel

      Actually you can write nested AR-statement. I think the author did it this way to not confuse beginners. Also, using AR makes your app more db agnostic.

    • rizky

      Do you know that AR offer you preprocessing for escape string. You don’t have to get Mysql Injection attack or manually put Mysql_escape_string.

      Why it should be complicated if you can do it easier?

      I use the simple query (without AR) if it’s the complex query only.

  • Linda

    Pardon my ignorance on the matter but what’s the point of using active records? Isn’t it far more succinct to write one line of SQL instead of 3 lines of AR code:

    $this->db->query(” SELECT title, contents FROM data WHERE id = 2 “);

    Instead of:

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

    It just seems more verbose to me but then again I know nothing about AR so I could be missing something. Would really like to know what the benefits are.

  • horhe normanito

    Thank You!!! :D

  • BG

    At the very beginning of the day 2 video (0:40 mark), Jeff references changing the name of the exact set of files to ci_day2. I know it must be elementary, but I’m not sure exactly what we’re supposed to do here. Can someone clarify?

  • Robo

    Another great tutorial. Thanks for sharing : )

  • sarmen

    in my opinion active records seems kind of a bad idea. because if your queries become very complex its going to be confusing typing all that out. why not just stick to the method we are all used to writing it all out and sticking it to a variable that way life will be easier in the long run.

  • abdumoulana

    hi guys…..

  • Carmen

    Wonderful tutorials. I just watched first two of them and i cleared a lot of my initial questions

  • http://www.placingjobs.com Shairyar

    Thanks for the tutorial, I just started working on this framework and this is really helpful…

  • alozuldo

    Great tutorial!!!!!!! Thanks

  • http://www.musicmusicmusic.webege.com/ Ken Titzel

    function getAll() {
    $q = $this->db->get(‘data’, 2);

    I tried setting the offset like so:
    function getAll() {
    $q = $this->db->get(‘data’, 2, 5);

    and I get error like so:
    A PHP Error was encountered

    Severity: Warning

    Message: Invalid argument supplied for foreach()

    Filename: views/home.php

    Line Number: 10

    Does anyone know what/why this is happening?

  • diafol

    To all those that are poo-pooing AR with binding. I have to say that it seems a much nicer way of ensuring that all your db drivers work (similar to PDO usage). Not having to deal with mysql_real_escape_string for everything seems to be a bonus.

  • Colin

    I’m a total noob so im wondering:

    In the Data_model, why do:

    function getAll() {
    $q = $this->db->get(‘data’);
    $this->db->select(‘title’, ‘contents’);
    if ($q->num_rows() > 0) {
    foreach ($q->result() as $row) {
    $data[] = $row;
    }
    return $data;
    }

    if you can just do:

    function getAll() {
    $q = $this->db->get(‘data’);
    $this->db->select(‘title’, ‘contents’);
    if ($q->num_rows() > 0) {
    return $q->result;
    }

    }

    • http://www.dragoviandesign.be Peter Vermeulen

      You’re right on the fact that in this specific case it doesn’t actually matter. However it’s best practice to do it this way, to allow for processing of data before being returned to our controller.

  • Johunn

    Thanks, these tutorials are great!

  • Andrea

    Great tutorials! I followed your PHP tuts as well and they were extremely helpful.

    Quick question: Why do you erase your php closing tag?

    • Andrea

      this was answered in the next tutorial… so thanks Jeff!

    • http://memeLab.com.au/ Tim Osborn

      its not required, and avoids problems with whitespace being output before the headers are sent

  • Hoang Phuc

    Video not working !

  • rohit

    Can you provide the how to update the particular record ….

  • Dennis

    Great, thanks!

  • http://petev.net Hristo Petev

    This is my second video that I am watching I have noticed some errors. I would like to tell them.

    1. When you retrieve that result from the database in your model. You have to set the $data array to empty array. $data = array(); in this case if there is no records return there wont be any errors duisplayed.

    2. When echo more than one variable or literal it is good concept to use commas instead of concatenations like this: echo ‘<h1>’, $r->title, ‘</h1><p>’, $r->contents, ‘</p>’; In this way the PHP will just print the information without concatenating it.

    3. You use the foreach loop so many times in the model that you should do this in a function. In this way you will save more time than just copying it. Also when it comes to support it will be easier.

    I know that this is tutorial for Codeigniter, and its realy useful for me. Thanks for this, but there are a lot of new guys to PHP and they should learn to do the things in the right way. So when we encounter their source code, one day, we will not hit walls with our heads.

  • Roberto

    Excellent, excellent, excellent!

    I owe you a beer man…maybe a six pack!

    -Roberto

  • PM

    Why you don’t use Netbeans IDE or any other IDE?

  • http://www.targetweb.gr Tw

    Thank you! These are the best codeigniter tutorials I have seen on the web!

  • Md nurul islam

    Great tutorial for the beginner.

  • Ashutosh Ajgaokar

    thank you so much :)

  • Harsain

    can we use mysql PDO with codeigniter ….
    i tried it but always throws an error undefined source

  • Charlie

    PLEASE UPLOAD YOUR VIDEO ON YOUTUBE…. We can’t load it.

  • Pratik Sawant

    Want a download link, so that i can get all the video tutorial, i would like to say this is the Best tutorial of codeigniter i have ever seen on net

    • rush@me

      you can use Internet download manager to download all the videos. :)

  • Abdelali

    Thank you so much

  • shearamariz

    What version of codeigniter did you use in this tutorial since it’s not the same anymore with the latest version. Thanks.

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

    Vacation. Going over all tutorial submissions today. :)

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

    yeah its named as CI:Dat 2

  • Nate

    What? VACATION? Good gawd. *sigh*

    *mumbles*….vacation….

    :P

  • Bhavani

    Now how to deal with this: The URI you submitted has disallowed characters.

    I did a lot of search and people were talking about downgrading to php 5.2 version. May be you have a better answer?