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
Let’s make some updates to our voting system to make sure a user can have only a single vote registered per book. We are going to update our book list as well to show the way the user voted by changing the arrow color.
Other samples for score
Ternary Operator:
echo $book['score']; ?: echo '0';
PHP 7: null coalesce operator
echo $book['score'] :: echo '0';
Breaking Down the Query
Let's break down this query now that it has become more complex.
First we select all columns from the books table and get the sum total of votes for a book and saving that column as score
SELECT books.*, sum(votes.value) as score, "
The main table used in the select.
. " FROM books "
To get the score used in the select, we do a LEFT JOIN of the votes table where the id of the current book equals the book_id's from the votes table.
. " LEFT JOIN votes ON (books.id = votes.book_id) "
Then, we group everything by the book id so there is only 1 row per book
. " GROUP BY books.id "
Finally we order the books by the score DESC so the highest voted book is at the top and the lowest scored book is at the bottom.
. " ORDER BY score DESC";
-
0:00
For the final step in our book voting system,
-
0:03
I want to be able to update the way the voting works.
-
0:07
Only authenticated users should be able to vote.
-
0:11
And each user only gets one vote per book, but
-
0:14
that user is always able to change their vote.
-
0:18
Let's take a look at our functions_vote file.
-
0:22
The first function gives us the saved vote for a specific book by a specific user.
-
0:29
The user ID is not required, it will default to 0.
-
0:33
The next function is the actual vote,
-
0:39
It accepts a bookId, a score, and an optional userId.
-
0:46
This function calls the clearVote function.
-
0:52
That function accepts a bookId and a userId, and
-
0:56
attempts to delete that user's vote.
-
1:00
If a vote is actually deleted,
-
1:03
the function will return a count greater than 0.
-
1:08
The vote function will count that as the user's vote and return true.
-
1:14
This keeps the vote to 1 point instead of jumping from positive 1 to negative 1.
-
1:20
For more information, see the notes associated with this video.
-
1:24
If no vote was cleared, then we add the user's vote to the database.
-
1:31
Let's update our procedures/vote.php to pass the ID of the authenticated user.
-
1:41
First we'll add requireAuth,
-
1:45
then we can get the user = getAuthenticatedUser.
-
1:58
And then we can pass user id.
-
2:03
Next, we want to update our arrows to show which way the current user has voted,
-
2:10
so let's open book.php.
-
2:13
The first thing that we'll need to do is get the authenticated user.
-
2:18
After we check if the user is authenticated,
-
2:23
we can do user = getAuthenticatedUser.
-
2:29
Now, inside of the class of our up arrow, we're going to add a php block.
-
2:40
We can add as much space as we want, so let's add some new lines.
-
2:45
We're going to add a conditional that checks if, getUserVote,
-
2:55
And we pass the book,
-
2:59
id, and the user id.
-
3:05
And then we check to see if this equals 1.
-
3:09
If it does, then we're going to echo, space, orange.
-
3:17
Make sure that we have a space before our new orange class so
-
3:20
that it doesn't get mixed up with our previous classes.
-
3:24
Let's copy this code block, And we'll add it to our down arrow.
-
3:33
Instead of 1, we wanna look for negative 1.
-
3:37
Now let's go back to the browser and view the Book List page.
-
3:44
We can now only make a single vote per book,
-
3:48
either we have no vote, one vote up, or one vote down.
You need to sign up for Treehouse in order to download course files.
Sign up