Codeigniter App Tutorial
Lesson 7 of 10 · Codeigniter App Part 7 – Models List CRUD · IT & Software
Codeigniter App Part 7 – Models List CRUD
alright so the authentication aspect of our application is complete now we want to move on to the task lists alright and we want to do is create a list
controller which will interact with a list model and then we'll have a couple different views for the list so the first thing I want to do is I want to add a link up here I want to go to the
list controller but only if we're logged in so I'm just going to go ahead and go into my layout my main layout and I'm gonna put this right after the home list and what we're doing here is saying if
we're logged in then show this link to the list controller and from this point on I'm gonna be copying and pasting more than I have been just because this is getting kind of long and a lot of the
stuff that we're doing yeah it has to do with what we what you've already learned so you know interacting with models and things like that so maybe copying and pasting a little more all right so let's
go ahead and login so now we have this link which goes to nothing because we don't have a list controller so let's create that under controllers new file I'm going to
save this as list on PHP and let's just try to speed things up here fast lists okay so what do we want here we want to have first of all we don't want to be able to access any of this if we're not
logged in all right so what I'm gonna do is create a constructor and that's one thing we haven't gone over yet although if you're familiar with other languages you may know what a constructor is but
it's basically something that just runs it'll run when this class runs you won't you don't need to have a special method for it to run so that's where we can create our logic for a login for
authentication so I'm going to say public function and we do this with a double underscore and then the word construct all right and what we want to do is we want to enter
our parent construct because what this is doing is calling the constructor the constructor of CI controller all right and we need that or things won't work part won't work like
they should all right so we're gonna say if this session user data logged in well we're gonna say if not logged in so I'm gonna put an exclamation mark here which means not so if they're not logged in
then we're going to set a message okay so let me just grab the syntax okay and we'll say not logged in or we'll just say no access
and they're gonna go to back to the homepage all right so this isn't gonna do anything we need to create a view all right so let's create an index method
public function index and remember index is called when you go to a controller like lists without any method without any second word or whatever all right so let's say user ID
yeah so I'm going to set the user ID to the session to the user data ID so this session user data user ID all right so we're gonna create a view
for this it's going to be under lists index so let me just paste that in all right so let's create that view so in our views folder we want to create a folder called lists
and in that list folder we want a file called index.php this is the list index all right so we're in the ListView now what we want to do now is try out the the the access so let's go to home slash
lists all right so oh I'm sorry we don't want home slash lists we just want lists all right so it just redirected us we didn't catch that that flash data there's no access so we
got to throw that in the home page and you can see you are your home view can get quite cluttered with these messages no access no access all right so let's try that
again go to lists alright sorry you're not logged in so that should that should actually be danger this class all right that's all you want so let's
log back in now that we know that that works all right in my list so what are we gonna do here what we want to do is grab all the lists which we don't have done yet so in our
database I'm just gonna manually insert a couple lists so if we go to lists and then insert list name say family stuff this list is for family basis tasks I guess I'm just making stuff up all right
so the user ID would be me let me just check my ID so under users my ID is – all right so – and then let's see the park stuff this is business
I don't know too all right so now we have two less in our database and we want to grab those and present them here so we can do that in the index controller we want to create a
data variable which will be called lists and we want to set that equal to this DB I'm sorry this list model catalysts all right so we have to create a list model so I'm just gonna be going just a little
bit faster than we were all right so our list model we have to auto load that so in our auto load file you just want to add that right here okay now let's go to our lives model and
let's see I model and what was it get list so public function get lists basically here we want to create basically we just want to get all the
lists so we'll say query equals this DV get well and inside here we want the name of the table which is lists and let's create
actually we don't need any where clauses we want to grab all of them so all we want to do here is we want to return we want to return query result and that should give us an array with the lists
all right so that's being passed to this data lists okay and then what we're adding the the data array in our view so we should now be able to access it in our view so let's see and what we want
to do is loop through them so we'll say PHP for each for each list as list what we just want to end for each all right so let's see if we can if we're
grabbing the list so in the in the middle of the loop let's just echo list and then we should be able to access all the fields for instance the title is it called title not list name so I should
be able to say this name and then we'll just add on a break all right so let's go back to the lists and reload and we get our family stuff and work stuff lists all right so now we can create our
view and we can include our lists all right so what we want to do is we want to make the view a little nicer let's put in and each one paragraph this is
I don't know what should I say I'll just say these are your current let's alright so we're gonna have we what I want to do is make each each list item an actual HTML list item so we're gonna create a
ul and give it a class of list items and it's gonna end after the for each and then in between here we want list tags take away this break all right it looks good and then we'll
just have a link let me add in a break and then a link to create a new list let's just put that in a paragraph okay so let's reload and there's our lists now what I want to
do is add a link to be able to go to that actual list page so what we're going to do actually we want the body to show there as well so actually in between these list tags we're going to
do a couple things I'm going to create a div with the class of list name and that will go there and then we want the list body this body and this should actually have
a link to the list name so in here have a link which will go to jeez URL go to BC URL list slash show which will show an individual list and then we want
the ID of the list so PHP echo list ID okay let's close that link this this button here this create new list is gonna go to this slash ad
all right let's take a look at that all right we will add a little bit of styling for this actually let's do that now I'll just throw it in so we want to go to our
assets and then CSS and then custom and I'm just gonna throw in a style just give it a border a couple things alright so that's good we have a list of lists so let's create the show method so we
can actually view these so you can see we go to lists last show and that should be slash one let me fix that right here he's a slash all right so that should be it for our index for you
next let's create a view under lists and save this as show dot PHP and that'll be our show view so we need to create a show function so show and this is going to take in an ID and that'll be the list
ID and where this comes from is the URL alright so well that's ad C so we're going list show and that's the idea of the list alright so in here I'm going to
throw in some data values after throwing the whole thing all right so we're saying list this value here is equal to we're just going to get the list and we're going to include the ideally me
I'm just gonna make this ID all right so this ID is coming from here and next we're getting the completed tasks which actually I'm not going to do this yet it's comment that out all right so
basically we're just getting the information for this particular list sending it to the show view alright so then in the show view I'm gonna paste in our actions so this
will basically just be a little box that has links to add a task to edit the list or delete the list all right and we have a little click Handler that's just gonna give us a confirm and say are you sure
when we only click delete all right so this should now list model get list all right so we need to create that sorry if I'm skipping around here this is the list controller we need to create this
list model get list and this is basically pretty easy just say get list and it's going to take an ID you're gonna say this query equals this DB get and we want to get this from
the list table and we need a where clause so I'm going to say this DB where ID is equal to that ID and then we're just going to return same thing query result and that should do it
all right what do we got here find a property of non-object with Slash show three four and five Oh list ID
now why is that not showing the list get list all right so let's ah oh I know why since we're getting one record alright we're getting one record
by the ID we don't want to return a result we want to return a row okay cuz result is for an array if you're returning an array a row is for I'm sorry
result is if you're returning more than one column all right so we got ad task edit alright let's go back to the show view and let's see I'm gonna piece some stuff in we're
gonna have a heading which will be the list name we'll have the create date in the body so let's save that all right
and I'm gonna throw some more CSS in so if we go to the custom CSS to put in these this is for the actions for these right here alright so it puts that over there
now ultimately we're gonna want our tasks for this list to show down here but we're not going to do that yet what I want to do now is I want to be able to create a new list alright so
let's go to our list controller and we're just going to paste in a new function called add which is basically stuff that we've done the form validation let's see we're loading a
view called lists slash add list which will create we're creating an array with the post data as well as the session user ID and then we're calling this create list from the list model and
they're just redirecting it so pretty pretty easy stuff that we've done so let's create that view under under lists we want to create a we want to create a new file and we'll
call it add list dot PHP okay so and let's let me just let's just test this out so if we click on that now we're in the add view now this is gonna be a form using the
forum helper I'm gonna paste in same stuff again same you know same idea we have a list nameless body that's pretty much it so save this is being sent to the hat to the list controller and the
add function which we have here so let's reload this and see if actually we need to create the model the model method which is create list alright so just copy that little
and this is taken our data array which includes log post values and this is going to be pretty quick we're just going to create an insert variable it will say this DB insert data and then
we're just going to return that variable all right so let's reload this and we'll add a new list let's say I don't know we'll say sports all sporting events okay add list all
right so again you must use the set method to update an entry not sure why that happened let me see insert this TV insert oh we don't have the
table name then I put lists all right and we also need a message we have this list created message which we want to show in the list index view which is here and I actually have quite
a few messages that I want to paste in here um do you don't these don't matter right now the only one that matters is the created but we're also gonna delete and update a list so put those in there
now and let's go back to my lists create a new one say sports all right your task your task list has been created and there it is alright so now we can view we can select all our lists we can add
them now we want to be able to update them so if we click on this we want to click on edit list and go to the the Edit control method so if we go to lists and I'm going to paste in the edit
method okay so we have edit and we have to pass an ID a list ID form validation and here this date of this list we're going to grab all of the the list data for this
particular list by ID all right and then same thing down here we're just creating a data array and then we're submitting it through the model through edit list alright so now we got to go to our model
just go to our model and I'm gonna say public function edit list and any time you have an edit anything you need to pass in an ID alright so we're going to pass in a list ID and we want to pass in
the data array as well and this what we say in this TV where we want to see you say where ID is equal to list ID query this DB yet and then the table
name which is lists okay and then we just want to return the query the row all right so that's that at at list now that's the Edit function now we want to get the we want to get
the list data from the model as well so let's go back to list model and you know what I did this wrong this is actually shoot let me just get rid of this I put the
wrong function in there let me just grab it all right so if saying we're ID equals list ID we want to update will do this DB update and we're passing in the data
array and we're returning true now let's paste in the get list data which is basically what we just did up here it's just returning the data from a single list all right and the last thing
to do is to create a view and edit view so I know the ListView we want a new file save it as edit list dot PHP and I'm gonna paste in a form okay same thing that we've been doing and let's
try that out so I'll go to edit lists let's say family stuff we'll say it's that a base task we'll say stuff okay it's been updated and you can see that it's changed to this family stuff
alright last thing delete we want to be able to delete the list so in our controller we need a delete function and it's basically calling delete list in the model okay so I'll go to the list
model and we'll paste in delete list all right so that should do it let's reload this in delete list are you sure alright so called two undefined method delete tasks
of list all right so what this function that we don't have does is it deletes all the tasks that are associated with this list and we haven't done tasks yet so let's just hold off on that alright
so that looks good basically the lists are done it shows on our home page actually no they don't that's what we need to do we need to go to the home view and I'm just gonna paste in a table
that'll show all our lists and let me see reload okay so it's not getting that the lists and that's probably because this isn't set this lists so if we go to
our home controller we just want to say if we're if we're logged in then get the list all right so if session data logged in then we're grabbing the user ID and then we're
getting all the lists from that user it will also get the tasks but we'll hold off on that all right so we just need to create this get all lists in the model in the list model I guess you could also
put it in the home controller if you wanted all right so paste that in same thing we're just getting it with the where clause with the user ID and we're saying
this DV order by we want to order it by the create date and ascending okay so that's how that works and there's our lists for view all right so our lists are basically
done next we'll be creating our task functionality
We will use our list model to perform Create, Update, Read & Delete on the lists table using active record
The full source code is at – https://github.com/bradtraversy/mytodo_ci