PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
function polli()
{
require '../dbconn.php';
// what do you do with this, it doesn't appear to be used?
$stringi = "SELECT id FROM polli-1";
// if you want to insert something if the form is posted then you'll
// need a reorganised version of the lines you had before
if(isset($_GET['action']) && $_GET['action'] == 'vote') {
// The values of va and vb can be gathered using a simplified
// version of an if...else statement which uses something called
// the ternary operator
// see the comments above the form as to why $_POST['option'] is
// being tested.
$va = ($_POST['option'] == 'va') ? 1 : 0;
$vb = ($_POST['option'] == 'vb') ? 1 : 0;
// since $va and $vb are numbers they don't need quotes around
// their values
$stringi2 = "INSERT INTO polli-1(id, a, b) VALUES ('', $va, $vb)";
// add some error handling to your mysql_query() call to catch
// any problems
mysql_query($stringi2) or die(mysql_error().'<p>'.$sql.'</p>');
}
// separate out these functions and SQL statement so that you can
// do some error handling.
$sql1 = "SELECT COUNT(a) FROM polli-1 WHERE a=1";
$result1 = mysql_query($sql1) or die(mysql_error().'<p>'.$sql1.'</p>');
$count1 = mysql_result($result1, 0);
$sql2 = "SELECT COUNT(b) FROM polli-1 WHERE b=1";
$result2 = mysql_query($sql2) or die(mysql_error().'<p>'.$sql2.'</p>');
$count2 = mysql_result($result2, 0);
// you can simplify these two if statements into one
if($count1 != 0 && $count2 != 0) {
// using heredoc format you can remove the need for multiple echo
// statements
// your table of results won't work as expected because of the
// 300 width you've set for the table - you can use DIV's around
// the counts to overcome this.
// I was unsure of how this poll was supposed to work but it
// seemed unlikely to work as expected with two differently
// named radio buttons, so I've coded it to be a choice
// between two options. If you want to allow people to choose
// both options then you should use checkboxes instead of
// radio buttons.
echo <<<END
<table width="300">
<tr>
<td>
<form action="?show=poll&action=vote" method="post">
<input name="option" type="radio" value="va">option 1<br />
<input name="option" type="radio" value="vb">option 2<br />
<input type="submit" value="vote" name="do_vote">
</form>
</td>
</tr>
<tr>
<td height="5">
<div style="background-color: #cccccc; width: $count1;">$count1</div>
</td>
</tr>
<tr>
<td height="5"> </td>
</tr>
<tr>
<td height="5">
<div style="background-color: #cccccc; width: $count2;">$count2</div>
</td>
</tr>
</table>
END;
}
} // end func