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

0 Members and 1 Guest are viewing this topic.

Offline doctor-EggmanTopic starter

  • Irregular
  • Posts: 43
    • View Profile
Re: Questions Array not shuffling
« Reply #15 on: March 16, 2010, 03:24:49 PM »
I really dont know why the shuffle works for others and not me. Could some of the other code be effecting it?

Offline ksugihara

  • Irregular
  • Posts: 43
    • View Profile
Re: Questions Array not shuffling
« Reply #16 on: March 16, 2010, 03:28:47 PM »
what is your result? what is your expected result?

Offline doctor-EggmanTopic starter

  • Irregular
  • Posts: 43
    • View Profile
Re: Questions Array not shuffling
« Reply #17 on: March 16, 2010, 03:57:16 PM »
What do you mean?

Offline ksugihara

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

Offline doctor-EggmanTopic starter

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

Offline ksugihara

  • Irregular
  • Posts: 43
    • View Profile
Re: Questions Array not shuffling
« Reply #20 on: March 16, 2010, 04:53:33 PM »
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?

Offline doctor-EggmanTopic starter

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

Offline doctor-EggmanTopic starter

  • Irregular
  • Posts: 43
    • View Profile
Re: Questions Array not shuffling
« Reply #22 on: March 17, 2010, 09:00:38 AM »
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

Online Psycho

  • Guru
  • Freak!
  • *
  • Posts: 7,754
    • View Profile
Re: Questions Array not shuffling
« Reply #23 on: March 17, 2010, 11:46:19 AM »
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.
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 doctor-EggmanTopic starter

  • Irregular
  • Posts: 43
    • View Profile
Re: Questions Array not shuffling
« Reply #24 on: March 17, 2010, 12:15:49 PM »
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.

Offline hcdarkmage

  • Enthusiast
  • Posts: 326
  • Gender: Male
  • I.U.I.Y.G., E.Y.C.B.
    • View Profile
Re: Questions Array not shuffling
« Reply #25 on: March 17, 2010, 12:25:54 PM »
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.

I think I actually see where the problem is that you are running into. In step 4 of the tutorial it shows code for if they are registered or not and then starts the session:
if (isset($_POST['register'])) { // they want to register
    
$username trim(strip_tags(stripslashes($_POST['username'])));
    if (
ini_get('magic_quotes_gpc')) {
        
$username stripslashes($username);
    }
} else { 
//they don't want to register
    
$random rand(1,1000);
    
$username 'Anon'$random;
//end of if/else conditional</strong>
$num 0;

If you notice, there is a $num = 0; just after that section. Check to see if that is in your code. That makes a big difference.
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 #26 on: March 17, 2010, 01:31:09 PM »
	

	
	
$_SESSION['user'] = $username;
	
	
$_SESSION['score'] = 0;
	
	
$_SESSION['correct'] = array(); 
	
	
$_SESSION['wrong'] = array();
	
	
$_SESSION['finished'] = 'no';
	
	
if (isset(
$_SESSION['error']))
	
	
unset(
$_SESSION['error']); 
	
	
$num 0;
	
} else {
	
	
$random rand(1,1000);
	
	
$_SESSION['user'] = 'Anon'$random;
	
	
$_SESSION['score'] = 0;
	
	
$_SESSION['correct'] = array(); 
	
	
$_SESSION['wrong'] = array(); 
	
	
$_SESSION['finished'] = 'no';
	
	
$num 0;
	
}


There is my code. I have the num 0 in there and it still doesnt work!

Offline hcdarkmage

  • Enthusiast
  • Posts: 326
  • Gender: Male
  • I.U.I.Y.G., E.Y.C.B.
    • View Profile
Re: Questions Array not shuffling
« Reply #27 on: March 17, 2010, 01:46:08 PM »
First off, according to the tutorial, your $num=0; is in the wrong place. If you look at the code it looks like this:
if (isset($_POST['register'])) {
    
$username trim(strip_tags(stripslashes($_POST['username'])));
} else {
    
$random rand(1,1000);
    
$username 'Anon'$random;
//end of if/else conditional</strong>
$num 0;
$_SESSION['user'] = $username// username
$_SESSION['score'] = 0// score set to 0
$_SESSION['correct'] = array(); // to hold the user's correct answers
$_SESSION['wrong'] = array(); // to hold the user's incorrect answers
$_SESSION['finished'] = 'no'// they haven't finished the quiz yet


Without knowing your full if-else statement, no one can tell what is going on in that code. It could be that it isn't even calling that if-else statement at all. Plus the includes and placement of your code is important. The tutorial uses 3 pages. it is hard to tell from the snippets of code that you provide whether you have them in the right pages to begin with.
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 #28 on: March 17, 2010, 02:46:11 PM »
I moved the two $nums = 0 's and it did nothing. Answer one is still the right one with no change. I didnt even have to copy the code. He gives all the source files for download. All I had to do was take them and modify the questions. But it still doesnt work. I have looked through the entire tutorial like twice and went over the code and there is no difference I really have no idea why it isnt working for me.

Online Psycho

  • Guru
  • Freak!
  • *
  • Posts: 7,754
    • View Profile
Re: Questions Array not shuffling
« Reply #29 on: March 17, 2010, 03:06:08 PM »
There is obviously something wrong in your code. Attach the files you are using and we can take a look at the full code.
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