Jump to content

[SOLVED] Collect 4 answers, put them in one array and random readout


xxtobirichter

Recommended Posts

Hello, I m building a vocabulary trainer for a school project and i m stuck...

I ve done the following

//select 1 correct answers

$answerpick = mysql_query("select id from answer where id='{$image['id']}'");

$answer =mysql_fetch_array($answerpick);

 

//select 3 wrong answers

$wronganswer = mysql_query("select id from answer where id<>'{$image['id']}' order by rand() limit 3");

$wanswer= (mysql_fetch_array($wronganswer))

So far so good, I can read all of them out and display the id.

 

I need to randomly put them into a table. My idea was to put them into one array and then randomly give them out...

 

Another thing, in order to evaluate the answer. I thought off an input form where from a drop down the student can choose (A,B,C,D) each linked to the id of one of the answers...anyone an idea or am i completely off track?

Link to comment
Share on other sites

Ok sure, that s the plant. A vocabulary trainer. From a database we select 1 set of data e.g 1 chinese character, the task is to find the correspondent answer in english.

 

//select 1 character

$imagepick = mysql_query("SELECT id, english, py, cc, sound FROM maintb order by rand() limit 1");

$image = mysql_fetch_array($imagepick);

 

//Then we need 4 answers, one of which has to be equal to the 1 character so the student can pick the right one.

$answerpick = mysql_query("select id from answer where id='{$image['id']}'");

$answer =mysql_fetch_array($answerpick);

 

//we need to select 3 wrong answers

$wronganswerpick = mysql_query("select id from answer where id<>'{$image['id']}' order by rand() limit 3")

$wronganswer=mysql_fetch_array($wronganswerpick);

 

Now the part i m struggeling with. The 4 possible answers have to be mixed and given out. For the mixing i had the idea.

e.g.

$a=

$b=

$c=

$d=

 

$value= array("$a","$b","$c","$d");

$rand_keys=array_rand($value,4);

echo "First random element = ".$value[$rand_keys[0]];

echo "<br>Second random element = ".$value[$rand_keys[1]];

echo "<br>third random element = ".$value[$rand_keys[2]];

echo "<br>third random element = ".$value[$rand_keys[3]];

echo ("<br>")

 

That works fine as long as $A, $B, $C, $D are "static variables" (e.g. words, e.g. $A=Good, $B=Bad etc) but it appears that this does NOT work when

$a=".$wanswer{'id'}.";

 

I m trying to find a solution for that.

If it is possible to assign a variable to ".$wanswer{'id'}." it would also solve the next problem of evaluating the input.

Because next step is,

Choose A,B,C,D then php if $image('id') = Input then echo ("Correct") else echo ("Wrong").

 

I am relativly new to PHP and have tried to find a way to store the array values (e.g. ".$wanswer{'id'}.") in variables for the past 2 or 3 days....

Do you have any suggestions?

Link to comment
Share on other sites

This is the first file

http://www.voc.tobias-richter.com/files/Print%20Image.php

Where i collected the 5 information 1 question 4 answers and have printed them

I copied the code outside so it is visible

 

This is the file with a similar start, but instead of printing them direclty i tried to randomly position them

http://www.voc.tobias-richter.com/files/array%20random%20position%20test.php

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.