?
Solved

Easy php poll code check and message display problem

Posted on 2004-08-17
3
Medium Priority
?
755 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 8

Accepted Solution

by:
GEM100 earned 2000 total points
ID: 11824230
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
ID: 11832111
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
ID: 11841012

hi,

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

Cheers
Rabid Lemming
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

764 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