Link to home
Start Free TrialLog in
Avatar of Rawdon Hume
Rawdon HumeFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Easy php poll code check and message display problem

Hi,

I have made a php poll based on the same type at www.flashkit.com 

Example pages:
http://www.flashkit.com/tutorials/3D/
http://www.flashkit.com/tutorials/3D/Pseudo_3-Cort_Stu-104/index.php

My problem is simple, when the user click the submit button on one of the tutorials to cast a vote. The page refreshes and their vote is added but the voting table doesn’t disappear to stop them voting again. You have to go back to the main tutorial list page and then open up the tutorial again before you see the "you have already voted" message. So my question is this when the user votes how can I get it to display a message replacing the voting table that says "Thank you for your vote" and when you next visit the tutorial I displays you have already voted message?

Also would some one please check my php code and tell me if it’s correct as I am not sure the Rating_Functions.php code is right ???

Is this part right for instance: if (  $tmpDec[$loop] >= 10 ) {  ($tmpDec[$loop-1])

Could you please check the Rating_Functions.php code and tell me if its all right as well as help me get it so when a user submits a vote there is a message displayed when the page reloads saying “thank you for your vote”. Also when ever you revisit the page you get the normal “you have already voted message”

Any help would be appreciated

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"; //was:
        } 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 =  ""; //was:   $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] >= 10 ) {  ($tmpDec[$loop-1]) +1; }
                    }
                    $tmpDec = substr($tmpDec,0,2);
                    $tmpVal = $tmpWhole . "." . $tmpDec;
                  }
              }
              $ReturnVar = "Rating: " . $tmpVal . " ";
        }
        mysql_close($link);   // CLOSE DB
      }

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


Rating.js
===================================================================
// JAVA SCRIPT RATING'S FORM HANDLER

function HandleBoxes(intBox){
  document.Rating.SelectedRating.value = intBox;
  checkbox10.checked = false;
  checkbox9.checked = false;
  checkbox8.checked = false;
  checkbox7.checked = false;
  checkbox6.checked = false;
  checkbox5.checked = false;
  checkbox4.checked = false;
  checkbox3.checked = false;
  checkbox2.checked = false;
  checkbox1.checked = false;
  switch (intBox){
    case 10: checkbox10.checked = true;
            break;
    case 9: checkbox9.checked = true;
            break;
    case 8: checkbox8.checked = true;
            break;
    case 7: checkbox7.checked = true;
            break;
    case 6: checkbox6.checked = true;
            break;
    case 5: checkbox5.checked = true;
            break;
    case 4: checkbox4.checked = true;
            break;
    case 3: checkbox3.checked = true;
            break;
    case 2: checkbox2.checked = true;
            break;
    case 1: checkbox1.checked = true;
            break;
  }
}
===================================================================

Ratings.php
===================================================================
  <table  border="0" cellpadding="2">
    <tr>
      <td rowspan="2" valign="top"><div align="right"><span class="text-Small">Please
            indicate what you thought of this tutorial&nbsp;<br>
      10 is the best:&nbsp;</span></div></td>
      <td rowspan="2" valign="top"><table border="1" cellpadding="0" cellspacing="0" class="text-Small">
        <tr>
          <td align="center" class="text-Small"><input type="checkbox" name="checkbox10" value="10" onClick="Javascript:HandleBoxes(10)">
      10</td>
          <td align="center" class="text-Small"><input type="checkbox" name="checkbox9" value="9" onClick="Javascript:HandleBoxes(9)">
      9</td>
          <td align="center" class="text-Small"><input type="checkbox" name="checkbox8" value="8" onClick="Javascript:HandleBoxes(8)">
      8</td>
          <td align="center" class="text-Small"><input type="checkbox" name="checkbox7" value="7" onClick="Javascript:HandleBoxes(7)">
      7</td>
          <td align="center" class="text-Small"><input type="checkbox" name="checkbox6" value="6" onClick="Javascript:HandleBoxes(6)">
      6</td>
          <td align="center" class="text-Small"><input type="checkbox" name="checkbox5" value="5" onClick="Javascript:HandleBoxes(5)">
      5</td>
          <td align="center" class="text-Small"><input type="checkbox" name="checkbox4" value="4" onClick="Javascript:HandleBoxes(4)">
      4</td>
          <td align="center" class="text-Small"><input type="checkbox" name="checkbox3" value="3" onClick="Javascript:HandleBoxes(3)">
      3</td>
          <td align="center" class="text-Small"><input type="checkbox" name="checkbox2" value="2" onClick="Javascript:HandleBoxes(2)">
      2</td>
          <td align="center" class="text-Small"><input type="checkbox" name="checkbox1" value="1" onClick="Javascript:HandleBoxes(1)">
      1</td>
        </tr>
      </table></td>
      <td valign="bottom">
        </td>
    </tr>
    <tr>
      <td valign="bottom"><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 name="Submit" type="submit" class="text-Small" value="Submit">
      </form></td>
    </tr>
  </table>
===================================================================


Tutoral list page (only snipits of the page code): http://www.webwasp.co.uk/tutorials/index.php
===================================================================
<!-- RATINGS VARIABLES START   -->
<?php include ("Rating_Functions.php"); ?>
<!-- RATINGS VARIABLES END  -->

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


Tutoral page where users vote (only snipits of the page code) example page: http://www.webwasp.co.uk/tutorials/a01-simple-links/index.php
===================================================================
<?php
/*
<!--RATINGS VARIABLES START PART1  -->
*/

//Sets the uniqe ID for the tutorial
//Each tutorial must have it's own ID
  $ArticleID = 101;  //CHANGE THIS VALUE TO INDICATE CURRENT DOCUMENT'S RATING TABLE ID

  if ( (isset($_POST['SelectedRating'])) && $_POST['SelectedRating'] != "")
  {
    setcookie($ArticleID,"yes",time()+90*90*90);
  }
?>
<!-- RATINGS VARIABLES END PART1 -->
<!-- RATINGS VARIABLES START PART2  -->
<?php
  include ("../Rating_Functions.php");
 
  if ( (isset($_POST['SelectedRating'])) && $_POST['SelectedRating'] != "")
  {
    UpdateRating();
  }
?>
<script language="JavaScript" type="text/javascript" src="../Rating.js"></script>
<!-- RATINGS VARIABLES END PART2 -->


<?php
//Displays the voting table
if(!isset($_COOKIE[$ArticleID]))
{
  include ("../Ratings.php");
}
else if( (isset($_COOKIE[$ArticleID])) || ((isset($_POST['SelectedRating'])) && $_POST['SelectedRating'] != "") )
{
  print '<center><table border="1"><tr><td><b>You have already voted once</b></td></tr></table></center>';
}
?>
===================================================================

cheers
Rabid Lemming
ASKER CERTIFIED SOLUTION
Avatar of GEM100
GEM100

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Rawdon Hume

ASKER

hi,

sorry for the late reply

is the rest of my code ok then?

cheers
Rabid Lemming

hi,

Never mind, the code seams to be working really well so has to be ok?

Cheers
Rabid Lemming