Jump to content

php help (voting) ?


jd2007

Recommended Posts

this is the voting form:

 

<?php
echo "<form method='post' action='voting2.php'>
      Who do you think will be the next President of the United States ?<br>
      <input type='radio' name='NextPresident' value='Barack Obama' checked='checked' />Barack Obama<br>
      <input type='radio' name='NextPresident' value='Hilary Clinton' />Hilary Clinton<br> 
      <input type='submit' value='Vote' />
      </form>";
?>

 

voting2.php is :

 

<?php

$obama=0;
$clinton=0;
$time=getdate();

if (!$time[month]="August" && !$time[mday]=14 && !$time[year]=2007) {
echo "Thank you.";
if ($_POST["NextPresident"]=="Barack Obama") {
    $obama++;
}
else {
$clinton++;
}

}
else if ($time[month]="July" && $time[mday]=14 && $time[year]=2007) {

$total=$obama+$clinton;
$obama_perc=($obama /  $total) * 100;
$clinton_perc=($clinton /  $total) * 100;
echo "<img src='barack.png' width='$obama_perc' height='20'><br><br><img src='clinton.png' width='$clinton_perc' height='20'>";
}

else {}

?>

 

barack.png and clinton.png is a purple bar which acts as a graph bar. this thing i'm doing is a poll until august 14, 2007. users vote whether Barack Obama or Hilary Clinton will be President. after august 14, 2007, it does some calculation and controls barack.png and clinton.png size, so that it acts like a graph...pls check why its not working...pls help me ?

Link to comment
Share on other sites

I am assuming the problem is due to neither having any values. Well, not actually not having values, but not having a divisible value (since 0 isn't divisible).

 

For example, try:

$obama=50;
$clinton=10;

 

How are you keeping track of the amount of votes each gets? 

Link to comment
Share on other sites

I modified your code to get around your current problem, however I am still unsure how you plan to keep track of the votes and also keep track of who has voted already. I suggest a mysql database to keep track of the votes and to use sessions/cookies to keep track of people that have already voted.

 

test_form.php

<?php
echo "<form method='post' action='voting2.php'>
      Who do you think will be the next President of the United States ?<br>
      <input type='radio' name='NextPresident' value='Barack Obama' checked='checked' />Barack Obama<br>
      <input type='radio' name='NextPresident' value='Hilary Clinton' />Hilary Clinton<br> 
      <input type='submit' name='login' value='Vote' />
      </form>";
?>

 

voting2.php

<?php

if (isset($_POST['login']))
{
    $obama = 0;
    $clinton = 0;
    $current_date = date("M-d-Y", mktime());
    $end_date = date("M-d-Y", mktime(0, 0, 0, 8, 14, 2007));

    if ($current_date != $end_date)
    {
        echo "Thank you.<br />";

        if ($_POST["NextPresident"] == "Barack Obama")
        {
            $obama++;
        }
        else
        {
            $clinton++;
        }

        $total = $obama+$clinton;

        if ($obama == 0)
        {
            echo "<img src='barack.png' width='0' height='20'>";
        }
        else
        {
            $obama_perc=($obama /  $total) * 100;
            echo "<img src='barack.png' width='$obama_perc' height='20'><br><br>";
        }
        if ($clinton == 0)
        {
            echo "<img src='clinton.png' width='0' height='20'>";
        }
        else
        {
            $clinton_perc=($clinton /  $total) * 100;
            echo "<img src='clinton.png' width='$clinton_perc' height='20'>";
        }
    }
    else
    {
        echo "Voting has ended!";
    }
}
else
{
    echo "You must vote first!";
}

?>

Link to comment
Share on other sites

I think that the best way of keeping the trak of all the people who voted is to get their ip and store it because (at least me) people delete their cookies after using the browser.

About the code. Not sure what's the problem but maybe you should try doing something like this:

echo "<img src='barack.png' width='".$obama_perc."' height='20'><br><br>";

 

instead of this:

 

echo "<img src='barack.png' width='$obama_perc' height='20'><br><br>";

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.