• We just launched and are currently in beta. Join us as we build and grow the community.

PHP/MySQLi Creating a Forum - Part 5 - Adding Ratings

mpking

Codebase Historian
M Rep
0
0
0
Rep
0
M Vouches
0
0
0
Vouches
0
Posts
63
Likes
92
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
Introduction:
This tutorial will be continuing my series of creating a forum in PHP/MySQLi/HTML. In this fifth part, we will be adding ratings to the threads, I am going to use simple "1 Star" etc. text in order for the user to rate the threads, but you can use images (a star for example).

Pre-creation:
First you will need a host for your PHP, either a web host or localhost is fine but you will need PHP and MySQL(i) capabilities.

Also, this will not be covering creating users, or styling the pages. For the purpose of using the logged in users username, we will be using $_SESSION['username']; from my login script, you can find that tutorial on my profile page.

Obviously you will also need to go through the first, second, third and fourth parts of this tutorial series which can all be found on my profile tracking page.

Thread Page Editing:
First lets edit our threadPage.php by adding some options just above our title of our thread (at the top of the page) for the user to click on and add a rating...

  1. <div>
  2. <h1>Rate Thread:</h1>
  3. <a href=<?php

    echo

    'rateThread.php?tid='

    .

    $_GET

    [

    'tid'

    ]

    .

    '&rating=1'

    ;

    ?>

    <p>1 Star</p></a>
  4. <a href=<?php

    echo

    'rateThread.php?tid='

    .

    $_GET

    [

    'tid'

    ]

    .

    '&rating=2'

    ;

    ?>

    <p>2 Stars</p></a>
  5. <a href=<?php

    echo

    'rateThread.php?tid='

    .

    $_GET

    [

    'tid'

    ]

    .

    '&rating=3'

    ;

    ?>

    <p>3 Stars</p></a>
  6. <a href=<?php

    echo

    'rateThread.php?tid='

    .

    $_GET

    [

    'tid'

    ]

    .

    '&rating=4'

    ;

    ?>

    <p>4 Stars</p></a>
  7. <a href=<?php

    echo

    'rateThread.php?tid='

    .

    $_GET

    [

    'tid'

    ]

    .

    '&rating=5'

    ;

    ?>

    <p>5 Stars</p></a>
  8. </div>

Each option has a link to a rateThread.php page which we will need to create now. As you can see, it sends the ThreadID (tid) and the amount of stars the user has rated the thread as, we will get the information through the URL once we are on the page.

rateThread.php:
Now we need to add some code in to our rateThread.php page. We are simply going to check if there is a tid and rating set first. If they are both set we will ensure the rating is between 1 and 5 (the user could of edited it to send a modified rating value), if it is above 5 we will set it to 5 and if it is less than 1 we will set it to 1. Finally we get the relevant information and append to the values (1 to the total ratings submitted, and the selected rating on to the total ratings) and insert them in to two new columns within our threads table (we create those in the next step)...

  1. <?php
  2. session_start

    (

    )

    ;
  3. $con

    =

    mysqli_connect

    (

    'localhost'

    ,

    'root'

    ,

    ''

    ,

    'forumTutorial'

    )

    or die

    (

    mysql_error

    (

    )

    )

    ;
  4. if

    (

    isSet

    (

    $_GET

    [

    'tid'

    ]

    )

    &&

    isSet

    (

    $_GET

    [

    'rating'

    ]

    )

    )

    {
  5. $id

    =

    $_GET

    [

    'tid'

    ]

    ;
  6. $rating

    =

    $_GET

    [

    'rating'

    ]

    ;
  7. if

    (

    $rating

    >

    5

    )
  8. $rating

    =

    5

    ;
  9. if

    (

    $rating

    <

    1

    )
  10. $rating

    =

    1

    ;
  11. $qu

    =

    mysqli_query

    (

    $con

    ,

    "SELECT * FROM `threads` WHERE `id`='$id

    '"

    )

    or die

    (

    mysql_error

    (

    )

    )

    ;
  12. if

    (

    mysqli_num_rows

    (

    $qu

    )

    >

    0

    )

    {
  13. $info

    =

    mysqli_fetch_array

    (

    $qu

    )

    or die

    (

    mysql_error

    (

    )

    )

    ;
  14. $newRatings

    =

    $info

    [

    'totalRatings'

    ]

    +

    1

    ;
  15. $newTotal

    =

    $info

    [

    'rating'

    ]

    +

    $rating

    ;
  16. $q

    =

    mysqli_query

    (

    $con

    ,

    "UPDATE `threads` SET `rating`='$newTotal

    ', `totalRatings`='$newRatings

    ' WHERE `id`='$id

    '"

    )

    or die

    (

    mysql_error

    (

    )

    )

    ;
  17. if

    (

    $q

    )

    {
  18. echo

    'Updated ratings.'

    ;
  19. }

    else
  20. echo

    'Failed updating ratings.'

    ;
  21. }
  22. }

    else
  23. echo

    'Information not set correctly.'

    ;
  24. ?>
  25. <html>
  26. <head></head>
  27. <body>
  28. <a href='forumTutorial.php'>Return to thread list.</a>
  29. </body>
  30. </html>

We also add a simple link to the page so the user can return to the main threads list .php page.

Showing The Average Ratings For The Threads:
Finally we want to display the average rating for the thread on the threadPage.php. So; we are going to get the total ratings submitted value and cumulative ratings value from our table columns and divide the total ratings by the total submitted ratings, and display it...

  1. <h3><?php
  2. $qq

    =

    mysqli_query

    (

    $con

    ,

    "SELECT * FROM `threads` WHERE `id`='$id

    '"

    )

    ;
  3. if

    (

    mysqli_num_rows

    (

    $qq

    )

    >

    0

    )

    {
  4. $info

    =

    mysqli_fetch_array

    (

    $qq

    )

    ;
  5. $all

    =

    $info

    [

    'rating'

    ]

    ;
  6. $total

    =

    $info

    [

    'totalRatings'

    ]

    ;
  7. if

    (

    $all

    ==

    0

    ||

    $all

    ==

    null

    ||

    $total

    ==

    0

    ||

    $total

    ==

    null

    )

    {
  8. echo

    'No ratings have yet been given for this thread.'

    ;
  9. }

    else

    {
  10. $average

    =

    $all

    /

    $total

    ;
  11. echo

    'Average Rating: '

    .

    $average

    ;
  12. }
  13. }
  14. ?>

    </h3>

(The above code was added by me above the title of the thread.)

ForumTutorial Thread List Page Edit:
Finally we need to edit the insert query on our create thread form action in order to get it to work with the two new columns for the ratings. Simply replace...

  1. $q

    =

    mysqli_query

    (

    $con

    ,

    "INSERT INTO `threads` VALUES ('', '$title

    ', '$description

    ', '$user

    ')"

    )

    or die

    (

    mysql_error

    (

    )

    )

    ;

With:

  1. $q

    =

    mysqli_query

    (

    $con

    ,

    "INSERT INTO `threads` VALUES ('', '$title

    ', '$description

    ', '$user

    ', '0', '0')"

    )

    or die

    (

    mysql_error

    (

    )

    )

    ;

 

452,292

323,526

323,535

Top