May
2nd

How To Create Basic Poll Using PHP & MySQL (Part 3)

Files under PHP & MySQL | 1,998 views

This week, you can learn how to calculate the percentage value for each poll option that you have chosen. The percentage value is computed based on this formula:

poll formula

So, you can create another file named Poll_Display.php
Then create a function to show your poll question and options. Let say your function is named ShowPoll()

function ShowPoll() {

$poll = Poll::GetPollQuestion();
$vote = Poll::GetUserVote($poll->qst_id,CurrentUser::$userid);

if ( $vote ) {
showResultPage();//if voted before, just show poll result page
}

else {

if ($_POST["submit"] == “Submit”) {
$choice = (intval(substr($_POST["Vote"], 3, 1))); //use intval to convert string value to int value

if ($_POST["Vote"] == opt$choice“) {
$v = Poll::UserVoteOption(CurrentUser::$userid,$poll->qst_id,$choice);
$r = Poll::IncreasePollAnswerCount($poll->qst_id, $choice);
showResultPage();

}

}

else {
showQuestion();
}

}

}

Next, you need to create showResultPage() and showQuestion()

poll_result

function showResultPage() {

$poll = Poll::GetPollQuestion();
$vote = Poll::GetUserVote($poll->qst_id,CurrentUser::$userid);

$totalPercentage = 0;
$highestPercentage = 0;
for ($i=1; $i<5; $i++) {

$opt = $poll->{’opt’.$i};
$percent = ‘VotePercent’.$i;
if ( $opt ) {

$percent = Round( ($opt / $poll->votes) * 100);
$totalPercentage += $percent;

} else {
$percent = 0;
}

if ($percent > $highestPercentage ) {
$highestPercentage = $percent;
}

}

echo <<<STOP
<table width=”100%”>

<tr>

<td align=”left”>
<div class=”voteQ”>$poll->qst</div>
</td>

</tr>

</table>

<table width=”100%” bgcolor=”transparent” style=”margin:0 0px 10px 0px;”>

<tr><td height=”10px;”></td></tr>

STOP;

for ($i=1; $i<5; $i++) {

$ans = $poll->{‘ans’.$i};
if ( ! $ans ) {
continue;
}
$percent = ${‘VotePercent’.$i};
$class = $percent == $highestPercentage ? ‘voteHighestBar’ : ‘voteNormalBar’;
$pClass = $percent == $highestPercentage ? ‘voteHighestPercent’ : ‘voteNormalPercent’;

echo <<<STOP
<tr>

<td align=”left”>$ans</td>
<td width=”100px” align=”left”> //set the width to 100px since each percentage value will not greater than 100, this is to prevent alignment issues
<div class=”voteBarBlk”>
<div style=”width:{$percent}px;” class=”$class“>
</div>
</td>
<td align=”right”>
<span class=”$pClass“>$percent%</span>
</td>
</tr>
<tr><td height=”4px”></td></tr>

STOP;
}

echo <<<STOP
</table>

<table width=”100%” style=”margin-left:10px;”>
<tr><td height=”20px;”></td></tr>
<tr>

<td width=”0px”></td>
<td>
<div style=”margin-left:-15px;”>
Total votes: $poll->votes
</div>
</td>

</tr>
<tr><td style=”height:5px”></td></tr>

</table>

<table>
<tr>

<td style=”font-size:12px;”>
Each member is entitled to 1 vote. <br>
You have voted ! Thank You.
</td>

</tr>
</table>

STOP;
}

function showQuestion() {

$poll = Poll::GetPollQuestion();
$vote = Poll::GetUserVote($poll->qst_id,CurrentUser::$userid);

echo <<<STOP
<form method=”post” action=”$PHP_SELF“>

<table style=”width:100%”>

<tr>

<td align=”left”>
<div class=”voteQ”>$poll->qst</div>
</td>

</tr>
<tr><td height=”2px;”></td></tr>

STOP;

for ($i=1; $i<5; $i++) {

$ans = $poll->{‘ans’.$i};
if ( ! $ans ) {

continue;

}
echo <<<STOP
<tr>

<td style=”height:10px”>
</td>

<td align=”left”>
<div style=”margin-left:20px; margin-top:2px;”>
<input type=”radio” name=”Vote” value=”opt$i” style=”margin-right:15px”>$ans</input></div> //make sure that the name is same with $_POST[‘Vote']
</td>

</tr>
STOP;
}

echo ‘<tr><td>’;
if ( CurrentUser::isTempUser() ) {

echo ‘<div style=”color:#0000ff; margin-top:50px;” align=”left”>’
.’<label>Express your opinion.</label><br>’
.’<label><a href=”apps/users/login.php” style=”color:#ff0000″>Log in</a> to vote.</label>’
.’</div>’; //create login.php that allow user to log in

} else {

echo
‘<div style=”margin-top:50px; margin-left:120px;”>’
.’<input type=”submit” name=”submit” value=”Submit” /></div>’; //make sure that the name is same with $_POST[‘Submit']

}

echo ‘</td></tr></table></form>‘;

}

After learning my steps and procedures, im sure you can create a good poll voting for yourself. If you found any problem, please do not feel hesitate to leave me a comment here. I will always give you great solution. Have a nice day!!!


Post a Comment

| 1,998 views