PHP tutorials,a way to getting some thing new in web

Followers

Powered by Blogger.

Tuesday 3 March 2015

Online Voting System

No comments :
Hello Friends,Today I came with small application on this blog.I am sure you will like it.This system is name as "online voting system".

In this system,Right now voting functionality is shown.voting questions and options are static and also user is also static.



Here are two files are used and one database with one table.

config.php

<?php
$host="localhost";
$username="root";
$password="";
$db="test";
$link=mysql_connect($host,$username,$password);
mysql_select_db($db,$link);

?>

voting.php

<?php
include('config.php');
if(isset($_POST['submit1']))
{
$vote=$_POST['actor'];
$INSERT="insert into votes SET user_id='".$_SESSION['id']."',answer='".$vote."',create_date='now()'";
$res=mysql_query($INSERT) or die(mysql_error());
if(mysql_insert_id() > 0)
{
header("location:voting.php?msg=1");
}
}
$select="select * from votes where id>0";
$res1=mysql_query($select) or die(mysql_error());
$tot=mysql_num_rows($res1);
if(mysql_num_rows($res1) > 0)
{
    $answer1=0;
$answer2=0;
$answer3=0;
$answer4=0;
while($row_s=mysql_fetch_array($res1))
{
if($row_s['answer']==1)
{
$answer1+=1;
$per1=($answer1/$tot) * 100;
}
else if($row_s['answer']==2)
{
$answer2+=1;
$per2=($answer2/$tot) * 100;
}
else if($row_s['answer']==3)
{
$answer3+=1;
$per3=($answer3/$tot) * 100;
}
else if($row_s['answer']==4)
{
$answer4+=1;
$per4=($answer4/$tot) * 100;
}
}
}
?>
<html>
<head>
<title>Online Voting System</title>
</head>
<body>
<?php if($_GET['msg']==1){?><span style="background:green;color:#FFF;border-radius:5px;font-size:14px;width:100%;height:100%;">Your vote is submitted successfully</span><?php } ?><br/>
<?php echo 'Total Votes          '.$tot;?>
<div style="">
<span>Arjun Kapoor <?=$answer1;?>  <?= $per1.'%';?></span><br/>
<span>Ranvir Kapoor <?=$answer2;?> <?= $per2.'%';?></span><br/>
<span>Rabir Kapoor <?=$answer3;?>  <?= $per3.'%';?></span><br/>
<span>Varun Dhawan <?=$answer4;?>  <?= $per4.'%';?></span><br/>
</div>
<h1>Who will take place of superstar?</h1>
<form id="voteform" name="voteform" action="" method="post">
<input type="radio" name="actor" id="actor" value="1">Arjun Kapoor<br/>
<input type="radio" name="actor" id="actor" value="2">Ranvir Kapoor<br/>
<input type="radio" name="actor" id="actor" value="3">Rabir Kapoor<br/>
<input type="radio" name="actor" id="actor" value="4">Varun Dhawan<br/>
<input type="submit" name="submit1" id="submit1" value="Vote"/>
</form>
</body>

</html>

Database table structure:

table name:votes.
fields:id,user_id,answer,create_date


Integrate this code into your files.Enjoy coding!!!!

No comments :

Post a Comment