Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
We will set the database to use a vote table which accepts a book id and a user id for the vote instead of adding a column to the book table for score. This will allow more control over user voting.
-
0:00
Next up is the actual procedure for the voting.
-
0:03
To do this, let's create a single file called vote.php in the procedures folder.
-
0:18
We need to start with our bootstrap file and pull a couple values.
-
0:38
$vote is going to = request()->get('vote').
-
0:50
And then our $bookId = request()->get('bookId').
-
1:01
Next we're going to use a switch statement for
-
1:03
stating which direction we're going to vote.
-
1:20
Case 'up', We're
-
1:25
going to vote($bookId, 1).
-
1:35
Case 'down', we're going to vote($bookId, -1).
-
1:50
And finally, we can redirect to the book's page.
-
1:56
$response =
-
1:59
\Symfony\Component\HttpFoundation\Respons-
-
2:07
e::create(null,
-
2:22
\Symfony\Component\HttpFoundation\Respon- se::
-
2:36
HTTP_FOUND.
-
2:40
Then our location.
-
2:46
'/books.php'.
-
2:52
$response->send.
-
2:55
And then exit.
-
2:57
Now, let's give this a try in the browser.
-
3:01
Those arrows aren't in the right place, let's go take a look.
-
3:07
Sure enough, I forgot a div.
-
3:16
"media-left".
-
3:29
Great, now let's try the vote.
-
3:34
Awesome, our book voting system works.
-
3:37
We'll extend this as we move forward with our authentication system,
-
3:41
allowing a single user to vote only once for a book.
You need to sign up for Treehouse in order to download course files.
Sign up