944,029 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 1519
  • PHP RSS
Oct 27th, 2006
0

Need Help with Code

Expand Post »
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.

PHP Syntax (Toggle Plain Text)
  1.  
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  5. <title>My Favorites</title>
  6. </head>
  7. <body>
  8. <FORM style="MARGIN-TOP: 0px; MARGIN-LEFT: 0px; MARGIN-RIGHT: 0px"
  9. name=Favorites action=Pollresults2.php method=post
  10. target="">
  11. <table width="33%" border="1" align="center" bordercolor="#333399">
  12. <tr bordercolor="#333399">
  13. <th scope="col">
  14. <TABLE width=133 border=1 cellPadding=2 cellSpacing=0 bordercolor="#333399" bgColor=#ddeeff>
  15. <TBODY>
  16. <TR>
  17. <TD bgColor=#5588cc>
  18. <TABLE width="100%" border=1 cellPadding=0 cellSpacing=0 bordercolor="#FFFFFF">
  19. <TBODY>
  20. <TR>
  21. <TD align=middle>
  22. <div align="center"><FONT face=verdana,arial,sans-serif color=#ffffff size=2><B>My Favorites</B></FONT></div>
  23. </TD>
  24. </TR>
  25. </TBODY>
  26. </TABLE>
  27. </TD>
  28. </TR>
  29. <TR>
  30. <TD>
  31. <div align="center"><FONT face=verdana,arial,sans-serif color=#000000 size=1>Which is your favorite Flower?<BR>
  32. </FONT>
  33. <TABLE border=1 bordercolor="#333399">
  34. <TBODY>
  35. <TR>
  36. <TD vAlign=top>
  37. <INPUT type=radio value=Daisy name=Flower>
  38. </TD>
  39. <TD vAlign=center><FONT face=verdana,arial,sans-serif color=#000000
  40. size=1>Daisy</FONT></TD>
  41. </TR>
  42. <TR>
  43. <TD vAlign=top>
  44. <INPUT type=radio value=Rose name=Flower>
  45. </TD>
  46. <TD vAlign=center><FONT face=verdana,arial,sans-serif color=#000000
  47. size=1>Rose</FONT></TD>
  48. </TR>
  49. <TR>
  50. <TD vAlign=top>
  51. <INPUT type=radio value=Tulip name=Flower>
  52. </TD>
  53. <TD vAlign=center><FONT face=verdana,arial,sans-serif color=#000000
  54. size=1>Tulip</FONT></TD>
  55. </TR>
  56. <TR>
  57. <TD vAlign=top>
  58. <input type=radio value=Lily name=Flower>
  59. </TD>
  60. <TD vAlign=center><FONT face=verdana,arial,sans-serif color=#000000
  61. size=1>Lily</FONT></TD>
  62. </TR>
  63. <TR>
  64. <TD vAlign=top>
  65. <INPUT type=radio value=Orchid name=Flower>
  66. </TD>
  67. <TD vAlign=center><FONT face=verdana,arial,sans-serif color=#000000
  68. size=1>Orchid</FONT></TD>
  69. </TR>
  70. <TR>
  71. <TD vAlign=top>
  72. <INPUT type=radio value=Lilac name=Flower>
  73. </TD>
  74. <TD vAlign=center><FONT face=verdana,arial,sans-serif color=#000000
  75. size=1>Lilac</FONT></TD>
  76. </TR>
  77. <TR>
  78. <TD vAlign=top>
  79. <INPUT type=radio value=Other name=Flower>
  80. </TD>
  81. <TD vAlign=center><FONT face=verdana,arial,sans-serif color=#000000
  82. size=1>Other</FONT></TD>
  83. </TR>
  84. </TBODY>
  85. </TABLE>
  86. <BR>
  87. </div>
  88. </TD>
  89. </TR>
  90. </TBODY>
  91. </TABLE>
  92. </th>
  93. <br>
  94. <br>
  95. <table width="100%" cellspacing="5" cellpadding="5">
  96. <tr>
  97. <th scope="col">
  98. <INPUT type=submit value="Submit Vote" name=poll_submit>
  99. </th>
  100. </tr>
  101. </table>
  102. <br>
  103. </FORM>
  104. </body>
  105. </html>

[php]
</head>
<body>
<form action="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>
[/php]
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Elle0000 is offline Offline
4 posts
since Oct 2006
Oct 27th, 2006
0

Re: Need Help with Code

You've got two <?php open tags without a closing tag before the second. (See the red in your original code.)

I'm not quite sure what you're asking about, it looks like you have a decent start on your poll.
Reputation Points: 23
Solved Threads: 23
Posting Pro in Training
Puckdropper is offline Offline
494 posts
since Jul 2004
Oct 28th, 2006
0

Re: Need Help with Code

You've got two <?php open tags without a closing tag before the second. (See the red in your original code.)
I'm not quite sure what you're asking about, it looks like you have a decent start on your poll.
I'm not sure if I have properly created the database, or if the poll results will be stored and properly displayed. I need the results displayed similar to what's 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.

I have reworked some of the code. My immediate problem, is that 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 SELECT statement correct?

Thanks again.
Below is the troublesome code:
[php]
<?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
?>
[/php]
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Elle0000 is offline Offline
4 posts
since Oct 2006
Oct 28th, 2006
0

Re: Need Help with Code

$selectedFlower = Request.Form("Poll2.html");

Does PHP use the dot "." for refering to methods in an object. If it does thats cool, but I thought it used "->".
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Barnz is offline Offline
53 posts
since Jan 2006
Oct 29th, 2006
0

Re: Need Help with Code

Create table statements need run only once per database. I have never felt the need to code them, doing most my stuff with inserts, deletes, and updates.

I think your insert query probably needs single quotes around the field names. Take a look at the manual (or Google) for MySQL and verify that, as I'm not sure.

If you're getting an error, tell use the error you're getting, it helps troubleshoot sometimes. I'm guessing you get "You have an error in your MySQL Syntax"
Reputation Points: 23
Solved Threads: 23
Posting Pro in Training
Puckdropper is offline Offline
494 posts
since Jul 2004
Oct 29th, 2006
0

Re: Need Help with Code

Click to Expand / Collapse  Quote originally posted by Barnz ...
$selectedFlower = Request.Form("Poll2.html");

Does PHP use the dot "." for refering to methods in an object. If it does thats cool, but I thought it used "->".
Thank you again for helping>
I inserted the above code and got this error:
Fatal error: Call to undefined function: form() in /www/cgi/Poll2results.php on line 39

How do I define the function?
I need to request the results to print them. Should my PHP file be in the place of "Poll2.html"?
Did I possibly insert it in the wrong place?
I'm not sure about the "." vs "->". Actually I have no clue. I don't know what the "->" does. But, I'm going to reserach it, and when I find info, I will post it.
Could someone please show me an example of how to calculate a percentage using a variable, and how to print the results?

Reputation Points: 10
Solved Threads: 0
Newbie Poster
Elle0000 is offline Offline
4 posts
since Oct 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: How to insert PHP in HTML language?
Next Thread in PHP Forum Timeline: Storing dynamic form values in Arrays for display & insert





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC