[thelist] PHP populate dropdowns.
Liam Delahunty
ldelahunty at britstream.com
Fri Nov 29 07:07:01 CST 2002
Jack Guest wrote:
>I'm doing a quick questionnaire using PHP/MYSQL.
>5 of the questions use the same drop down menu which is simply a list of
names in a table.
This query gets a list of choices for this specific vote_id. In this example
people are voting on a favourite song by a particular artist. In my example,
they are choosing a top 10, and the votes get ranked, 10 points for first, 9
for second etc... In this case you can't have multiple votes for the same
song. If you'd like the full code and the db structure please shout.
$query = "SELECT id, choice_name FROM choice_tbl WHERE vote_id = '$vote_id'
";
if ($result = mysql_query($query, $connection) or die ("Error line: "
.__LINE__ . "")) {
while ($myrow = mysql_fetch_row($result)) {
$choice_id = $myrow[0];
$choice_name = stripslashes($myrow[1]);
$song[$choice_id] = $choice_name;
}
}
in my code vote_num_selection is pulled from a database, for you it'd be 5.
for ($i=0; $i<$vote_num_selection; $i++) {
$vote_name = "vote_" . $i;
print ("<tr><td>$nums_array[$i] Choice</td><td>\n");
print ("<select name=\"" . $vote_name . "\">\n");
asort($song);
reset($song);
while (list ($key, $val) = each ($song)) {
print ("<option value=\"$key\"");
if ($key == $$vote_name) { print (" selected"); }
print (">$val\n");
}
print ("</select>\n");
print ("</td></tr>\n");
}
this section is in the if($submit){ ...
for ($i=0; $i<$vote_num_selection; $i++) {
$vote_name = "vote_" . $i;
$vote_array[] = $$vote_name;
}
sort($vote_array);
for ($i=0; $i<count($vote_array); $i++) {
$j=$i+1;
if ($vote_array[$i] == $vote_array[$j]) {
$vote_val = $vote_array[$i];
$err_array[] = "<b>Error.</b> Multiple votes for $song[$vote_val] Please
select $vote_num_selection unique values.";
}
}
if (count($err_array) != 0) {
print ("<p class=err>Please correct the $err_count error(s):<br>");
while (list($key,$val) = each($err_array)) {
print ("$val<br>");
}
print ("</p>");
} else {
print ("<p><b>Cheers $first_name $last_name!</b></p>");
$message = "Thank you for voting on $SERVER_NAME\n";
$message .= "Email $email\n";
$message .= "Name $first_name $last_name\n";
$query = "INSERT INTO contact_tbl (first_name, last_name, email) VALUES
('$first_name', '$last_name', '$email') ";
if ($result = mysql_query($query, $connection)) {
$contact_id = mysql_insert_id($connection);
} else {
printf ("<b>Error: %s\n", mysql_errno () . "</b><br>");
printf ("%s\n", mysql_error () . "<br>");
print ("$query<hr>\n");
}
for ($i=0; $i<$vote_num_selection; $i++) {
$vote_name = "vote_" . $i;
$choice_id = $$vote_name;
$vote_weight = 10 - $i;
$message .= $song[$choice_id] ." $vote_weight points\n";
$query = "INSERT INTO vote_count_tbl
(choice_id, contact_id, vote_weight, ip_address, vote_date) VALUES
('$choice_id', '$contact_id', '$vote_weight', '$ip_address', NOW() )";
if ($result = mysql_query($query, $connection)) {
$j=$i+1;
print ("Vote $j counted.<br>\n");
}
}
Kind regards,
Liam Delahunty
Mega Products Limited, 10-11 Moor Street, Soho, London W1D 5NF
t: 020 7434 4201 f: 0870 135 8412
http://www.liamdelahunty.com/ web/ design/ database/ programming
http://www.britstream.com/ Hosting/ Domain Names From UKP 7.50 p.a.
More information about the thelist
mailing list