Avatar billede andpek2323 Nybegynder
22. november 2013 - 22:58 Der er 1 kommentar og
1 løsning

kun 1 stemme?

Hej derude, jeg har fundet sådan et voting system på nettet, som entlig fungerer fint, men jeg kunne godt tænke mig at man kun havde mulighed for og stemme 1 gang, i stedet for som det bare er nu hvor man kan stemme alle de gange som man kan, er der nogle kloge mennesker derude som ved hvordan man gør det :)
mine koder ser således ud:

<?php

// including file to allow connecting to database
include ("include.php");

//connecting to database with dbconnect() which included in include.php
dbconnect();

//check if radio button is being choosed
if (isset($_GET['player']))
{
    $player = $_GET['player'];
   
// disallowing user to put some name manually to the url as a variable
    $check_player = mysql_query("SELECT * FROM votes WHERE entry='$player'") or die ("mysql_error()");
    if(mysql_num_rows($check_player) == 0)
    {
        die ("No such player , please do not play smart and choose a radio button to vote!");
    }
    else
    {
        echo "You voted for $player<br>";
    }
}

// checking if vote button is being pressed

if (!isset($_POST['vote']))
{
    echo "please select a vote.<br>";
// fetching data from database    
    $get = mysql_query ("SELECT entry,num_votes,percent FROM votes ORDER BY id ASC") or die ("mysql_error()");
    echo "<table>";
    while ($row = mysql_fetch_assoc($get))
    {
        $view_entry = $row['entry'];
        $view_percent = $row['percent'];
        $num_votes = $row['num_votes'];
        $image_width = $num_votes * 2 ; // i did this because i noticed that the image width is too small
// displaying the vote form
        echo "<form action='vote.php' method='post'>";
        echo "<tr>";
        echo "<td><input type='radio' name='player' value='$view_entry'>$view_entry</td>";
        echo "<td><img src='bar.jpg' width='$image_width' height='20'></td><td>% $view_percent</td><td> ($num_votes Votes)                      </td></tr>";
    }
    echo"<tr><td><input type='submit' name='vote' value='Vote!'></td></tr>";
    echo "</table>";
}

else
{

// if a vote is selected we set a variable to hold player name
    $show_vote = $_POST['player'];
   
// get the number of votes for the selected vote
    $get_num_votes = mysql_query ("SELECT num_votes FROM votes WHERE entry='$show_vote'"); //or die ("mysql_error()");
    while ($row_votes = mysql_fetch_assoc($get_num_votes))
    {
    $num_votes_per_entry = $row_votes['num_votes'];
   
// updating the number of votes by adding 1 to the total votes
    $update_num_votes = mysql_query ("UPDATE votes SET num_votes=$num_votes_per_entry+1 WHERE entry='$show_vote'") or die ("mysql_error()");
       
    }
// get percentage for each record
    $total_percent = 100;

// calculating the total of all votes for all entries

    $get_sum_of_votes = mysql_query ("SELECT SUM(num_votes) FROM votes") or die ("mysql_error()");
    while ($row2 = mysql_fetch_assoc($get_sum_of_votes))
    {
        $total_votes = $row2['SUM(num_votes)'];
    }
   
// setting a variable x to assign its value as the entry id

    $x = 1;
   
// get the value of each entry percent

    $get_each_percent = mysql_query ("SELECT num_votes FROM votes ORDER BY id ASC") or die ("mysql_error()");
    while ($row3 = mysql_fetch_assoc($get_each_percent))
    {
        foreach ($row3 as $value)
        {
// get the percentage for each entry       
            $value = round(($value / $total_votes) * 100);
            $update_percent = mysql_query ("UPDATE votes SET percent=$value WHERE id=$x") or die ("mysql_error()");
            $x++;
        }
    }
// redirect to vote.php and passing $player to check if it is exists.
    header ("Location: vote.php?player=$show_vote");
}
?>
Avatar billede andpek2323 Nybegynder
22. november 2013 - 23:00 #1
og min database ser således ud:


table votes set (id int not null auto_increment, entry varchar(200), percent float, num_votes int, primary key (id));
Avatar billede andpek2323 Nybegynder
15. januar 2014 - 19:31 #2
lukker
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Computerworld tilbyder specialiserede kurser i database-management

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester