Jump to content

stephflix

Recommended Posts

Does anyone know how to do this?

 

Use one multi- dimensional associative array to store the questions, choices and answers for this quiz. Use a nested foreach loop to display the questions and choices. Create and use a simple function to calculate the percentage based on the number correct out of the total number of questions.

Link to comment
Share on other sites

Yes, but we aren't here to do your homework for you. What's the point of taking a class you won't even try to learn from?

 

If you get stuck on a specific bug or concept we can help, but you should turn to your professor or TA first anyway.

Link to comment
Share on other sites

Hi Jessica,

That's completely understandable. You are correct, this is a homework assignment that I've been working on for some time. I just really don't understand it. Here's what I have so far:

 

 

<?php

$questions = array("Rebel Wilson's Character Name","What are Nodes?","What was Fat Amy's Secret Truth?", "What song did Beca audition with?", "What happened at last years finals?");

$answers = array(

array(0 =>'Fat Amy','Rebelicious','Fat Samy','Pitch Slapper'),

array(0 => 'Vocal cords rubbing together at an above average rate w/o proper lubrication','An STD','A high note','A point at which lines or pathways intersect'),

array(0 => 'My real name is Fat Patricia','I cheated on my boyfriends',"I'm kind of a stalker",'Back at my old school I was already a star'),

array(0 => 'Cup Song','Call me Maybe',"Titanium",'No Diggity'),

array(0 => 'Aubrey threw up on stage','They Won',"Chloe had surgery on her vocal chords",'Bumber sabotaged the Barden Belles')

);

 

 

function shuffle_assoc(&$array) {

$keys = array_rand($array, count($array));

foreach($keys as $key)

$new[$key] = $array[$key];

$array = $new;

return true;

}

 

 

I just really don't understand how to use a foreach loop to display the questions and choices.

Is there anything you can do to help me?

Link to comment
Share on other sites

Your function doesn't make any sense, why bother doing a foreach if you're just going to return the first value? Use array_shift() for that. 

 

Your assignment says to use ONE array. You're using several. Your answers aren't related to your questions in any way. Go look at Example #6 in the manual.

http://www.php.net/manual/en/language.types.array.php

 

Once you have your array structured correctly it would be elementary to foreach() through it and list the Q/A.

 

Edit: By the way, the song is called "Cups", and the character's name is Bumper with a p.

 

Edit 2: Arrays start at 0 by default, you don't need to start the first one with a 0 key.

Edited by Jessica
Link to comment
Share on other sites

Ok, here's what I have now:

 

 

 

$questionarray = array(

"What is Rebel Wilsons Character Name?" => array

(

20 => "Fat Amy",

0 => "Rebelicious",

0 => "Fat Samy",

0 => "Pitch Slapper",

 

),

"What are Nodes?" => array

(

0 => "An STD",

20 => "Vocal cords rubbing together at an above average rate w/o proper lubrication",

0 => "A point at which lines or pathways intersect",

0 => "A high note"

),

"What song did Beca audition with?" => array

(

20 => "Cups",

0 => "Call me Maybe",

0 => "Titanium",

0 => "No Diggity"

),

"What happened at last years finals?" => array

(

0 => "They Won",

0 => "Chloe had surgery on her vocal chords",

0 => "Bumper sabotaged the Barden Belles",

20 => "Aubrey threw up on stage"

)

);

 

 

 

// $questionarray is the key and $options is the value (an array)

foreach ($questionarray as $questions => $options) {

 

echo '<p>';

echo $questions;

echo '<br>';

 

// $options is the key and $points is the value

foreach ($options as $answer => $points) {

echo '<input type="radio" name="questions" value="20" id="questions" />';

echo $answer;

echo '<br>';

}

 

echo '</p>';

}

 

echo '<p align="center"><input type="submit" name="submit" value="Submit" /></p>';

 

 

 

It displays my quiz but it only allows me to answer one of the questions. When I click on the radio button for question 2 it erases question 1.

Link to comment
Share on other sites

So, that's a big improvement. Good job :)

 

(Please use the code tags so we can read your code easier).

 

I would not use the actual question as the key. You also cannot have multiple values at the 0 key, so you shouldn't be able to see all your answers...

 

Try something like this

<?php
$quiz = array(
   array(
       'question'=>'Question 1',
       'choices'=>array(
           'Choice 1-1', // Correct
           'Choice 1-2',
           'Choice 1-3',
           'Choice 1-4'
       ),
       'correct_answer'=>1 //Could do either the 0-based array key or the 1-based key, I chose 1 to be visually friendly
   ),
   array(
       'question'=>'Question 2',
       'choices'=>array(
           'Choice 2-1',
           'Choice 2-2',
           'Choice 2-3', // Correct
           'Choice 2-4'
       ),
       'correct_answer'=>3  
   ),
);

 

 

Then make sure each radio set has the name of "question[$id]" where $id is the array key of the current question and the values are the array keys of the answers. You'll have X distinct sets where X is your number of questions.

That will give you an array in $_POST that contains all the question IDs and the related answer ID.

Edited by Jessica
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.