Solved

Easy php poll code check and message display problem

Posted on 2004-08-17
3
742 Views
Last Modified: 2012-05-05
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
0
Comment
Question by:Rawdon Hume
  • 2
3 Comments
 
LVL 8

Accepted Solution

by:
GEM100 earned 500 total points
Comment Utility
I would just rewrite the code to display voting table to make it:

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



When you set cookie, it may not be read by the same script, unless you reload the page.
You also need to add cookie checking to this part:

 if ( (isset($_POST['SelectedRating'])) && $_POST['SelectedRating'] != "")
  {
    UpdateRating();
  }


tomake it:

 if (isset($_POST['SelectedRating']) && $_POST['SelectedRating'] != "" && !isset($_COOKIE[$ArticleID]))
  {
    UpdateRating();
  }



That's it I believe.
0
 
LVL 2

Author Comment

by:Rawdon Hume
Comment Utility
hi,

sorry for the late reply

is the rest of my code ok then?

cheers
Rabid Lemming
0
 
LVL 2

Author Comment

by:Rawdon Hume
Comment Utility

hi,

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

Cheers
Rabid Lemming
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now