We help IT Professionals succeed at work.
Get Started

Simple php poll problem

Rawdon Hume
Rawdon Hume asked
on
692 Views
Last Modified: 2012-05-05
hi,

I have made a php poll based on the same php poll used to rate flash tutorials at flash kit. Example: http://www.flashkit.com/tutorials/Getting_Started/

Your notice how each tutorial has a number of hits and rating (number of votes) displayed near it name. When you open up the tutorial their is a voting system, that you can use to vote with.

Example:
http://www.flashkit.com/tutorials/Getting_Started/Using_La-Eddie_Ca-10/index.php

My php poll is exactly the same. My problem is that I don't want users to be able to vote more than once. The user needs to be able to vote once for each tutorial. I don’t want the user to be locked out from voting on all the tutorials once they vote for one of them. Quite simple a user can vote for a tutorial once only but can cast a vote on each tutorial at least once. The code needs to be simple as wee add new tutorials on a regular bases. I also haven't a clue how to use mysql so if your code uses that feature you need to explain it to me in layman’s terms please. If a user has already voted I want a message on the tutorial page where they cast their vote to be displayed saying sorry you have already voted. You will need to use and edit the code below.

You can see a working example of my poll here: http://www.webwasp.co.uk/test/poll/

The first tutorial : Simple Links  (http://www.webwasp.co.uk/test/poll/a01=simple-links/index.php) is the only one that has the php poll installed on it so far

Any help would be appreciated

This is all my code for the php poll:

Ratings.php
http://www.webwasp.co.uk/test/poll/Ratings.php
=====================================================================================
  <table border="1" cellspacing="0" cellpadding="0" align="center">
    <tr><td align="center">Please Indicate what you thought about this Tutorial</td></tr>
      <tr><td align="center">(1 for the worst review, 10 for the best...)</td></tr>
    <tr>
      <td align="center">
         <table width="100%" border="1" cellspacing="0" cellpadding="0">
          <tr>
            <td align="center"><input type="checkbox" name="checkbox1" value="1" onClick="Javascript:HandleBoxes(1)">1</td>
            <td align="center"><input type="checkbox" name="checkbox2" value="2" onClick="Javascript:HandleBoxes(2)">2</td>
            <td align="center"><input type="checkbox" name="checkbox3" value="3" onClick="Javascript:HandleBoxes(3)">3</td>
            <td align="center"><input type="checkbox" name="checkbox4" value="4" onClick="Javascript:HandleBoxes(4)">4</td>
            <td align="center"><input type="checkbox" name="checkbox5" value="5" onClick="Javascript:HandleBoxes(5)">5</td>
                  <td align="center"><input type="checkbox" name="checkbox5" value="5" onClick="Javascript:HandleBoxes(6)">6</td>
                  <td align="center"><input type="checkbox" name="checkbox5" value="5" onClick="Javascript:HandleBoxes(7)">7</td>
                  <td align="center"><input type="checkbox" name="checkbox5" value="5" onClick="Javascript:HandleBoxes(8)">8</td>
                  <td align="center"><input type="checkbox" name="checkbox5" value="5" onClick="Javascript:HandleBoxes(9)">9</td>
                  <td align="center"><input type="checkbox" name="checkbox5" value="5" onClick="Javascript:HandleBoxes(10)">10</td>
          </tr>
        </table>
       </td>
    </tr>
    <tr><td align="center"><form action='<?php print (ReturnCurrPage($HTTP_SERVER_VARS['PHP_SELF'])); ?>' target="_self" method="post" name="Rating">
                             <input name="CurrentArticle" type="hidden" value="<?php print $ArticleID ?>">
                             <input name="SelectedRating" type="hidden" value="">
                             <input type="submit" name="Submit" value="Submit">
                                       </form>
    </td></tr>
  </table>
=====================================================================================



Rating_Functions.php:
http://www.webwasp.co.uk/test/poll/Rating_Functions.php
=====================================================================================
<?php
  function ReturnCurrPage($CurrPage){
      for ($x=0;$x<strlen($CurrPage);$x++) {
        if ($CurrPage[$x] == "/") { $tmpPos = $x; }
      }
      return (substr($CurrPage,($tmpPos+1)));
  }

  function UpdateRating() {
    $strError = "";
    $Flag = 3;
    $link = mysql_connect("*****","*****","*****") or $Flag = 1;
    if ($Flag == 3){
      mysql_select_db("webwasp_co_uk1") or $Flag = 2;
      if ($Flag  <> 3){
        mysql_close($link);  // No Database Found Found!
      } else {  // DB LINK ESTABLISHED
        $z = $_POST['CurrentArticle'];
        $query = "select NumofRates, CurrentScore  from ratings where ArticleID = '$z';";
        $result = mysql_query($query);
        if (mysql_num_rows($result) == 0) {
          // ERROR NO ARTICLE IN DB - START NEW ID RECORD
          $x = 1;
              $y = $_POST['SelectedRating'];
              $query = "Insert Into ratings Values ('$z', '$x' , '$y');";
          mysql_query ($query);
        } else {
          while ($row = mysql_fetch_row($result)) {
            $x = $row[0];
            $y = $row[1];
          }
          $x++;
          $y = $y + $_POST['SelectedRating'];
          $query = "Update ratings Set NumofRates = '$x', CurrentScore = '$y' Where ArticleID = '$z';";
          mysql_query($query);
        }
      }
      mysql_close($link);   // CLOSE DB
    } else { // NO CONNECTION AVAILABLE
      }
  }

  function ReturnRating($ArtID) {
    $strError = "";
    $Flag = 3;
    $link = mysql_connect("*****","*****","*****") or $Flag = 1;
      if ($Flag == 3){
      mysql_select_db("webwasp_co_uk1") or $Flag = 2;
        if ($Flag  <> 3){
        mysql_close($link);  // No Database Found Found!
        $ReturnVar =  "Not Rated";
        } else {  // DB LINK ESTABLISHED
        $z = $ArtID;
        $query = "select NumofRates, CurrentScore  from ratings where ArticleID = '$z';";
        $result = mysql_query($query);
        if (mysql_num_rows($result) == 0) {     //  NO ARTICLE IN DB - NOT RATED
          $ReturnVar =  "Not Rated";
        } else {
          while ($row = mysql_fetch_row($result)) {
            $x = $row[0];
            $y = $row[1];
          }
          $tmpVal = ($y / $x);
              if (strpos($tmpVal,".") === false){
                $tmpVal = $tmpVal . ".00";
              } else {
                if ( ((strlen($tmpVal)) - (strpos($tmpVal,"."))) > 3 ) {
                    $tmpWhole = substr($tmpVal, 0, strpos($tmpVal,"."));
                    $tmpDec = substr($tmpVal,strpos($tmpVal,".")+1);
                    for ($loop=(strlen($tmpDec));$loop>1;$loop--){
                        if (  $tmpDec[$loop] >= 5 ) {  ($tmpDec[$loop-1]) +1; }
                    }
                    $tmpDec = substr($tmpDec,0,2);
                    $tmpVal = $tmpWhole . "." . $tmpDec;
                  }
              }
              $ReturnVar = "Rated: " . $tmpVal . " / 10";
        }
        mysql_close($link);   // CLOSE DB
      }

    } else { // NO CONNECTION AVAILABLE
        $ReturnVar =  "Not Rated";
      }
      return ($ReturnVar);
  }
?>
=====================================================================================


Rating.js
http://www.webwasp.co.uk/test/poll/Rating.js
=====================================================================================
// JAVA SCRIPT RATING'S FORM HANDLER
function HandleBoxes(intBox){
  document.Rating.SelectedRating.value = intBox;
  checkbox1.checked = false;
  checkbox2.checked = false;
  checkbox3.checked = false;
  checkbox4.checked = false;
  checkbox5.checked = false;
  switch (intBox){
    case 1: checkbox1.checked = true;
            break;
    case 2: checkbox2.checked = true;
            break;
    case 3: checkbox3.checked = true;
            break;
    case 4: checkbox4.checked = true;
            break;
    case 5: checkbox5.checked = true;
            break;
  }
}
=====================================================================================


Index page for the tutorial where user can vote:
http://www.webwasp.co.uk/test/poll/a01=simple-links/index.php
=====================================================================================
<!-- RATINGS VARIABLES START   -->
//Sets the uniqe ID for the tutorial
//Each tutorial must have it's own ID
<?php
  $ArticleID = 1;  //CHANGE THIS VALUE TO INDICATE CURRENT DOCUMENT'S RATING TABLE ID
  include ("../Rating_Functions.php");
  if ( (isset($_POST['SelectedRating'])) && $_POST['SelectedRating'] != ""){ UpdateRating(); }
?>
<script language="JavaScript" type="text/javascript" src="../Rating.js"></script>
<!-- RATINGS VARIABLES END -->

//Displays the voting table
  <!-- RATINGS VARIABLES START   -->
<?php include ("../Ratings.php"); ?>
<!-- RATINGS VARIABLES END   -->
=====================================================================================


Index page for the list of all the available tutorials. Where the ratings are displayed for each tutorial
http://www.webwasp.co.uk/test/poll/index.php
=====================================================================================
<!-- RATINGS VARIABLES START   -->
<?php include ("Rating_Functions.php"); ?>
<!-- RATINGS VARIABLES END  -->


<?php print(ReturnRating(1)); ?>
=====================================================================================

Cheers
Rabid Lemming

Comment
Watch Question
This problem has been solved!
Unlock 1 Answer and 25 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE