hi everyone, just wondering if there was anywhere online that I could find a decent 5 star rating system with database integration. I found several online but the explanations of some of them aren't all that great. any help would be great. Thanks.

    i don't know any, but you need three things when doing that...

    1. A unique ID for each page (like the... URL, or $_SERVER['SCRIPT_NAME'])
    2. Total sum; the sum of each vote... example : 1 star + 3 star + 5 star = 1 + 3 + 5 = 9 stars
    3. The number of votes counted... example (from above) three votes...

    Then, when showing the result : get values #2 and #3 where #1 is the actual page... Then, divide #2 by #3, so Current_Stars = #2 / #3 = 9 / 3 = 3 stars...

      suntra, you are the man...always saving me haha...

      so i have to create a form that on submit writes to a php file that will insert the rating into the database thats obviously my basic setup....
      in terms of the table in the database... it should have the unique id, the total sum, and total number of votes counted?

        Yes... something like this...

        $strID = '/vote.php'; // this is just an example...
        $intRating = 5; // this is the rating... 1 to 5
        $resQuery = mysql_query('UPDATE rating_table SET Sum=Sum+' . $intRating . ', Total=Total+1 WHERE ID=\'' . mysql_real_escape_string($strID) . '\'');
        

          thanks, i'm trying to put it all together now...another question ...how can i post the value from my drop down menu as a variable to the next page? i want it be posted onsubmit...

          here is the form...

          <form name="rating" method="post" action="tutorials.php">
              <font class="subfont">Rate this Tutorial:</font> 
              <select name="rating">
              <option value="5.0" selected>5 - Excellent!</option>
              <option value="4.0">4</option>
              <option value="3.0">3 - Fair</option>
              <option value="2.0">2</option>
              <option value="1.0">1 - Poor</option>
              <option value="0.0">0 - Awful!</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>

          i guess i want it to so that in tutorials.php, i can just use a $POST[];
          to get the option that was chosen and set $example=$
          POST
          so that i can then submit $example to the database as the selected value...

            i found a site that has a tutorial...... and this is the code...

            function ratemenu($tut_id){
                echo "<form name="rating" method="post" action="tutorials.php">
                <font class="subfont">Rate this Tutorial:</font> 
                <select name="rating">
                <option value="5.0" selected>5 - Excellent!</option>
                <option value="4.0">4</option>
                <option value="3.0">3 - Fair</option>
                <option value="2.0">2</option>
                <option value="1.0">1 - Poor</option>
                <option value="0.0">0 - Awful!</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>";
            }

            the next peice of code is this

            function do_rating($tut_id, $rating){
                if (session_is_registered("rating$tut_id")){
                    echo "<center>Sorry! You have already voted!";
                } else {
                    $get_count = mysql_query("SELECT tut_rating, tut_num_votes FROM tutorials WHERE tut_id=$tut_id");
                    while(list($tut_rating, $tut_num_votes)=mysql_fetch_array($get_count)){
                        $new_count = ($tut_num_votes + 1);
                        $tut_rating2 = ($tut_rating * $tut_num_votes);
                        $new_rating = (($rating + $tut_rating2) / ($new_count));
                        $new_rating2 = number_format($new_rating, 2, '.', '');
                        $update_rating = mysql_query("UPDATE table SET tut_rating='$new_rating2',tut_num_votes='$new_count' WHERE tut_id=$tut_id");
                        $sessionvar = "tut$tut_id";
                        session_register($sessionvar);
            
                    echo "<div align="center"><b>
                    <p>Thanks for your vote!</p>
                    <p>The new rating for this tutorial 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> | <a href="/tutorials.php">Tutorial 
            Index&gt;&gt;</a></b><br>
            </p>";
            } 

            the final code is this...

            function tut_stars($tut_rating){
                if((($tut_rating >= 0)or($tut_rating == 0)) && ($tut_rating <= 0.50)){
                    echo "<img src="/images/stars/0o5.gif" width="70" height="18">";
                }
                if((($tut_rating >= 0.50)or($tut_rating == 0.50)) && ($tut_rating <= .99)){
                    echo "<img src="/images/stars/05o5.gif" width="70" height="18">";
                }
                if((($tut_rating >= 1.00)or($tut_rating == 1.50)) && ($tut_rating <= 1.49)){
                    echo "<img src="/images/stars/1o5.gif" width="70" height="18">";
                }
                if((($tut_rating >= 1.50)or($tut_rating == 1.50)) && ($tut_rating <= 1.99)){
                    echo "<img src="/images/stars/15o5.gif" width="70" height="18">";
                }
                if((($tut_rating >= 2.00)or($tut_rating == 2.00)) && ($tut_rating <= 2.49)){
                    echo "<img src="/images/stars/2o5.gif" width="70" height="18">";
                }
            
            if((($tut_rating >= 2.50)or($tut_rating == 2.50)) && ($tut_rating <= 2.99)){
                echo "<img src="/images/stars/25o5.gif" width="70" height="18">";
            }
            
            if((($tut_rating >= 3.00)or($tut_rating == 3.00)) && ($tut_rating <= 3.49)){
                echo "<img src="/images/stars/3o5.gif" width="70" height="18">";
            }
            
            if((($tut_rating >= 3.50)or($tut_rating == 3.50)) && ($tut_rating <= 3.99)){
                echo "<img src="/images/stars/35o5.gif" width="70" height="18">";
            }
            if((($tut_rating >= 4.00)or($tut_rating == 4.00)) && ($tut_rating <= 4.49)){
                echo "<img src="/images/stars/4o5.gif" width="70" height="18">";
            }
            if((($tut_rating >= 4.50)or($tut_rating == 4.50)) && ($tut_rating <= 4.99)){
                echo "<img src="/images/stars/45o5.gif" width="70" height="18">";
            }
            if($tut_rating == 5.0){
                echo "<img src="/images/stars/5o5.gif" width="70" height="18">";
            }
            } 

            it seems like this is exactly what i want, but i can't get it to work....thats all the code there is and it says the form posts to tutorials.php...but there's no code for tutorials.php.....or is all this code supposed to be just called tutorials.php

            it also says I can type

            ratemenu($tut_id);

            wherever i want in my site and it'll pull up that menu......is that right?? here's the website link.... http://www.phpfreaks.com/tutorials/9/0.php

              10 days later

              it seems like this is exactly what i want, but i can't get it to work....thats all the code there is and it says the form posts to tutorials.php...but there's no code for tutorials.php.....or is all this code supposed to be just called tutorials.php

              yes friend that is all tutorials.php code...

              and you need change

              function tut_stars($tut_rating){
                  if((($tut_rating >= 0)or($tut_rating == 0)) && ($tut_rating <= 0.50)){
                      echo "<img src="/images/stars/0o5.gif" width="70" height="18">";
                  }
                  if((($tut_rating >= 0.50)or($tut_rating == 0.50)) && ($tut_rating <= .99)){
                      echo "<img src="/images/stars/05o5.gif" width="70" height="18">";
                  }
                  if((($tut_rating >= 1.00)or($tut_rating == 1.50)) && ($tut_rating <= 1.49)){
                      echo "<img src="/images/stars/1o5.gif" width="70" height="18">";
                  }
                  if((($tut_rating >= 1.50)or($tut_rating == 1.50)) && ($tut_rating <= 1.99)){
                      echo "<img src="/images/stars/15o5.gif" width="70" height="18">";
                  }
                  if((($tut_rating >= 2.00)or($tut_rating == 2.00)) && ($tut_rating <= 2.49)){
                      echo "<img src="/images/stars/2o5.gif" width="70" height="18">";
                  }
              
              if((($tut_rating >= 2.50)or($tut_rating == 2.50)) && ($tut_rating <= 2.99)){
                  echo "<img src="/images/stars/25o5.gif" width="70" height="18">";
              }
              
              if((($tut_rating >= 3.00)or($tut_rating == 3.00)) && ($tut_rating <= 3.49)){
                  echo "<img src="/images/stars/3o5.gif" width="70" height="18">";
              }
              
              if((($tut_rating >= 3.50)or($tut_rating == 3.50)) && ($tut_rating <= 3.99)){
                  echo "<img src="/images/stars/35o5.gif" width="70" height="18">";
              }
              if((($tut_rating >= 4.00)or($tut_rating == 4.00)) && ($tut_rating <= 4.49)){
                  echo "<img src="/images/stars/4o5.gif" width="70" height="18">";
              }
              if((($tut_rating >= 4.50)or($tut_rating == 4.50)) && ($tut_rating <= 4.99)){
                  echo "<img src="/images/stars/45o5.gif" width="70" height="18">";
              }
              if($tut_rating == 5.0){
                  echo "<img src="/images/stars/5o5.gif" width="70" height="18">";
                4 days later

                Just what I was looking for 🙂 looks like its exactly what I am trying to do, thanks for posting this

                  Hint for cleaing up that code: counting half-stars is a lot easier.

                    a year later

                    How do you cleanly "count half stars" ?

                    regards,

                    Don

                      Exactly what it sounds like: Instead of recording 4 or 4.5, record 8 or 9, and divide by two only if and when you need the actual number.

                      A [0..5] rating that allows half scores is functionally identical to a [0..10] rating that only uses integers - the only differences are cosmetic. And although simply eliminating redundant tests would cut that $tut_rating code above in half, separating style from substance makes for even more savings.

                      function tut_stars_image($tut_rating)
                      {
                      	$images = array(
                      		'0o5', '05o5', '1o5', '15o5', '2o5', '25o5',
                      		'3o5', '35o5', '4o5', '45o5', '5o5');
                      
                      // Because the averaged $tut_rating is a float in the range [0,10]
                      $tut_rating = (int)round($tut_rating);
                      if($tut_rating<0 || $tut_rating>10) // Shouldn't happen.
                      {
                      	$tut_rating = 0; // or something smarter.
                      }
                      $alt = ($tut_rating/2).' out of 5'; // What I mean about dividing only if needed
                      $src = '/images/stars/'.$images[$tut_rating].'.png';
                      $style = 'width:70px;height:18px;';
                      echo "<img src=\"$src\" style=\"$style\" alt=\"$alt\" />";
                      }
                      

                      (though personally, I'd more likely return the image tag and echo it later).

                        Write a Reply...