Developing a Website with CodeIgniter Part 2: Users Database Table and the User Model
Posted on 06. Sep, 2009 by Shawn McCool in PHP, Screencasts, Web Development
This screencast continues a series with the goal of documenting the development of a functionally complete site using PHP with the Code Igniter framework and jQuery for Javascript UI improvements including AJAX interactions.
This video covers the creation of a database table for storing users and the code igniter model class for interacting with it.
Errata:
I’ve noticed a few errors in the screencast. When errors creep up (and they will) I’ll audit them as they’re discovered and keep this post up to date. Errors will also be updated during the following screencast.
- The UpdateUser method “set password” line (line 80) contains the variable $options['userEmail'] instead of $options['userPassword'].
- The UpdateUser method is missing the line $this->db->where(‘userId’, $options['userId']); This should be added before the line that contains $query = $this->db->update(‘users’); (line 84)
- The AddUser method’s default value is set incorrectly (line 48). It currently shows array(‘userStatus’, ‘active’) where it should read array(‘userStatus’ => ‘active’).
Related Posts
- How to Write a Better Model in CodeIgniter
- Developing a Website with CodeIgniter Part 4: Admin Site CRUD
- Developing a Website with CodeIgniter Part 3: The Login Process
- Developing a Website with CodeIgniter Part 1: Development Environment and Framework Configuration
- Using CodeIgniter’s Active Record Class to Create Subqueries
Share this Post
Show Your Appreciation
If you'd like to express your appreciation for the work I've done why not buy me a coffee?Click here to donate $3 to my caffeine fund.


Which software do you use to record your tutorials? Isn’t it Camtasia Studio?
Yes, Camtasia Studio.
nice tutorials, thanks you , Shwan!
This is a great tutorial. Much more professional code than any of the other CI membership tutorials I’ve found yet. I like your programming style, very similar to my own.
I’m looking forward to the next episode in this series.
Very good tutorial and presentation style…. Thanks Shawn.
Looking forward for your next screencast
Hey Shawn,
Great tutorial. There aren’t many decent CodeIgniter screencasts around, so I was pleased to find yours.
Perhaps an idea for a future tutorial/screencast… i’m interested to know how a simple form such as a newsletter signup/site login could be included on multiple pages without needing to go to separate pages to display error or success messages.
Thanks,
Neil
Hey Shawn, excellent tutorial.
What’s php ide do you use in this tutorial?
Thanks in advance,
Jony
I use Zend Studio 7. In the videos I am using Zend Studio 5.5.
Shawn,
Thanks much for the tutorial, I’m just getting into PHP and using Code Igniter so it’s great that people like you take the time to create these screencasts. I am curious, however, do you provide the code from the screencast anywhere or should I just take the video really slow and copy it?
Thanks
Kris
I really should provide the code, shouldn’t I? I’ll upload the code for part 3, and then upload the code for every subsequent part. Thanks for the feedback. If you can think of anything else that’d help me make these screencasts better please let me know!
Thanks for the great tutorials. Would you mind also posting the code from this video. I am just beginning to learn PHP and Codeigniter and am fairly certain that there is something wrong with my code.
Thanks,
Sam
@Sam
Yes, I should have been doing this from the beginning. I’m going to include the code in the “part 3″ post soon, and continue to include it for every post after that.
@ Shawn
I didn’t notice that KRD already asked for the code. didn’t mean to be a pain thanks for the response
@Sam hey man, no problem. Comments on blogs are hard to follow. I’m thinking about ditching WordPress in favor of maybe Expression Engine 2.0 so that I can really make the site more fit my vision. Unfortunately, that would mean that I’d need to put in quite a bit of work that would result in fewer new tutorials. So, instead I’ll make do with what I have.
Hi Shawn,
Thanks for the screencast, It’s great to hear that you also work with ExpressionEngine. If you have some time I would like to see some introduction on how integrating CodeIgniter to ExpressionEngine like maybe making some small application, like read and write to database.
Thanks
Gery
@Gerry
Actually, I just started using Expression Engine. My first experience with it is the EE 2.0 public beta. I’m still learning and am a good bit away from having best practice worthy experience with it.
I can definitely see that people are interested in learning about EE / CI integration. I myself am interested in it, as well.
well… great resource here, but I just typed out a lengthy response/question and it told me I was spammy. I hit the back button and my response is not here.
Quick version: I can’t get your GetUsers method to work.
http://60dayflip.com/main/listing2/
http://pastie.org/796499
It works here: http://60dayflip.com/main/listing/
…using the following GetUsers method.
function GetUsers()
{
return $this->db->get(‘users’);
}
@Jason
Perhaps you can show the “new” GetUsers() method source?
Shawn, it is the same as listing2, but swapped out User_Model for MUser.
I’ve updated the pastie link with both.
http://pastie.org/796499
I’m asking about the code that actually gives you the error. The table library line 269. Can you post the method of which line 269 is a part?
That’s the CodeIgniter method. System/libraries/Table.php
@Jason
Oh, ok right… Ok.. so what’s happening here is GetUsers is returning an array of Objects. You need to format this into an array or create a custom option for GetUsers to return $result_array().
Basically, get back the data from GetUsers, then loop through it and format it into a new array so that you have more control over the actual layout.
Alternatively you can create a parameter $options['returnFormat'] = ‘array’;
if(isset($options['returnFormat']) && $options['returnFormat'] == ‘array’) return $query->result_array();
You have a lot of options based on what your needs are.
Do a print_r() on the results from GetUsers. It shows that it is returning objects. You’re directly passing them to the table library, this is your problem.
Thanks! I’m new to OOP PHP, and really only know enough PHP to be dangerous, lol. But I’m developing a new membership site with unique accounts and I didn’t want to do another procedural app like I’ve done in the past.
I’ll work on your suggestions once the Apple presentation is over.
Hey shawn, forgive the noob question but what IDE are you using in this video?
@Xero
I’m using Zend Studio 7 in these videos. I’ve recently switched to NuSphere PHPed.