Hi all,
I made up a dynamic form that gets the celebrity names from a table and makes a radio button form. There are 5 different radio buttons that you can choose, and I want to store a 0 for the ones not selected and a 1 for the one selected (You can only choose 1 per person).
Here is my form code...
if (!isset($_SESSION['names'])) {
$db_server = 'localhost';
$user = '1';
$password = '1';
$database = '1';
$conn = mysql_connect ($db_server, $user, $password);
@mysql_select_db($database) or die( "Unable to select database");
if ($conn === false) {
die('Connection to "' . $db_server . '" failed: ' . mysql_error() . '<br />');
}
$sql = "SELECT * FROM QRatingNames";
$result = mysql_query($sql, $conn) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
$_SESSION['names'][] = $row['Name'];
$_SESSION['checked'][$row['Name']] = 0;
}
}
if (!isset($_POST['submit'])) {
echo '<form action="' . $_SERVER['PHP_SELF'] . '" method="POST">' .
'<table width="647" border="1" align="center" class="font"><br />
<tr><td colspan="6"><div align="center" class="fontbold">TEACHER SURVEY <br />
Take the survey below to tell us which personalities you would like to see in our future video productions for use in your classroom. If you would like to ask your students to participate, please direct them to the “Student” section of our website, where they can access a survey set up especially for them by clicking on “Vote for Your Favorite Celebrities.” <br />
Thank you for your participation! </div></td>
</tr>
<tr>
<td width="147">Name</td>
<td width="100"><div align="center">One of My Favorites!</div></td>
<td width="100"><div align="center">Very Good Personality</div></td>
<td width="100"><div align="center">Good Personality</div></td>
<td width="100"><div align="center">Fair - Poor Personality</div></td>
<td width="100"><div align="center">I\'ve Never Heard of this Person</div></td>
</tr>';
foreach ($_SESSION['names'] as $star) {
echo '<tr><td width="147">' . $star . '</td>';
for ($rating = 1; $rating <= 5; $rating++) {
echo '<td width="118"><input name="N[' . $star . ']" type="radio" ';
if ($rating == $_SESSION['checked'][$star]) {
}
echo 'value="' . $rating . '" /></td>';
}
echo '</tr>';
}
echo '<tr><td colspan="6">' .
'<input type="submit" name="submit" value="Submit" />' .
'<input name="Reset" type="reset" value="Reset" />' .
'<input name="QRatingID" type="hidden" value="1" />' .
'</td></tr></table></form>';
} else {
if (!isset($_POST['N'])) {
die ('<p>You did not vote for anyone, please go back and vote.</p>');
} elseif (count($_POST['N']) !== count($_SESSION['names'])) {
die ('<p>You did not vote for all the celebrities, please go back and finish voting.</p>');
foreach ($_SESSION['names'] as $key => $value) {
if (isset($_POST['N'][$value])) {
$_SESSION['checked'][$value] = $_POST['N'][$value];
} else {
$_SESSION['checked'][$value] = 0;
}
}
} else {
echo '<meta http-equiv="Refresh" content="0; URL=processed.php">';
// do_stuff();
}
}
Here is my table to store the votes...
|-----VotesID----|----NamesID---|---fav_count--|---vg_count---|---g_count---|---fp_count--|--nh_count--|
|---------1--------|-------1---------|--------1-------|-------0--------|------0--------|------0-------|------0-------|
|---------2--------|-------2---------|--------0-------|-------0--------|------0--------|------0-------|------1-------|
|---------3--------|-------1---------|--------0-------|-------1--------|------0--------|------0-------|------0-------|
Here is my code to lookup the results...
$localhost = 'localhost';
$username = '1';
$password = '1';
$database = '1';
mysql_connect($localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
echo '<table><tr>
<td width="150">Name</td>
<td width="100">Favorite</td>
<td width="100">Very Good</td>
<td width="100">Good</td>
<td width="100">Fair</td>
<td width="100">Never Heard</td>
<td width="100">Total Count</td>
<td width="100">QRating</td></tr>';
$query= "SELECT QRatingNames.Name, QRatingNames.ID, SUM(fav_count) as total_fav_count, SUM(vg_count) as total_vg_count, SUM(g_count) as total_g_count, SUM(fp_count) as total_fp_count, SUM(nh_count) as total_nh_count FROM QRatingVotes2, QRatingNames WHERE QRatingVotes2.NamesID=QRatingNames.ID group by QRatingNames.ID";
$result=mysql_query($query);
while($celeb = mysql_fetch_array($result)) {
$name = $celeb['Name'];
$fav_count = $celeb['total_fav_count'];
$vg_count = $celeb['total_vg_count'];
$g_count = $celeb['total_g_count'];
$fp_count = $celeb['total_fp_count'];
$nh_count = $celeb['total_nh_count'];
$total_count = $fav_count + $vg_count + $g_count + $fp_count;
$qRating = round((($fav_count/$total_count)*100),2);
echo '<tr>';
echo '<td>'.$name.'</td>';
echo '<td>'.$fav_count.'</td>';
echo '<td>'.$vg_count.'</td>';
echo '<td>'.$g_count.'</td>';
echo '<td>'.$fp_count.'</td>';
echo '<td>'.$nh_count.'</td>';
echo '<td>'.$total_count.'</td>';
echo '<td>'.$qRating.'%</td>';
echo '</tr>';
}
echo '</table>';
mysql_close();
I am having a brain hiccup and am not seeing what route to take. I tried several and none even seem close.
Thanks,
Don