User Administration Panel5:18 with Alena Holligan
Here we will create an administration panel that displays all the users and allows their role to be changed.
<div class="panel"> <h4>Users</h4> <table class="table table-striped"> <thead> <tr> <th>Username</th> <th>Registered</th> <th>Promote/Demote</th> </tr> </thead> <tbody> </tbody> </table> </div>
Before we add and edit the books themselves, 0:00 we're going to want to allow administrators to edit all the books. 0:03 So we need a way to set administrators. 0:07 We'll want to make sure that an administrator is able to promote 0:10 other users to an administrator as well. 0:14 We'll need to add this functionality in two steps. 0:17 First, we need to create a user list with promote and demote buttons. 0:21 When we have those wired up, we'll promote ourselves to an administrator. 0:27 After that, 0:31 we'll lock down the administrator panel to only administrators. 0:32 Let's create a new file, 0:37 we'll name this admin.php. 0:41 We're going to copy everything from add.php as our base. 0:46 Now we can make our changes. 0:53 We'll change our header to be Admin, and we'll remove the rest of the content. 0:55 We're going to add a new panel with a user table. 1:04 You can grab this code from the notes attached to this video. 1:08 Within our table body, we want to loop through each user and 1:18 display the correct information, 1:23 along with a button to either promote or demote that user. 1:26 There are two functions we'll be using in the functions_users file, 1:31 so let's take a look. 1:36 The first function is named getAllUsers, and 1:37 it simply returns an array of all the users in the database. 1:41 The other function we'll be using is named changeRole, 1:45 it accepts the userId and a roleId. 1:50 It then updates the database with these new values and returns an updated user. 1:54 Let's go back to admin. 2:00 We'll start with our loop. 2:03 Foreach, getAllUsers() as $user. 2:06 We're going to add a row, And 2:20 then add our table cells, 2:27 echo $user['username']. 2:31 And then our created_at date. 2:42 And then we're going to add our button. 2:48 And then we end our foreach. 2:55 Now we're ready to set up our buttons. 3:00 I want our button to show the action that can be done, so 3:03 we will add a little bit of logic around it. 3:06 First for our conditional, 3:12 if the $user['role_id'] == 1, 3:17 And then we'll add our endif. 3:27 If our role_id == 1, 3:33 then we're going to add a link to /procedures, 3:36 /adjustRole.php. 3:44 And we're going to pass the roleId, 3:50 which will equal 2, and the userId, 3:54 which is going to equal echo $user['id']. 3:59 And then we add a class = 4:07 "btn btn-xs btn-warning". 4:13 And this will be Demote to General User. 4:22 Let's copy this line, and then we're going to add an else. 4:30 Elseif our $user['role_id'] == 2, 4:37 Then we're going to change our roleId to 1. 4:53 And we're going to be promoting, Promote to Admin. 4:58 We'll also change this styling on the button. 5:06 This is going to be success. 5:10 Now to make these buttons work, we're going to need a new procedure. 5:13
You need to sign up for Treehouse in order to download course files.Sign up