Jump to content

5 star rating system - need quick answer


thenext88

Recommended Posts

Ok, so I took a look at the tutorial on how to make a 5 star rating system.

My only problem is, is there way I can modify the scripts, so I can have multiple forms from multiple pages all linking to one. I haven't tested anything out yet, because I haven't created any database yet for this information.

But would this be the proper way to make all these forms before I go creating all these?

[code]<?php

function ratemenu($tut_id) {
    [b]$tut_id = 1; // This is what I added[/b]
    echo "<form name='rating' method='post' action='mysqltester.php'>
    <font>Rate this Guide:</font>
    <select name='rating'>
    <option value='5.0' selected>5 - Excellent</option>
    <option value='4.0'>4 - Pretty Good</option>
    <option value='3.0'>3 - Fair</option>
    <option value='2.0'>2 - Poor</option>
    <option value='1.0'>1 - Waste of Time</option>
    <input type='hidden' name='cmd' value='do_rating'>
    <input type='hidden' name='tut_id' value='$tut_id'>
    <input type='submit' value='Go!'>
    </select>
    </form>";
}

ratemenu($tut_id);

?>[/code]

and then this get's passed on to another page, which has this in the code:
[code]$get_count = mysql_query("SELECT tut_rating, tut_num_votes FROM tutorials WHERE tut_id=$tut_id");[/code]

and where tut_id=$tut_id is where I got the idea to inset a number up in the form specifying what tut_id would be. And that way, each form would be exactly the same, all I would have to do is change $tut_id to a different number.

Would that work?
Link to comment
https://forums.phpfreaks.com/topic/33517-5-star-rating-system-need-quick-answer/
Share on other sites

[code]<?php

include('config.php');

mysql_connect("$host","$user","$pass");
mysql_select_db("iratings") or die(mysql_error());

function do_rating($item_id, $rating){
    if (session_is_registered("rating$item_id")){
        echo "<center>Sorry! You have already voted!";
    } else {
        $result = mysql_query("SELECT item_rating, num_votes FROM ratings WHERE item_id=$item_id");
        while($row = mysql_fetch_array($result)){
            $new_count = ($row['num_votes'] + 1);
            $item_rating2 = ($row['item_rating'] * $row['num_votes']);
            $new_rating = (($rating + $item_rating2) / ($new_count));
            $new_rating2 = number_format($new_rating, 2, '.', '');
            $update_rating = mysql_query("UPDATE table SET item_rating='$new_rating2',num_votes='$new_count' WHERE item_id=$item_id");
            $sessionvar = "item$item_id";
            session_register($sessionvar);
           
            echo "<div align='center'><b>
            <p>Thanks for your vote!</p>
            <p>The new rating for this guide is: <br>
            <br>
            $new_rating2 out of 5</p>";
        }

    }
    echo "<p><a href='/...'>Go Back</a></p>";
}

do_rating($item_id, $rating);

?>[/code]

Form is the same, except all the "tut" is just changed to "item"

The echo part near the bottom shows up, but line 13 is the error. (what was originally in the script on line 13 from the tutorial and beyond had the same error, this was just slightly modified in attempt to maybe fix it - but still gave the same error)

And the database was created with the following columns: item_id, num_votes, item_rating (and I created the first row with values 1,0,0)
You were right about me not declaring variables. Also, the error came because I specifying a column in my select, and not a row, so I have that fixed.

My only problem now is, the update the database part is not working. I did some debugging and all the variables are correct, but these variables are not being written into my database. I've never used the "update table" command before, so I hope mine is incorrect, and hopefully it's an easy fix there.

[code]<?php

include('config.php');

mysql_connect("$host","$user","$pass");
mysql_select_db("iratings") or die(mysql_error());

$item_id=$_POST['item_id'];
$rating=$_POST['rating'];

echo $item_id; //Test Pass
echo "<br>";
echo $rating; //Test Rating Pass
echo "<br>";

function do_rating($item_id, $rating) {
    if (session_is_registered("rating$item_id")){
        echo "<center>Sorry! You have already voted!";
    } else {
        $result = mysql_query("SELECT * FROM ratings WHERE item_id='$item_id'");
        echo $result;
        echo "<br>";
        while($row = mysql_fetch_array($result)) {
            $new_count = ($row['num_votes'] + 1);
    echo $new_count;
            echo "<br>";
            $item_rating2 = ($row['item_rating'] * $row['num_votes']);
    echo $item_rating2;
            echo "<br>";
            $new_rating = (($rating + $item_rating2) / ($new_count));
    echo $new_rating;
            echo "<br>";
            $new_rating2 = number_format($new_rating, 2, '.', '');
    echo $new_rating2;
            echo "<br>";
            $update_rating = mysql_query("UPDATE table SET item_rating='$new_rating2',num_votes='$new_count' WHERE item_id='$item_id'"); //Not working here
            $sessionvar = "item$item_id";
            session_register($sessionvar);
           
            echo "<div align='center'><b>
            <p>Thanks for your vote!</p>
            <p>The new rating for this guide is: <br>
            <br>
            $new_rating2 out of 5</p>";
      }

    }
    echo "<p align='center'><a href='javascript:history.back();'>&lt;&lt;     
    Back</a> | <a href='/index.php'>Main Page</a></b><br>
    </p>";
}

do_rating($item_id, $rating);

?>[/code]
[quote author=thorpe link=topic=121700.msg500958#msg500958 date=1168390956]
Hehe... your table is called [i]ratings[/i] not [i]table[/i].

[code=php:0]
"UPDATE ratings SET...."
[/code]

We all make em. :)
[/quote]

lol.. :-[

Thanks a lot for the help, it's working now. ;D

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.