Author Topic: Questions Array not shuffling  (Read 1497 times)

0 Members and 1 Guest are viewing this topic.

Offline doctor-EggmanTopic starter

  • Irregular
  • Posts: 43
    • View Profile
Questions Array not shuffling
« on: March 16, 2010, 08:54:24 AM »
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(
=>'File Transfer Protocol','Force Through Privately','File Through Protocol','File Test Protocol'), 
array(
=> 'Asynchronous JavaScript and XML','All JavaScript and XML','Alternative Java and XML','Actual JavaScript and XML'), 
array(
=> 'Really Simple Syndication','Really Simple Scripting','Ready-Styled Scripting','Really Stupid Syndication'), 
array(
=> 'Cross-site Scripting','Cross-site Security','Cleverly Structured Scripting','eXtremely Safe and Secure'), 
array(
=> 'PHP: Hypertext Preprocessor','Post Hypertext Processor','Practical HTML Processing','Process HTML Prettily'), 
array(
=> 'World Wide Web Consortium','World Wide Web Committee','World Wide Web Creatives','Wakefield Willy Wavers Club'), 
array(
=> 'eXtensible Markup Language','eXtendable Markup Language','Crossover Markup Language','eXtreme Markup Language'), 
array(
=> 'Yahoo User Interface','Yahoo\'s Useful Idea','Yahoo Utility Interface','Yahoo User Interaction'), 
array(
=> 'Hypertext Markup Language','Human Markup Language','Helpful Markup Language','Hypertext Memory Language'), 
array(
=> 'Common Gateway Interface','Common or Garden Interaction','Computer\'s Graphical Intelligence','Common Graphical Interface'), 
array(
=> 'Secure Sockets Layer','Server Security Layer','Server Security Level','Secret Socket Layer'), 
array(
=> 'Structured Query Language','Stupid Query Language','Secure Query Language','Strict Query Language'), 
array(
=> 'Hypertext Transfer Protocol','Hypertext Traffic Protocol','HTML Traffic Transfer Protocol','HTML Through Traffic Protocol'), 
array(
=> 'Cascading Style Sheets','Custom Style Sheets','Clientside Style Sheets','Calculated Style Sheets'), 
array(
=> 'Simple Object Access Protocol','Structured Object Access Protocol','Simple, Objective And Private','Simply Obvious Access Principle'), 
array(
=> 'Web Accessibility Initiative','World Wide Accessibility Intiative','World Wide Accessibility Incorporation','Web Accessibility and Inclusion'), 
array(
=> 'Server-Side Include','Server-Side Intelligence','Scripted Server Include','Secure Server Include'), 
array(
=> 'JavaScript Object Notation','JQuery-Scripting Object Notation','Just Simple Object Notation','JavaScript Over the Net'), 
array(
=> 'eXtensible Stylesheet Language Transformation','eXpandable Stylesheet Language Transfer','eXtensible Stylesheet Language Transfer','eXtendable Stylesheet Language Transformation'), 
array(
=> 'Web Content Accessibility Guidelines','Wakefield Community Action Group','Web Criteria And Guidelines','World-wide Common Access Group'
); 
?>


function shuffle_assoc(&$array) { 
    
$keys array_rand($arraycount($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!


Offline doctor-EggmanTopic starter

  • Irregular
  • Posts: 43
    • View Profile
Re: Questions Array not shuffling
« Reply #1 on: March 16, 2010, 01:53:44 PM »
Can anyone help at all I am really desperate

Online Psycho

  • Guru
  • Freak!
  • *
  • Posts: 7,753
    • View Profile
Re: Questions Array not shuffling
« Reply #2 on: March 16, 2010, 02:00:15 PM »
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.
« Last Edit: March 16, 2010, 02:03:25 PM by mjdamato »
The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net

Offline hcdarkmage

  • Enthusiast
  • Posts: 326
  • Gender: Male
  • I.U.I.Y.G., E.Y.C.B.
    • View Profile
Re: Questions Array not shuffling
« Reply #3 on: March 16, 2010, 02:04:42 PM »
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(
=>'File Transfer Protocol','Force Through Privately','File Through Protocol','File Test Protocol'),  // <--- This belongs to array "0"
array(=> 'Asynchronous JavaScript and XML','All JavaScript and XML','Alternative Java and XML','Actual JavaScript and XML'),  // <--- This belongs to array "1"
array(=> '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.
Stupid fat fiungers.
01000101-01101100-00100000-01000011-01100001-01110010-01100100-01101111-

Online Psycho

  • Guru
  • Freak!
  • *
  • Posts: 7,753
    • View Profile
Re: Questions Array not shuffling
« Reply #4 on: March 16, 2010, 02:10:04 PM »
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.
« Last Edit: March 16, 2010, 02:13:16 PM by mjdamato »
The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net

Offline ksugihara

  • Irregular
  • Posts: 43
    • View Profile
Re: Questions Array not shuffling
« Reply #5 on: March 16, 2010, 02:16:43 PM »
Wouldnt it be smarter to do something like...

array('FTP' => array('File Transer Protocol', 'Force Yada Yada', 'Other Answer Here', 'W T F'));
array('PHP' => array('Blah Blah Blah', 'Blah blah blah', 'Foo Bar', 'Whatever'));

Offline doctor-EggmanTopic starter

  • Irregular
  • Posts: 43
    • View Profile
Re: Questions Array not shuffling
« Reply #6 on: March 16, 2010, 02:19:31 PM »
Sorry I am really terrible at PHP what do you mean when you say you set one of the values to $num?

Offline ksugihara

  • Irregular
  • Posts: 43
    • View Profile
Re: Questions Array not shuffling
« Reply #7 on: March 16, 2010, 02:31:58 PM »
Please. for the love of god and all that is holy. dont do it that way.... here
Code: [Select]
<?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 />";
}
}
?>

Offline doctor-EggmanTopic starter

  • Irregular
  • Posts: 43
    • View Profile
Re: Questions Array not shuffling
« Reply #8 on: March 16, 2010, 02:45:07 PM »
Right now my test is all linked up with a leaderboard and results systems. It wouldnt effect it to much changing it like that would it?

Offline ksugihara

  • Irregular
  • Posts: 43
    • View Profile
Re: Questions Array not shuffling
« Reply #9 on: March 16, 2010, 02:55:47 PM »
All that does is format the questions. unless your leaderboard is something extravagant like keeping track of the answers each person picked, it wont affect anything.

Offline doctor-EggmanTopic starter

  • Irregular
  • Posts: 43
    • View Profile
Re: Questions Array not shuffling
« Reply #10 on: March 16, 2010, 03:01:48 PM »
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(=>'File Transfer Protocol','Force Through Privately','File Through Protocol','File Test Protocol'),  // <--- This belongs to array "0"


Offline ksugihara

  • Irregular
  • Posts: 43
    • View Profile
Re: Questions Array not shuffling
« Reply #11 on: March 16, 2010, 03:06:49 PM »
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.

Offline hcdarkmage

  • Enthusiast
  • Posts: 326
  • Gender: Male
  • I.U.I.Y.G., E.Y.C.B.
    • View Profile
Re: Questions Array not shuffling
« Reply #12 on: March 16, 2010, 03:12:04 PM »
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.
« Last Edit: March 16, 2010, 03:12:57 PM by hcdarkmage »
Stupid fat fiungers.
01000101-01101100-00100000-01000011-01100001-01110010-01100100-01101111-

Offline doctor-EggmanTopic starter

  • Irregular
  • Posts: 43
    • View Profile
Re: Questions Array not shuffling
« Reply #13 on: March 16, 2010, 03:12:46 PM »
I didnt just copy the code. He provided all the source files for it. He even had a working example on his site that shuffled them all about. I think I get it some more. I am terrible at PHP though.

Offline ksugihara

  • Irregular
  • Posts: 43
    • View Profile
Re: Questions Array not shuffling
« Reply #14 on: March 16, 2010, 03:18:05 PM »
Well reaching out to the community is good with PHP, as that is what makes PHP great. Once you get good, give back. keeps the world of PHP going round and round.