Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP - Poll

Status
Not open for further replies.

BettyJo50

Technical User
Apr 25, 2002
47
US
Hi all!

I have a poll on this site As you can see, it is written in ASP, I need to convert it to PHP. Of course, this is new to me and I do not know PHP, so I need to figure this out. I need the poll to also have an administrative feature that allows someone else (who has authority)to enter a new question and choices.

Could someone please point me in the right direction?

Here is a piece of code that I have so far for the administrative piece, if anyone would like to take a glance and see if I am even on the right track.

Any help would be extremely appreciated!!!

Thanks!!!! :)


<?
usr=&quot;root&quot;;
$pwd=&quot;&quot;;
$db=&quot;poll&quot;;
$host=&quot;10.100.2.51&quot;;
$cid=mysql_connect($host,$usr,$pwd);
?>

<?
$SQL=&quot;SELECT * FROM EVENT_REG WHERE ID='&quot;.$id.&quot;'&quot; ;
$result = mysql_db_query($db, &quot;$SQL&quot;, $cid);
if (!$result) { print(&quot;ERROR: &quot; . mysql_error().&quot;\n$SQL\n&quot;);}
$arrList = mysql_fetch_array($result);
var_dump($arrList);
?>


<?

// connect to database and query
$connection = mysql_connect($host, $usr, $pwd) or die (&quot;Unable to connect!&quot;);

?>


<HTML>
<HEAD>
<TITLE>Display</TITLE>
</HEAD>
<BODY BGCOLOR=&quot;#FFFFFF&quot; >

<table width=&quot;35%&quot; border=&quot;1&quot; cellpadding=&quot;3&quot; height=&quot;369&quot;>
<tr>
<td height=&quot;308&quot;>
<?
$SQL = &quot; SELECT * FROM Poll &quot;;
$result = my_sql_query ($SQL);
mysql_fetch_array($arrPoll)
?>
<form method=&quot;post&quot; action=&quot;display.php&quot;>
<?
=$arrPoll(&quot;Question&quot;)
?>

<br>
<br>
<input type=&quot;radio&quot; name=Answer value=1>
<?
=$arrPoll (&quot;Response_1&quot;)
?>
<p>
<input type=&quot;radio&quot; name=Answer value=1>
<?
=$arrPoll (&quot;Response_2&quot;)
?>
<p>
<input type=&quot;radio&quot; name=Answer value=1>
<?
=$arrPoll (&quot;Response_3&quot;)
?>
<p>
<input type=&quot;radio&quot; name=Answer value=1>
<?
=$arrPoll (&quot;Response_4&quot;)
?>
<p>
<input type=&quot;radio&quot; name=Answer value=1>
<?
=$arrPoll (&quot;Response_5&quot;)
?>
<p>
<input type=&quot;radio&quot; name=Answer value=1>
<?
=$arrPoll (&quot;Response_6&quot;)
?>
<p>
<input type=&quot;radio&quot; name=Answer value=1>
<?
=$arrPoll (&quot;Response_7&quot;)
?>
<p>
<input type=submit name=submit value=&quot;Vote&quot;>
</form>
</td>
</tr>
</table>

<?
if (answer=1) {
$new_value=$arrPoll(&quot;Vote_1&quot;)++

if (answer=2) {
$new_value=$arrPoll(&quot;Vote_2&quot;)++

if (answer=2) {
$new_value=$arrPoll(&quot;Vote_3&quot;)++

if (answer=4) {
$new_value=$arrPoll(&quot;Vote_4&quot;)++

if (answer=5) {
$new_value=$arrPoll(&quot;Vote_5&quot;)++

if (answer=6) {
$new_value=$arrPoll(&quot;Vote_6&quot;)++

if (answer=7) {
$new_value=$arrPoll(&quot;Vote_7&quot;)++
}
}
}
}
}
}
}
?>

<?

// close connection
mysql_close($connection);

?>

</body>
</html>
 
I won't pretend I spent alot of time on it, but two things jump out at me...

first, it's not necessary to keep opening and closing your <? ?> tags, in fact it may hamper performance slightly, each time you do the parser switches contexts. In fact you can leave the whole thing within one big set of tags, and when you want to output HTML just do a print &quot;</body></html>&quot;; for example.

Second, is it looks like your specific code could benefit greatly from arrays and loops, right now you have a hardcoded seven options, but what if it's a yes/no survey? Or what if there are just any number other than 7?

I'd definately check out PHP's arrays for something like this, and best of luck.

-Rob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top