Forum Moderators: coopster

Message Too Old, No Replies

second opinion

         

bysonary

5:00 pm on Sep 30, 2008 (gmt 0)

10+ Year Member



hello,

I need to whip up a quick php quiz application, nothing too fancy so i have come up with this.

MySQL DB: Quiz

id
question
answer1
answer2
answer3
answer4

PHP:


<?php
$server = "localhost";
$user = "username here";
$pass = "password here";
$dbase = "database name here"


mysql_connect($server, $user, $pass);
mysql_select_db($dbase);


$query = "SELECT * FROM quiz";
$result = mysql_query($query);
$totalvotes = $result;


$ans1_query = "SELECT * FROM quiz WHERE answer1 = 1";
$ans1_result = mysql_query($ans1_query);
$totalans1 = $ans1_result;


$ans2_query = "SELECT * FROM quiz WHERE answer2 = 1";
$ans2_result = mysql_query($ans2_query);
$totalans2 = $ans2_result;


$ans3_query = "SELECT * FROM quiz WHERE answer3 = 1";
$ans3_result = mysql_query($ans3_query);
$totalans3 = $ans3_result;


$ans4_query = "SELECT * FROM quiz WHERE answer4 = 1";
$ans2_result = mysql_query($ans4_query);
$totalans4 = $ans4_result;


$percentans1 = round(($$totalans1 / $totalvotes) * 100);
$percentans2 = round(($$totalans2 / $totalvotes) * 100);
$percentans3 = round(($$totalans3 / $totalvotes) * 100);
$percentans4 = round(($$totalans4 / $totalvotes) * 100);


echo "Total votes were". $totalvotes ."!<br/><br/>";
echo "Yes, received ". $$totalans1 ." (". $percentans1 .")<br/>";
echo "No, received ". $$totalans2 ." (". $percentans2 .")<br/>";
echo "Maybe, received ". $$totalans3 ." (". $percentans3 .")<br/>";
echo "Dont know, received ". $$totalans4 ." (". $percentans4 .")<br/>";
echo "<br/>";
echo "Thanks for voting";
?>

I just done this now in about 30 mins quickly, the database is a bit poor, can anyone suggest a better way of implementing the database side of it and then perhaps the code? I assume all the code is in order? I have done this without testing it as php isn't installed here. I am not PHP wizard i just know a little bit and a friend asked me to do this quiz real quick.

Any feedback appreciated. E.g. maybe some way to store the actual answers in a db and have a count field next to that, bit isn't there some issue there when two people vote at exactly the same time?

Thanks
Chris

eelixduppy

2:56 pm on Oct 22, 2008 (gmt 0)



I would try to grab all the answers at once with one query to MySQL then place them into an array.

Knucklehead00

5:01 pm on Oct 23, 2008 (gmt 0)

10+ Year Member



$ans1_query = "SELECT * FROM quiz WHERE answer1 = 1";
$ans1_result = mysql_query($ans1_query);
$totalans1 = $ans1_result;

$ans2_query = "SELECT * FROM quiz WHERE answer2 = 1";
$ans2_result = mysql_query($ans2_query);
$totalans2 = $ans2_result;

$ans3_query = "SELECT * FROM quiz WHERE answer3 = 1";
$ans3_result = mysql_query($ans3_query);
$totalans3 = $ans3_result;

$ans4_query = "SELECT * FROM quiz WHERE answer4 = 1";
$ans2_result = mysql_query($ans4_query);
$totalans4 = $ans4_result;

------------------------------------------------------

Rookie mistake. Query hell. Try this:

$query = "SELECT id, question, answer1, answer2, answer3, answer4 FROM quiz WHERE answer1 = 1 OR answer2 = 1 OR answer3 = 1 OR answer4 = 1";
$result = mysql_query($query) or die(mysql_error());
$totalans = mysql_fetch_array($result);

That will put all of your results into an array, which is where you want them.

Run a FOREACH or FOR loop to grab them our of the array.

Also, your first query is wrong:

$query = "SELECT * FROM quiz";
$result = mysql_query($query);
$totalvotes = $result;

$result returns a Resource ID. As I have no idea how you are planning to calculate totalvotes, you may need to figure something out on that.