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