Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Here we will create an administration panel that displays all the users and allows their role to be changed.
This video doesn't have any notes.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Now that we have some guards set
up we'll need to have a way for
0:01
an administrator to promote
other users to an administrator.
0:03
We'll need to add this
functionality into steps.
0:07
First we need to create a user list
with promote and demote buttons.
0:11
When we have those wired up will
promote ourselves to an administrator.
0:16
After that we'll lock down
the administrator panel
0:21
to only administrators.
0:23
First we need to get all of our users so
that we can loop through them.
0:26
In the functions.php file
create a new function.
0:29
We'll call this getAllUsers.
0:34
We'll start with our global db,
and our try catch block.
0:42
For our query we're going
to SELECT all FROM users.
1:05
We prepare our query and
1:18
then execute.
1:23
Now we can return a fetchAll.
1:27
We want to make sure
that this is returned as
1:36
an associative array, so
we do PDO::FETCH_ASSOC.
1:41
Now let's create a new
file named admin.php.
1:47
We'll copy add.php for the base.
1:54
Change the header.
2:01
We'll keep the display messages, but
2:04
we'll change the rest of
the content on this page.
2:06
Start with a div,
2:15
with a class name panel.
2:19
We'll add a secondary header named Users.
2:24
And then a table.
2:30
For headers we'll use Email.
2:50
Registered.
2:55
And Promote or Demote.
2:59
For our table body,
we can loop through each user and
3:09
display the correct information.
3:12
For each, getAllUsers.
3:21
As user.
3:25
And endforeach.
3:37
Echo user email.
3:48
And then created_at.
4:01
And finally we'll add our button.
4:08
I want our button to show only
the action that can be done.
4:09
So we'll add a little
bit of logic around it.
4:14
If the user is already an administrator
we should show a Demote button to make
4:16
the user a normal user and vice versa.
4:21
If the user's role_id == 1,
meaning they're an administrator.
4:33
We're going to demote and
also pass the userId.
4:51
Demote from Admin.
5:22
Let's duplicate this line.
5:26
Then we can add our else.
5:29
ElseiIf(, $user["role_id] == 2)
5:34
Then we're going to promote.
5:48
Promote to Admin.
5:57
And let's end our if.
6:09
For these buttons to work,
we need a new procedure.
6:13
So let's create a new file in our
procedures called adjustRole.php.
6:16
We start with our
bootstrap file as always.
6:27
And then we need to pull two bits
of information, the user ID.
6:39
And the role.
6:51
Once we have both of these set, we need to
run a switch statement based on that role.
7:01
Anytime you run a switch statement, it
can be a good idea to cast the string to
7:08
a lower case to prevent any
issues with being case sensitive.
7:13
If this is promote,
then we're going to use
7:28
a promote function and pass the userId.
7:32
We'll add a success message.
7:42
Promoted to Admin!
7:53
And then break.
7:59
For demote.
8:07
We'll past the demote function and
we'll change our message.
8:11
After our switch statement
we'll redirect to admin.
8:23
Finally we need to create the promote and
8:32
demote function to trigger when
the user is promoted or demoted.
8:35
Both of these functions
simply do a database update.
8:39
We start with our global db,
and our try catch block.
9:01
For our query, we'll update users.
9:24
And set the role_id = 1,
9:29
where the id will equal
the path in userId.
9:32
Prepare a statement and
bind our variables.
9:49
And finally, execute.
10:07
Let's copy this function for demote.
10:11
And we simply change our role_id = 2.
10:21
Great, everything is wired up.
10:25
All we need is a link to our admin page.
10:27
Now let's visit our site in a browser and
promote ourselves to an administrator.
10:46
Great job.
11:02
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up