Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Update Voting System3:53 with Alena Holligan
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
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
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";
For the final step in our book voting system,
I want to be able to update the way the voting works.
Only authenticated users should be able to vote.
And each user only gets one vote per book, but
that user is always able to change their vote.
Let's take a look at our functions_vote file.
The first function gives us the saved vote for a specific book by a specific user.
The user ID is not required, it will default to 0.
The next function is the actual vote,
It accepts a bookId, a score, and an optional userId.
This function calls the clearVote function.
That function accepts a bookId and a userId, and
attempts to delete that user's vote.
If a vote is actually deleted,
the function will return a count greater than 0.
The vote function will count that as the user's vote and return true.
This keeps the vote to 1 point instead of jumping from positive 1 to negative 1.
For more information, see the notes associated with this video.
If no vote was cleared, then we add the user's vote to the database.
Let's update our procedures/vote.php to pass the ID of the authenticated user.
First we'll add requireAuth,
then we can get the user = getAuthenticatedUser.
And then we can pass user id.
Next, we want to update our arrows to show which way the current user has voted,
so let's open book.php.
The first thing that we'll need to do is get the authenticated user.
After we check if the user is authenticated,
we can do user = getAuthenticatedUser.
Now, inside of the class of our up arrow, we're going to add a php block.
We can add as much space as we want, so let's add some new lines.
We're going to add a conditional that checks if, getUserVote,
And we pass the book,
id, and the user id.
And then we check to see if this equals 1.
If it does, then we're going to echo, space, orange.
Make sure that we have a space before our new orange class so
that it doesn't get mixed up with our previous classes.
Let's copy this code block, And we'll add it to our down arrow.
Instead of 1, we wanna look for negative 1.
Now let's go back to the browser and view the Book List page.
We can now only make a single vote per book,
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