Jump to content

Questions Array not shuffling


doctor-Eggman

Recommended Posts

I am making this quiz following a tutorial I got online. it works by having the correct answer as the first part of the array then has a code to shuffle them other wise every time the correct answer will be the first one you can select. I have applied the code and done everything as it says but it isnt shuffling. Every time the correct answer appears first.

 

Here is the questions and answers array and the code to shuffle it. Can any one give me a hand and help me get it to work?

 

The questions and answers

<?php 
$questions = array('FTP','AJAX','RSS','XSS','PHP','W3C','XML','YUI','HTML','CGI','SSL','SQL','HTTP','CSS','SOAP','WAI','SSI','JSON','XSLT','WCAG'); 
$answers = array( 
array(0 =>'File Transfer Protocol','Force Through Privately','File Through Protocol','File Test Protocol'), 
array(0 => 'Asynchronous JavaScript and XML','All JavaScript and XML','Alternative Java and XML','Actual JavaScript and XML'), 
array(0 => 'Really Simple Syndication','Really Simple Scripting','Ready-Styled Scripting','Really Stupid Syndication'), 
array(0 => 'Cross-site Scripting','Cross-site Security','Cleverly Structured Scripting','eXtremely Safe and Secure'), 
array(0 => 'PHP: Hypertext Preprocessor','Post Hypertext Processor','Practical HTML Processing','Process HTML Prettily'), 
array(0 => 'World Wide Web Consortium','World Wide Web Committee','World Wide Web Creatives','Wakefield Willy Wavers Club'), 
array(0 => 'eXtensible Markup Language','eXtendable Markup Language','Crossover Markup Language','eXtreme Markup Language'), 
array(0 => 'Yahoo User Interface','Yahoo\'s Useful Idea','Yahoo Utility Interface','Yahoo User Interaction'), 
array(0 => 'Hypertext Markup Language','Human Markup Language','Helpful Markup Language','Hypertext Memory Language'), 
array(0 => 'Common Gateway Interface','Common or Garden Interaction','Computer\'s Graphical Intelligence','Common Graphical Interface'), 
array(0 => 'Secure Sockets Layer','Server Security Layer','Server Security Level','Secret Socket Layer'), 
array(0 => 'Structured Query Language','Stupid Query Language','Secure Query Language','Strict Query Language'), 
array(0 => 'Hypertext Transfer Protocol','Hypertext Traffic Protocol','HTML Traffic Transfer Protocol','HTML Through Traffic Protocol'), 
array(0 => 'Cascading Style Sheets','Custom Style Sheets','Clientside Style Sheets','Calculated Style Sheets'), 
array(0 => 'Simple Object Access Protocol','Structured Object Access Protocol','Simple, Objective And Private','Simply Obvious Access Principle'), 
array(0 => 'Web Accessibility Initiative','World Wide Accessibility Intiative','World Wide Accessibility Incorporation','Web Accessibility and Inclusion'), 
array(0 => 'Server-Side Include','Server-Side Intelligence','Scripted Server Include','Secure Server Include'), 
array(0 => 'JavaScript Object Notation','JQuery-Scripting Object Notation','Just Simple Object Notation','JavaScript Over the Net'), 
array(0 => 'eXtensible Stylesheet Language Transformation','eXpandable Stylesheet Language Transfer','eXtensible Stylesheet Language Transfer','eXtendable Stylesheet Language Transformation'), 
array(0 => 'Web Content Accessibility Guidelines','Wakefield Community Action Group','Web Criteria And Guidelines','World-wide Common Access Group') 
); 
?>

 

function shuffle_assoc(&$array) { 
    $keys = array_rand($array, count($array)); 
    foreach($keys as $key) 
        $new[$key] = $array[$key]; 
        $array = $new; 
        return true; 
    } 

 

Also there was this. I dont know if it related.

<?php 
$pattern = ' '; 
$replace = '_'; 
shuffle_assoc($answers[$num]); 
foreach ($answers[$num] as $answer) { 
    $answer2 = str_replace($pattern,$replace,$answer); 
    echo "<li><input type=\"radio\" id=\"$answer2\" value=\"$answer2\" name=\"answers\" />\n"; 
    echo "<label for=\"$answer2\">$answer</label></li>\n"; 
} 
?>

 

Any help anyone could give me would be amazing. Thanks!

 

 

Link to comment
Share on other sites

I just copies all of the code you just posted and set $num to one of the values in the arrays and it works fine for me. Every time I refresh the page the answers are in a different order.

 

Although that is a pretty poor implementation, IMHO.

Link to comment
Share on other sites

I am making this quiz following a tutorial I got online. it works by having the correct answer as the first part of the array then has a code to shuffle them other wise every time the correct answer will be the first one you can select. I have applied the code and done everything as it says but it isnt shuffling. Every time the correct answer appears first.

 

Here is the questions and answers array and the code to shuffle it. Can any one give me a hand and help me get it to work?

I think your problem is in the answers array.

 

<?php 
$questions = array('FTP','AJAX','RSS','XSS','PHP','W3C','XML','YUI','HTML','CGI','SSL','SQL','HTTP','CSS','SOAP','WAI','SSI','JSON','XSLT','WCAG'); 
$answers = array( 
array(0 =>'File Transfer Protocol','Force Through Privately','File Through Protocol','File Test Protocol'),  // <--- This belongs to array "0"
array(0 => 'Asynchronous JavaScript and XML','All JavaScript and XML','Alternative Java and XML','Actual JavaScript and XML'),  // <--- This belongs to array "1"
array(0 => 'Really Simple Syndication','Really Simple Scripting','Ready-Styled Scripting','Really Stupid Syndication'),  // <--- This belongs to array "2", etc.

?>

Change the zeros to correspond with the question array number.

 

Try that, see if it helps.

Link to comment
Share on other sites

I think your problem is in the answers array.

 

Change the zeros to correspond with the question array number.

 

Try that, see if it helps.

 

No, that is not the problem. The 0 is to set the index of the first question value to 0 (which is unnecessary). The questions and answers are indirectly "associated" via the parent array IDs. Which is one reason that is a poor implementation. The code he posted works as long as you set $num to the value of one of the available indexes.

 

It is also a poor implementation because the code is using a custom function to randomize the answers instead of just using shuffle. The custom function is maintaining the index association, but it is not even used in the output so it is unnecessay overhead.

Link to comment
Share on other sites

Please. for the love of god and all that is holy. dont do it that way.... here

<?php
$questions = array('FTP' => 
			array('File Transfer Protocol',
				  'Force Through Privately',
				  'File Through Protocol',
				  'File Test Protocol'), 
			    'Ajax' =>
			array('Asynchronous JavaScript and XML',
				  'All JavaScript and XML',
				  'Alternative Java and XML',
				  'Actual JavaScript and XML'));
foreach($questions as $q => $a) {
echo "What does " . $q . " stand for?<br />";
for($i=0; $i<=4; $i++) {
	echo $a[$i] . "<br />";
}
}
?>

 

Link to comment
Share on other sites

Before I go changing things I set one of the values to $num and nothing happened. I got all this code from a tutorial and it worked in the example there but as soon as download the source files and use it doesnt work.

 

When you said change the values you meant change the zero here to $num right.

array(0 =>'File Transfer Protocol','Force Through Privately','File Through Protocol','File Test Protocol'),  // <--- This belongs to array "0"

 

Link to comment
Share on other sites

Did you copy the code EXACTLY? if so, that comment is idiotically incorrect, and therefore I would not trust this tutorial as far as I can throw it.

 

In your example, 0 is the key of the first value. Not the "array", whatever the hell that means.

 

$array = array(0 => 'answer1', 'answer2', 'answer3')

 

$array[0] = answer1

$array[1] = answer2

$array[2] = answer3

 

Now look at it this way:

 

$array = array('apples' => 'answer1', 'answer2, 'answer3')

 

$array[apples] = answer1

$array[1] = answer2

$array[2] = answer 3

 

 

Does that help you understand how the key => value relationship works? You can also check PHP documentation to get some ideas of this too. If no key is assigned, the array will assume it is the next logical progression of numbers, starting at 0.

Link to comment
Share on other sites

Did you copy the code EXACTLY? if so, that comment is idiotically incorrect, and therefore I would not trust this tutorial as far as I can throw it.

 

Actually, that comment was my fault. It had nothing to do with what was originally posted, it was me mistaking my words. As mjdamato pointed out, I was looking at the problem the wrong way and got myself confused. Ignore the comments that where put in the code.

 

Oh, and thanks for calling me an idiot! It helps remind me that I'm human, lol.

Link to comment
Share on other sites

you said it isnt working for you, and its working for others. So what are you seeing when you run the script. i.e. what does it produce. And what are you seeing when you run the other peoples script.

 

This is what I mean by your result vs the others results.

Link to comment
Share on other sites

For me when i run it, all the correct answers to the questions are just the first one.

 

as in it will be

 

question

correct answer

wrong answer

wrong answer

wrong answer

 

the correct answer is always answer 1 which makes it a bit easy. When I try the example all the answers are shuffled and above mjdamato said that it worked fine for him.

Link to comment
Share on other sites

Could it be a problem with the web server I am using or anything? The version of PHP it is running or anything. it seems weird I am just having this one problem

 

No, it is not a web server problem. The problem has to be that you are doing something outside of the code you posted previously. As I stated, I took the three blocks of code you posted in the initial message and ran it successfully. The only thing I had to do was set a value for $num to specify which question/answers I wanted displayed. To be honest, the code you are using is pretty crappy which has made me reluctant to try and fix your problem.

 

However, the only thing I can think of which would cause your problem would be if you run this line with one value for $num

shuffle_assoc($answers[$num]); 

 

And then run this with a different value for $num

foreach ($answers[$num] as $answer) { 
    $answer2 = str_replace($pattern,$replace,$answer); 
    echo "<li><input type=\"radio\" id=\"$answer2\" value=\"$answer2\" name=\"answers\" />\n"; 
    echo "<label for=\"$answer2\">$answer</label></li>\n"; 
} 

 

Since you never showed how you are setting $num, I can't really do more than guess.

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.