Poll

This is a discussion on "Poll" within the PHP Forum section. This forum, and the thread "Poll are both part of the Program Your Website category.



 Subscribe in a reader

Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices


Reply
 
LinkBack Thread Tools
  #1  
Old Apr 11th, 2007, 23:25
Up'n'Coming Member
Join Date: Jan 2006
Location: NI
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
Poll

Hi i am trying to create a voting system but for some reason the code i am using wont work

code:
PHP: Select all

    <?php
    
    
    mysql_connect
("localhost""""") or die(mysql_error());
    
mysql_select_db("") or die(mysql_error());

    
//Name of our cookie
    
$cookie "Voted";

    
//A function to display our results - this refrences vote_pie.php which we will also make
    
function pie ()
    {
    
$data mysql_query("SELECT * FROM votes")
    or die(
mysql_error());
    
$result mysql_fetch_array$data );
    
$total $result[first] + $result[sec] + $result[third];
    
$one round (360 $result[first] / $total);
    
$two round (360 $result[sec] / $total);
    
$per1 round ($result[first] / $total 100);
    
$per2 round ($result[sec] / $total 100);
    
$per3 round ($result[third] / $total 100);
    echo 
"<img src=vote_pie.php?one=".$one."&two=".$two."><br>";
    Echo 
"<font color=ff0000>FIRST</font> = $result[first] votes, $per1 %<br>
    <font color=0000ff>SECOND </font> = $result[sec] votes, $per2 %<br>
    <font color=00ff00>THIRD </font> = $result[third] votes, $per3 %<br>"
;
    }
    
//This runs if it is in voted mode
if ( $mode=="voted")
{

//makes sure they haven't already voted
if(isset($_COOKIE[$cookie]))
{
Echo 
"Sorry You have already voted this month<br>";
}

//sets a cookie
else
{
$month 2592000 time();
setcookie(VotedVoted$month);

// adds their vote to the database
switch ($vote)
{
case 
1:
mysql_query ("UPDATE votes SET first = first+1");
break;
case 
2:
mysql_query ("UPDATE votes SET sec = sec+1");
break;
case 
3:
mysql_query ("UPDATE votes SET third = third+1");
}

//displays the poll results
pie ();
}
}
//if they are not voting, this displays the results if they have already voted
if(isset($_COOKIE[$cookie]))
{
pie ();
}

// or if they have not voted yet, they get the voting box
else
{
if(!
$mode=='voted')
{
?>
<form action = "<?php echo $_SERVER[&#8217;PHP_SELF’]; ?>" method = "GET">
<select name="vote"> <option value="1">Option 1</option>
<option value="2">Option 2</option> <option value="3">Option 3</option>
</select>
<input type=hidden name=mode value=voted>
<input type=submit>
</form>
<?
}
}
?>
?>
I have created the DB in mysql and all the field names are correct.

Anyone any ideas of what could be wrong, or is there an easier way to create a poll?


Thanks
Robert

Last edited by karinne; Apr 12th, 2007 at 12:19. Reason: Please use [php] ... [/php] tags when displaying PHP code!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote

  #2  
Old Apr 12th, 2007, 13:39
masonbarge's Avatar
Highly Reputable Member
Join Date: Jan 2006
Location: Atlanta GA
Posts: 631
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Poll

I hate to say this, but I have never been able to take a bit of packaged code and make it work. I tried a few times early on but I always ended up rewriting it so much that I might have been better off starting from scratch.

I just don't see this scheme. I'm sorry, I really am trying to say something helpful, but it's so full of questions I just can't take it on. My advice would be to figure out the methodology and recode it from scratch, testing step by step.

I immediately noticed this:
PHP: Select all

echo "<img src=vote_pie.php?one=".$one."&two=".$two."><br>"
A small thing, but there is no sense in concatenating the variables.
PHP: Select all

echo "<img src=vote_pie.php?one=$one&two=$two><br>"
Also, If this is XHTML you might need to code the ampersand.

Does the table have columns named 'first', 'sec', and 'third', or are these supposed to be rows? Okay, I see, you have a single row with these three field names and increase the proper field by one to reflect each vote.

Since you are using an associative array, don't you need to put the associative key name in single quotes? I think this is mandatory, but I always use numerical keys so I've lost touch, LOL. Also, I like to get my resources into variable values ASAP. That might be your problem.

I'm sorry this post is so wordy and I'm not sure what isn't working, but if it helps, I'd handle the query return like so (using the correct key number if you use a different query such as "SELECT *"):
PHP: Select all

$query "SELECT first, sec, third FROM votes";
$result mysql_query($query);
$data mysql_fetch_row($result);
$one $data[0];
$two $data[1];
$three $data[2]; 
Now, echo each of the variables to see if you are getting the right value. If you are, remove the echo and you can proceed knowing you have correct variables. If not, you know that the query return is incorrect.



Last edited by masonbarge; Apr 12th, 2007 at 13:45. Reason: didn't know about [php] :)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Tags
voting

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
[poll] Which is the best 360 game? CloudedVision Webforumz Cafe 4 May 22nd, 2008 10:17
Poll? Jack Franklin PHP Forum 4 Jan 5th, 2008 10:10
Need help with a poll karinne PHP Forum 4 Oct 6th, 2007 12:17
Image Poll Help errant PHP Forum 3 Jan 4th, 2007 14:36
Flash Poll JacobHaug Flash & Multimedia Forum 12 Sep 6th, 2006 13:39


All times are GMT. The time now is 06:07.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42