PHP develops si...LOGIN

PHP develops simple voting system voting page function module (2)

116.png

After completing the previous voting, you can choose to click View Results to view the total number of votes for each project and the voting percentage for all projects.

After clicking "View Results", the program will automatically calculate the number of votes and percentage of each item.

Use<input type="hidden">hidden form attribute

<input type="hidden"> The hidden field is invisible to the user on the page. The purpose of inserting hidden fields into a form is to collect or send information so that it can be used by the program that processes the form. When the viewer clicks the send button to send the form, the hidden field information is also sent to the server.

The hidden attribute can also be used to prevent the user from viewing an element until certain conditions are matched (such as a checkbox being selected). JavaScript can then remove the hidden attribute to make this element visible.

<input type="hidden" id="selected_id" name="selected_id" value="">
<input type="button" value="查看结果" onClick="location.href='index.php?id=ck'"/>&nbsp;&nbsp;
<script type="text/javascript">
    $("[type='radio']").click(function(){
       $("#selected_id").val($(this).val());
    });
</script>

Automatically calculate the number of added votes and judge the session operation:

 <?php
  $id=$_POST["itm"];
  $SQL="UPDATE vote SET count=count+1 WHERE id=$id";  //自动添加投票数
  if(mysqli_query($link,$sql))
  {
  $_SESSION["vote"]=session_id();
  ?>
    <script language="javascript">alert("投票成功,点确定查看结果");location.href="index.php?id=ck";</script>
  <?php
  }
  else
  {
  ?>
    <script language="javascript">alert("投票失败");location.href="index.php";</script>
    <?php
  }
?>

Calculate the percentage of voting items in the hidden <table> table

Calculate the total number of votes first, The voting information will be saved in the database, and the total number of numerical columns will be returned through the sum() function

<?php
if(isset($_GET["id"])=="ck"){
?>

  <?php
  $SQL="SELECT sum(count) as 'total' FROM vote";
  $rs=mysqli_query($link,$sql);
  $rows=mysqli_fetch_assoc($rs);
  $sum=$rows["total"];  //得出总票数
}  
  ?>

Then each voting item will be output in a loop and the percentage will be calculated, and each vote in the database table will be queried through the SQL statement SELECT The number of votes the content received, divided by the total number of votes.

<?php
 $SQL="SELECT * FROM vote";
 $rs=mysqli_query($link,$sql);
while($rows=mysqli_fetch_assoc($rs))
{
  ?>
  <tr>
    <td bgcolor="#FFFFFF"><?php echo $rows["item"]?></td>
    <td bgcolor="#FFFFFF"><?php echo $rows["count"]?></td>
    <td bgcolor="#FFFFFF">
      <?php
      $per=$rows["count"]/$sum;
      $per=number_format($per,4);
      ?>
      <img src="" height="4" width="<?php echo $per*100?>" />
      <?php echo $per*100?>%      </td>
  </tr>
  <?php
}
?>
Next Section
<input type="hidden" id="selected_id" name="selected_id" value=""> <input type="button" value="查看结果" onClick="location.href='index.php?id=ck'"/>   <script type="text/javascript"> $("[type='radio']").click(function(){ $("#selected_id").val($(this).val()); }); </script>
submitReset Code
ChapterCourseware