/    Sign up×
Community /Pin to ProfileBookmark

Multiple Choices

i am inserting question in database for multiple choices question

the table in database contains
id, question, right answer, choice 1, choice 2, choice 3

[B]Example:[/B]
Question: What is the capital of England?
Right answer: London
Choice 1: Paris
Choice 2: Amesterdam
Choice 3: New York

[COLOR=”Red”]all that i need is to view the fileds of right answer, choice 1, choice 2, choice 3 in random view so that when refreshing the page the order of multiple choice will change randomly[/COLOR]

[COLOR=”red”][B]Example:[/B][/COLOR]
[B]Please choose the correct answer[/B]
What is the capital of England?
(1- Paris, 2- London , 3- New York, 4- Amesterdam)

After Refresjing the page
the choice will change randomly

(1- London , 2- Paris , 3-Amesterdam , 4- New York)

etc……

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@tirnaAug 18.2010 — array_rand() should do exactly what you want.
Copy linkTweet thisAlerts:
@annaunivrajanAug 18.2010 — Hi John,

Due to some urgent work i could not complete the code. But this will give an idea how to do...



<html>

<head>

<script language="javascript">

var arr=new Array(5);

var randArray=new Array();

arr[1]="Sonia";

arr[2]="Manmohan";

arr[3]="Vajpayee";

arr[4]="Rahul";

var counter=-1

</script>

<script>

function ShowRandomAnswers()

{

// alert('Hi');

getRandomNumber();

/*alert(randArray[0]);
alert(randArray[1]);
alert(randArray[2]);
alert(randArray[3]);*/

pos1=randArray[0];

pos2=randArray[1];

pos3=randArray[2];

pos4=randArray[3];

document.getElementById('a1').innerHTML=arr[pos1];
document.getElementById('a2').innerHTML=arr[pos2];
document.getElementById('a3').innerHTML=arr[pos3];
document.getElementById('a4').innerHTML=arr[pos4];


}

function getRandomNumber()

{

var rand_no = Math.random();

rand_no = rand_no * 4;

var res = Math.ceil(rand_no);

//alert(res);

//alert(isUnique(res));

if(isUnique(res)!="true")

{

if(randArray.length<4)

{

getRandomNumber();

}

}

else

return res;

}



function isUnique(r)

{

var flag=true;
if(randArray.length==0)
{
counter=counter+1;
randArray[counter]=r;

return flag;
}
for(var j=0;j<randArray.length;j++)
{
if(randArray[j]==r){flag=false;break;}

}
if(flag==true)
{
counter=counter+1;
randArray[counter]=r;
return flag;
}


// alert("Array Length : "+randArray.length+"Array Item :"+randArray[counter]);

return false;

}



</script>

</head>

<body onload="ShowRandomAnswers()">

<p> Who is the PM of India ? </p>

<input type="radio" /><button id="a1"/><br />

<input type="radio" /><button id="a2"/><br />

<input type="radio" /><button id="a3"/><br />

<input type="radio" /><button id="a4"/><br />

</body>

</html>



As of now i have shown the data in button. You can put label or font or p tag and set the value dynamically....


Cheers,

P. Rajan
Copy linkTweet thisAlerts:
@tirnaAug 18.2010 — Since the OP put their post in the PHP forum and not the Javascript forum I suspect the OP is looking for a server side solution and not a client side one - especialy since the questions and choices are stored in a database.

If it was me building the application, I would do it all server side as it would be less work because I wouldn't have to transfer all the data in the database to javascript variables.
×

Success!

Help @john_zakaria spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.4,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...