Questions Array not shuffling

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Doctor-Eggman
Forum Newbie
Posts: 19
Joined: Mon Feb 22, 2010 5:12 am

Questions Array not shuffling

Post by Doctor-Eggman »

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

Code: Select all

<?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') 
); 
?>

Code: Select all

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.

Code: Select all

<?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!
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Questions Array not shuffling

Post by AbraCadaver »

Works for me, however I don't see why you need the shuffle_assoc(). You don't have an associative array and it's not multidimensional, so just use shuffle:

Code: Select all

$num = 0
shuffle($answers[$num]);
foreach ($answers[$num] as $answer) {
    echo "$answer\n";
}
first:
File Through Protocol
File Transfer Protocol
Force Through Privately
File Test Protocol

second:
File Transfer Protocol
File Test Protocol
Force Through Privately
File Through Protocol

Keep in mind, you have a 1 in 4 chance of getting the correct answer listed first.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Doctor-Eggman
Forum Newbie
Posts: 19
Joined: Mon Feb 22, 2010 5:12 am

Re: Questions Array not shuffling

Post by Doctor-Eggman »

Do I just replace

Code: Select all

  1. function shuffle_assoc(&$array) {
   2.     $keys = array_rand($array, count($array));
   3.     foreach($keys as $key)
   4.         $new[$key] = $array[$key];
   5.         $array = $new;
   6.         return true;
   7.     }
With the one you posted then? Cause I tried putting it in a few places and keep getting errors.
Doctor-Eggman
Forum Newbie
Posts: 19
Joined: Mon Feb 22, 2010 5:12 am

Re: Questions Array not shuffling

Post by Doctor-Eggman »

I have tried putting it everywhere and I just get errors! I am so lost. Can anyone help?
Post Reply