doctor-Eggman Posted March 16, 2010 Share Posted March 16, 2010 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! Quote Link to comment https://forums.phpfreaks.com/topic/195431-questions-array-not-shuffling/ Share on other sites More sharing options...
doctor-Eggman Posted March 16, 2010 Author Share Posted March 16, 2010 Can anyone help at all I am really desperate Quote Link to comment https://forums.phpfreaks.com/topic/195431-questions-array-not-shuffling/#findComment-1027113 Share on other sites More sharing options...
Psycho Posted March 16, 2010 Share Posted March 16, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/195431-questions-array-not-shuffling/#findComment-1027118 Share on other sites More sharing options...
hcdarkmage Posted March 16, 2010 Share Posted March 16, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/195431-questions-array-not-shuffling/#findComment-1027119 Share on other sites More sharing options...
Psycho Posted March 16, 2010 Share Posted March 16, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/195431-questions-array-not-shuffling/#findComment-1027124 Share on other sites More sharing options...
ksugihara Posted March 16, 2010 Share Posted March 16, 2010 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')); Quote Link to comment https://forums.phpfreaks.com/topic/195431-questions-array-not-shuffling/#findComment-1027130 Share on other sites More sharing options...
doctor-Eggman Posted March 16, 2010 Author Share Posted March 16, 2010 Sorry I am really terrible at PHP what do you mean when you say you set one of the values to $num? Quote Link to comment https://forums.phpfreaks.com/topic/195431-questions-array-not-shuffling/#findComment-1027132 Share on other sites More sharing options...
ksugihara Posted March 16, 2010 Share Posted March 16, 2010 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 />"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/195431-questions-array-not-shuffling/#findComment-1027144 Share on other sites More sharing options...
doctor-Eggman Posted March 16, 2010 Author Share Posted March 16, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/195431-questions-array-not-shuffling/#findComment-1027153 Share on other sites More sharing options...
ksugihara Posted March 16, 2010 Share Posted March 16, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/195431-questions-array-not-shuffling/#findComment-1027164 Share on other sites More sharing options...
doctor-Eggman Posted March 16, 2010 Author Share Posted March 16, 2010 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" Quote Link to comment https://forums.phpfreaks.com/topic/195431-questions-array-not-shuffling/#findComment-1027171 Share on other sites More sharing options...
ksugihara Posted March 16, 2010 Share Posted March 16, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/195431-questions-array-not-shuffling/#findComment-1027174 Share on other sites More sharing options...
hcdarkmage Posted March 16, 2010 Share Posted March 16, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/195431-questions-array-not-shuffling/#findComment-1027178 Share on other sites More sharing options...
doctor-Eggman Posted March 16, 2010 Author Share Posted March 16, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/195431-questions-array-not-shuffling/#findComment-1027180 Share on other sites More sharing options...
ksugihara Posted March 16, 2010 Share Posted March 16, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/195431-questions-array-not-shuffling/#findComment-1027185 Share on other sites More sharing options...
doctor-Eggman Posted March 16, 2010 Author Share Posted March 16, 2010 I really dont know why the shuffle works for others and not me. Could some of the other code be effecting it? Quote Link to comment https://forums.phpfreaks.com/topic/195431-questions-array-not-shuffling/#findComment-1027191 Share on other sites More sharing options...
ksugihara Posted March 16, 2010 Share Posted March 16, 2010 what is your result? what is your expected result? Quote Link to comment https://forums.phpfreaks.com/topic/195431-questions-array-not-shuffling/#findComment-1027195 Share on other sites More sharing options...
doctor-Eggman Posted March 16, 2010 Author Share Posted March 16, 2010 What do you mean? Quote Link to comment https://forums.phpfreaks.com/topic/195431-questions-array-not-shuffling/#findComment-1027215 Share on other sites More sharing options...
ksugihara Posted March 16, 2010 Share Posted March 16, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/195431-questions-array-not-shuffling/#findComment-1027217 Share on other sites More sharing options...
doctor-Eggman Posted March 16, 2010 Author Share Posted March 16, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/195431-questions-array-not-shuffling/#findComment-1027251 Share on other sites More sharing options...
ksugihara Posted March 16, 2010 Share Posted March 16, 2010 well, its tough to say from here what they are doing... have you looked into any of PHP's array shuffling functions? you could implement it yourself? Quote Link to comment https://forums.phpfreaks.com/topic/195431-questions-array-not-shuffling/#findComment-1027255 Share on other sites More sharing options...
doctor-Eggman Posted March 16, 2010 Author Share Posted March 16, 2010 I am really new....and really bad at php. I could find code and things but knowing where to put it and what to do with it is a huge problem for me. Quote Link to comment https://forums.phpfreaks.com/topic/195431-questions-array-not-shuffling/#findComment-1027322 Share on other sites More sharing options...
doctor-Eggman Posted March 17, 2010 Author Share Posted March 17, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/195431-questions-array-not-shuffling/#findComment-1027573 Share on other sites More sharing options...
Psycho Posted March 17, 2010 Share Posted March 17, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/195431-questions-array-not-shuffling/#findComment-1027666 Share on other sites More sharing options...
doctor-Eggman Posted March 17, 2010 Author Share Posted March 17, 2010 http://www.elanman.co.uk/2009/03/make-your-own-php-quiz-part-1/ This was the tutorial I was using, with all the code. Even his example that is pretty much identical to mine just works. Quote Link to comment https://forums.phpfreaks.com/topic/195431-questions-array-not-shuffling/#findComment-1027686 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.