shorten my if statement

Hello Everybody,

I’m just starting out with php and mysql and I have a question. I have a FORM with 20 questions, all radio buttons. Now when users click on the send button, I need to show information based on each answer. Only my php code is so big so I think and hope there is an easier way to do this. This is my example code:

[php]
if ($_POST[‘question1’] == “A”) {
$data = mysql_query(“SELECT Column1 FROM test”) or die(mysql_error());
echo “Here I echo the column data with a mysql_fetch_array”;
}
else if ($_POST[‘question1’] == “B”) {
$data = mysql_query(“SELECT Column2 FROM test”) or die(mysql_error());
echo “Here I echo the column data with a mysql_fetch_array”;
}
else if ($_POST[‘question1’] == “C”) {
$data = mysql_query(“SELECT Column3 FROM test”) or die(mysql_error());
echo “Here I echo the column data with a mysql_fetch_array”;
}

if ($_POST[‘question2’] == “A”) {
$data = mysql_query(“SELECT Column4 FROM test”) or die(mysql_error());
echo “Here I echo the column data with a mysql_fetch_array”;
}
else if ($_POST[‘question2’] == “B”) {
$data = mysql_query(“SELECT Column5 FROM test”) or die(mysql_error());
echo “Here I echo the column data with a mysql_fetch_array”;
}
else if ($_POST[‘question2’] == “C”) {
$data = mysql_query(“SELECT Column6 FROM test”) or die(mysql_error());
echo “Here I echo the column data with a mysql_fetch_array”;
}
[/php]

Etc. So if I have 20 questions, I have 20 x 3 = 60 if statements. Is there an easier/faster way to do this?

Thanks so much for helping me out with this!

Kind regards,
Mark

First I would make the switch over to mysqli or PDO (my recommendation) for mysql is depreciated, but it you don’t then someone else will have to help you in that department. I personally do want to learn soon-to-be obsolete code. :wink:

Second, why I you using if so many if statements? Let the database do the work for you. I converted over a trivia game that I did from Flash to php, jquery, and ajax. An here’s my query to the database that I use:

[php]$query = ‘SELECT id, question, answerA, answerB, answerC, answerD, correct FROM movieTrivia WHERE id>=:current_id ORDER BY id ASC LIMIT 1’;[/php]

as you can see each question will have an unique id and the correct answer number is stored in the database along with the answers. I think at most I used one if statement for the game logic, I might have used two ---- I can’t remember. I believe I know a little bit about game design/development, for I have a degree in Computer Graphics: Game Design and Interactive Media. I do think I know what I’m talking about. The tricky part (even that is not that tricky once you get the hang of it) is figuring out how to capture the user input. Here’s a link to the game I wrote in php - http://www.jrpepp.com/displayPage.php?page=189 and here’s the actual game - http://www.pepster.com/

Hope this helps.

Hi Strider64,

Thanks for your fast and good response! I am a real beginner so if I need to learn something, I’m willing to do that! So PDO is a good suggestion! But I hope I can fix this problem first :).

Please let me tell you what I want to build. I want something like this:
http://bit.ly/1l3Q0gw

I’ve created a form with radiobuttons with questions, so for example:

Question 1:
Q1A,Q1B

Question 2:
Q2A,Q2B,Q2C

After visitors filled in the answers, the form post it to my results.php. This works perfectly. Now I have a MySQL database with the results (1 per row) and columns with the points per question/answer. So for example:

            Q1 Answer 1 | Q1 Answer 2 | Q2 Answer 1 | etc

Result 1 | 2 | 1 | 3
Result 2 | 3 | 2 | 4
Result 3 | 1 | 1 | 2

So the result with the most points win! Now I am confused. How can I let the database do the work for me, without php if statements? I hope you can explain it to a php beginner :wink:

Thanks so much, really appreciated!!

Kind regards,
Mark

Sponsor our Newsletter | Privacy Policy | Terms of Service