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


Add Comment

Discussion 161 Comments

Comment Page 3 of 3 1 2 3
  1. Jubic Wong says:

    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.

  2. alex says:

    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?

    • Ammon says:

      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.

  3. Spawn says:

    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

  4. Jake says:

    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.

    • Gabriel says:

      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.

  5. Linda says:

    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.

  6. horhe normanito says:

    Thank You!!! :D

  7. BG says:

    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?

  8. Robo says:

    Another great tutorial. Thanks for sharing : )

  9. sarmen says:

    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.

  10. abdumoulana says:

    hi guys…..

  11. Carmen says:

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

  12. Shairyar says:

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

  13. alozuldo says:

    Great tutorial!!!!!!! Thanks

  14. Ken Titzel says:

    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?

  15. Jeffrey Way says:
    Author

    Vacation. Going over all tutorial submissions today. :)

  16. Yoosuf says:

    yeah its named as CI:Dat 2

  17. Nate says:

    What? VACATION? Good gawd. *sigh*

    *mumbles*….vacation….

    :P

  18. Bhavani says:

    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?

Comment Page 3 of 3 1 2 3

Add a Comment

To add a code snippet to your comment, please wrap your code like so: <pre name="code" class="html">YOUR CODE</pre>. You can replace the class name with "js," "css," "sql," or "php." If there are any "<" or ">" within your code, please search and replace them with: &lt; and &gt; respectively.