Thread: Need help with PHP poll code
Results 1 to 5 of 5
Related
-
Help with PHP poll script Forum: PHP Forum
Replies: 10 -
Hand Writing Poll Results Scripts Forum: PHP Forum
Replies: 0 -
php code for poll Forum: PHP Forum
Replies: 4 -
Ignore code and link greeting note code Forum: HTML Forum
Replies: 3 -
Cant figure out why my poll doesnt work? ^_^ Forum: PHP Forum
Replies: 4
-
10-27-2006, 12:45 PM #1
Need help with PHP poll code
I need to troubleshoot PHP code to store the results of a poll, and to display the results. I have started the code but am really confused how to properly use the CREATE DATABASE, CREATE TABLE, INSERT and SELECT statements.
I need help with this, as well as how to display the results in a similar fashion to the below:
Your favorite flower is the Rose.
There are 500 total votes.
Rose received 10% of the total vote.
Daisy received 20% of the total vote.
Other received 70% of the total vote.
Please assist me with structuring the code, and/or direct me to a similar poll on the Internet, so that I may actually see code that works. Examples of how to use the above statements would be very helpful.
Code:<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <title>My Favorites</title> </head> <body> <FORM style="MARGIN-TOP: 0px; MARGIN-LEFT: 0px; MARGIN-RIGHT: 0px" name=Favorites action=Pollresults2.php method=post target=""> <table width="33%" border="1" align="center" bordercolor="#333399"> <tr bordercolor="#333399"> <th scope="col"> <TABLE width=133 border=1 cellPadding=2 cellSpacing=0 bordercolor="#333399" bgColor=#ddeeff> <TBODY> <TR> <TD bgColor=#5588cc> <TABLE width="100%" border=1 cellPadding=0 cellSpacing=0 bordercolor="#FFFFFF"> <TBODY> <TR> <TD align=middle> <div align="center"><FONT face=verdana,arial,sans-serif color=#ffffff size=2><B>My Favorites</B></FONT></div> </TD> </TR> </TBODY> </TABLE> </TD> </TR> <TR> <TD> <div align="center"><FONT face=verdana,arial,sans-serif color=#000000 size=1>Which is your favorite Flower?<BR> </FONT> <TABLE border=1 bordercolor="#333399"> <TBODY> <TR> <TD vAlign=top> <INPUT type=radio value=Daisy name=Flower> </TD> <TD vAlign=center><FONT face=verdana,arial,sans-serif color=#000000 size=1>Daisy</FONT></TD> </TR> <TR> <TD vAlign=top> <INPUT type=radio value=Rose name=Flower> </TD> <TD vAlign=center><FONT face=verdana,arial,sans-serif color=#000000 size=1>Rose</FONT></TD> </TR> <TR> <TD vAlign=top> <INPUT type=radio value=Tulip name=Flower> </TD> <TD vAlign=center><FONT face=verdana,arial,sans-serif color=#000000 size=1>Tulip</FONT></TD> </TR> <TR> <TD vAlign=top> <input type=radio value=Lily name=Flower> </TD> <TD vAlign=center><FONT face=verdana,arial,sans-serif color=#000000 size=1>Lily</FONT></TD> </TR> <TR> <TD vAlign=top> <INPUT type=radio value=Orchid name=Flower> </TD> <TD vAlign=center><FONT face=verdana,arial,sans-serif color=#000000 size=1>Orchid</FONT></TD> </TR> <TR> <TD vAlign=top> <INPUT type=radio value=Lilac name=Flower> </TD> <TD vAlign=center><FONT face=verdana,arial,sans-serif color=#000000 size=1>Lilac</FONT></TD> </TR> <TR> <TD vAlign=top> <INPUT type=radio value=Other name=Flower> </TD> <TD vAlign=center><FONT face=verdana,arial,sans-serif color=#000000 size=1>Other</FONT></TD> </TR> </TBODY> </TABLE> <BR> </div> </TD> </TR> </TBODY> </TABLE> </th> <br> <br> <table width="100%" cellspacing="5" cellpadding="5"> <tr> <th scope="col"> <INPUT type=submit value="Submit Vote" name=poll_submit> </th> </tr> </table> <br> </FORM> </body> </html>
PHP Code:</head>
<body>
<form action="http://ellesell.com/cgi/Poll2.html" method="get">
<style type="text/css">
<!--
.style1 {
color: #333399;
font: bold 24px Arial;
text-align: center;
-->
</style>
<p class="style1">Le Fleur!</p>
<strong><p class="style5"> Thank you for your vote. Below are the results.
<br>
If you did not vote, please hit the back button and do so. </p> </strong>
<br>
<?php
/* insert into table */
$selectedFlower = Request.Form("Poll2.html");
$inserQuery="insert into Poll2.html (response) values (" + selectedFlower + ")";
mysql_query($inserQuery);
?>
<?php
if ( $Flower == "" ) {
$error=true;
echo "Please vote.";
} else {
echo "Your favorite flower is the: $Flower."; }
?>
<br><br>
<?php
$query="select (sum( response) /count(*)) as '% Votes'; }
$result=mysql_query($query);
$num=mysql_numrows($result);
$i=0;
echo "The Poll Results Are:" ; }
while ($i < $num) {
?>
<?php
$percentageVotes = mysql_result($result,$i,"% Votes");
$flowerName = mysql_result($result,$i,"response"); }
<?php
if( $flowerName = 'F1'){
$flowerName = 'Rose';
}else if($flowerName = 'F2'){
$flowerName = 'Daisy';
}else if($flowerName = 'F3'){
$flowerName = 'Tulip';
}else if($flowerName = 'F4'){
$flowerName = 'Lily';
}else if($flowerName = 'F5'){
$flowerName = 'Orchid';
}else if($flowerName = 'F6'){
$flowerName = 'Lilac';
}else if($flowerName = 'F7'){
$flowerName = 'Other';
?>
<?php
select (count( response) /count(*)) as '% Votes' </td><td>$flowerName </td></tr>"; }
$i++;
}
?>
<br><br>
<input type="Submit" value="BACK" name="Submit">
<br>
<p class="style5"> <strong>Thanks again for your time.</strong> </p>
</form>
</body>
</html>
-
10-27-2006, 06:01 PM #2
Re: Need Help with Code
well... leme do some sql 101....
SELECT * FROM table WHERE somthing = somethingelse
the SELECT statment is telling it to return data...
the * is not the only thing that can go there.... what * does is return all the rows. if you had a table with 2 rows: "id" and "name". * would be the equivolent to putting: id, name so the sql would look like this:
SELECT id, name FROM table WHERE something = somethingelse
the "FROM table" is telling it which table to get or put info. ie: the "table" should be the name of the table you store the data.
the WHERE is telling it that you only want certain info... always immedialty after it comes the expression which in this case is: "something = somethingelse". in this case "something" would be the row name and "somethingelse" would be the value...
here are some examples:
SELECT * FROM orders WHERE id = 555
so in this case there is a table called "orders" you want to return all the rows and you only want the record(s) that the "id" equils 555
SELECT name FROM users WHERE user_id = 1
in this example you would be returning ONLY the "name" of the person with the user id of 1 in the "users" table.
you can also do more advanced stuff like adding:
ORDER BY (ASC|DEC)
&
LIMIT (number)
Now for some INSERT examples...
INSERT INTO tablename (rowname1, rowname2, rowname3) VALUES (valueofrowname1, valueofrowname2, valueofrowname3);
this is pretty simple... "INSERT INTO" is telling it to add data to a table.... of corse the table name should immediatly follow. afterwhich in parentheses "()" you should have the row names of what you are inserting. after which comes "VALUES" which is telling it that the values of what to insert is coming. (ie: immedialty after the values of the rows should come).
so here are some examples:
INSERT INTO users (id, username, firstname, lastname) VALUES (DEFAULT, 'ALL', 'Nathan', 'Bruer')
In that example you have a table named 'users' and you are inserting id as DEFAULT (in this case it would probably be an auto_incroment value, meaning it adds 1 to each value added from the last). and of corse the username would be 'ALL' firstname would be 'Nathan', lastname would be 'Bruer')
another would be:
INSERT INTO `sites` (`Site Name`, `Description`, `URL`) VALUES ('ALL\'s Site', 'The Best Site in the World', 'http://www.allada.com')
In that example you are inserting into 'sites' table (not the ` this depends on which database program but i believe in mysql ` signifies table or the row. in postgresql the " signifies itentifiers and in both the ' signifies data) then the rest is self explanitory
As for the other statments for a new person to databases i seggest using a script like phpmyadmin to add tables, databases, or rows.
after that, i did notice a few things wrong with your code, such as on line 91 on the bottom code, and line 37 on the bottom code.
as for the functions you are using such as count() i seggest looking up the info on mysql's website because that is one of the things that doesnt stay the same between postgresql and mysql....
in other words i am a postgresql person and know just enough in mysql to do the job needed.
hope that helps you a little,
-ALL
-
10-28-2006, 03:10 PM #3
Re: Need Help with Code-Made changes-still errors
I have reworked much of the code.
I'm trying to correctly code the CREATE TABLE, and INSERT statements. I am getting an error for the INSERT line.
I'm not sure that I have structured any of it right. I am still confused as to what goes where. I created a table named votes, and named the only column (it's just 1 question with 7 possible answer choices) Flower. Should I be naming each row there? I named the rows Flower1-7. Then I inserted the choices for each row. There is only 1 choice for each row.
Do I need 7 insert statements? Is the CREATE statement wrong for a 1 column, 7 row table?
Is the SELECT statement correct?
Thanks for the help.
Below is the troublesome code:PHP Code:<?php
$query="CREATE TABLE votes (Flower);
VALUES(Flower1 not null primary key,Flower2 not null primary key,Flower3 not null primary key,Flower4 not null primary key,Flower5 not null primary key,Flower6 not null primary key,Flower7 not null primary key)" ;
?>
<?php
INSERT INTO votes (Flower) VALUES (Daisy, Rose, Tulip, Lily, Orchid, Lilac, Other);
?>
<?php
if ( $Flower == "" ) {
$error=true;
echo "Please vote." ;
} else {
echo "Your favorite flower is the: $Flower."; }
?>
<br><br>
<span class="style2">"Here are the results:"</span>
<?php
SELECT Flower FROM votes WHERE $Flower= Daisy
?>
-
10-28-2006, 06:18 PM #4
Re: Need Help with Code-Made changes-still errors
you have a few problems with your code... i am not sure why... but you are using the <? and ?> as sql stuff... that is NOT what they do...
the <? or <?php tells php that code starts here... the ?> tells php that the code ends here and everything after that, that is not in <??> should be output...
so if i have:
PHP Code:Hello my name is:
<?php
echo 'jon';
?><br>
and his name is: jason
HTML Code:hello my name is: jon<br> his name is jason
now to fix your code:
PHP Code:<?php
$dbname="my_db_name"; //<-- set the name of the database!
$tablename="votes"; //<-- set the name of the table
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password') or die('Could not connect: ' . mysql_error()); // <-- Set the username, password, and host
mysql_select_db($dbname) or die('Could not select database');
//This sets the database to use
$query = "
SELECT count(*) AS 'count'
FROM
information_schema.tables
WHERE
`Table_Schema` = '$dbname'
AND
`Table_Name` = '$tablename'";
/*
This is a very elaborite example...
It counts all the rows and returns the count as "count"
The table it is using is a special table in mysql called "information_schema" which is like the info of the database in the table "tables"
What it does is search the collum "Table_Schema" database to anything matching $dbname which is the name of the database
It also searches "Table_Name" for $tablename which is the name of the table... so it will return the number of times Table_Schema
equils the database name and Table_Name equils the table name in the same row...
EXAMPLE:
Database: information_schema
Table: table
[Table_Schema] [Table_Name]
my_db_name votes <-- will count this and add it to count
my_db_name users <-- will NOT count this because Table_Name != users
anotherdatabase votes <-- will NOT count because Table_Schema != my_db_name
*/
$result = mysql_query($query) or die(mysql_error());
// This runs the query in mysql
$line = mysql_fetch_array($result, MYSQL_ASSOC) or die(mysql_error());
// This fetches the first row of the requested query
if($line['count'] == '0'){
// This checks to see if the table already exists... $line['count'] will only return 1 or 0. 0 means does not exist, 1 means it does exist
$query = "
CREATE TABLE `$tablename`
(
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`Flower` VARCHAR( 255 ) NOT NULL ,
`ip` INT NOT NULL
)
ENGINE = innodb";
// This creates the table with the rows 'id','Flower', and 'ip'
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
// This executes the query above
}
$ipAry = explode('.', $_SERVER['REMOTE_ADDR']);
// This is really not necessary, but thought might help, what it does is get's the IP of the person accessing the page and splits it into an array. Example:
// if $_SERVER['REMOTE_ADDR'] = '12.34.56.67'
// this $ipAry would be: Array('12', '34', '56', '67')
$ipStr = str_pad(dechex($ipAry[0]), 2, "0", STR_PAD_LEFT) . str_pad(dechex($ipAry[1]), 2, "0", STR_PAD_LEFT) . str_pad(dechex($ipAry[2]), 2, "0", STR_PAD_LEFT) . str_pad(dechex($ipAry[3]), 2, "0", STR_PAD_LEFT);
// This does alot of stuff... but in short... it puts the ip in hexidecimal value and makes sure a '0' is in front of it if it is only 1 didget... EXAMPLE:
// if $_SERVER['REMOTE_ADDR'] = '12.34.56.67'
// $ipStr would = 0C223843 <-- that is 12 in hex added by 34 in hex added by 56 in hex (exc...)
$ipInt = hexdec($ipStr);
// This converts $ipStr to an integer. EXAMPLE:
// if the ip was '12.34.56.67' the hex value would be: 0C223843
// this convers that hex value (in this example 0C223843) to: 203569219
// to help you understand... you know how an ip is: xxx.xxx.xxx.xxx with the xxx being at max of 255 characters... well... the reason it is 255 characters is because 2^8 = 256 which computers include 0 meaning 256-1 (to include the 0) = 255
// well hex is 16 base meaning the range is 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F so... if i had the ip 12.34.56.67 it would be the same as: 0C.22.38.43 in hex
$query = "
SELECT count(*) AS `count`
FROM
`$tablename`
WHERE
`ip` = " . $ipInt;
// This checks to see if that ip address already voted and only returns the count which should only be 0 or 1
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
// This executes it
$line = mysql_fetch_array($result, MYSQL_ASSOC);
// This gets the first result the query returns (which 1 is the max it can be)
if($line['count'] != '0'){
// This checks to see if the person voted already
$postmsg = "<b>You have already voted<b><br>";
$voted=true;
}else{
// This is if the person has not voted
$postmsg = '<form method="POST" action="?">Vote: <input type="text" name="favFlower"><input type="Submit" value="Submit"></form><br>';
$voted=false;
}
if(@$_POST['favFlower'] && !$voted){
//this checks to see if data was sent and to see if they have not already voted
$query="
INSERT INTO `$tablename`
(
`Flower`,
`ip`
)
VALUES
(
'".mysql_real_escape_string($_POST['favFlower'])."',
$ipInt
)";
// This inserts the info into the table (note the mysql_real_escape_string() <-- this is to make sure somone is not trying to hack your site)
mysql_query($query) or die('Query failed: '.mysql_error());
// This executes the query above
$postmsg = "<b>You have already voted</b><br>";
$voted=true;
}
echo $postmsg;
// This Sends $postmsg to the browser
$query = "SELECT *
FROM
`$tablename`";
// This fetches all the values in the table
$result = mysql_query($query) or die('Query failed: '.mysql_error());
// This executes the query above
while($line = mysql_fetch_array($result, MYSQL_ASSOC)){
// This runs though each item in the table and returns $line as an associatave array
@$votes[$line['Flower']]++;
// This is semi-complicated if you dont understnad it... what this does is...
// checks to see if somone already voted on the same flower and if they did it adds 1 to it...
// if somone else has not already voted on the same one... it sets it to 1
}
?>
<table border="1"><tr><td>FlowerName</td><td>Votes</td></tr><?php
//This returns everything between the ?\> and </? to the browser
if(is_array(@$votes)){
//this checks to see if anyone has voted
foreach($votes as $key => $value){
//This loops though each unique "flower" and returns the flower name as $key and the number of votes as $value
echo "<tr><td>$key</td><td>$value</td></tr>\n";
//This returns the info in the table
}
}
?></table>
The comments should explain everything...
the one thing i did not explain was the @ symbol infront of the variables...
the @ symbol simply suppresses errors... this is simply a shortcut around errors and notices... some places i expect errors to occur but where forplaned so just remember all it does is makes sure it does not error on that satament.
Questions... ont hesitate to ask,
-ALL
-
04-17-2015, 05:20 AM #5
Re: Need help with PHP poll code
I typed "free banner rotation script php" into my search box, and got 34,368 results.
I'm sure they're not all what you're looking for, but one of them must be a reasonable match.
Also, right a credit for the script being used.
If you've tried these options, I apologise for bringing them up - but as you've not mentioned them I assume you haven't.
DevIL jiN
Cloudjiffy- PaaS for Developers
10-05-2020, 12:30 AM in Web Hosting Forum