Jump to content

Recommended Posts

I'm currently searching the forum and halfway done, but not finding much too similar to my problem.

 

You can view source and try it out, etc. here:

» http://www.phaitaccompli.com/php/arrays2/

 

I am writing a very simple multiple choice quiz. Currently, I'm just doing a small test of it so I can get the mechanics working. Now, I do have a working quiz script I wrote and I have been trying to reference it's workings from. The difference is - the old quiz used associative arrays for questions/answers, which made writing questions/answers tedious (having to do like $q[$i]['c'] = "answer 1" and so on.

 

The new quiz uses the following format:

 

Question 1:,one,two,three,four
Question 2:,five,six,seven,eight
Question 3:,nine,ten,eleven,twelve

 

Practically a .CSV file. I HAVE tried fgetcsv and it seemed to work for spitting out the data as needed, but I've been having problems with the radio buttons so I have just been retooling my code, as such:

 

<FORM action="<?=$PHP_SELF?>" method="POST">

<?php

$file = file("ch1.csv");
foreach($file as $line){                                        // READ EACH LINE
$show = explode(",",$line);                                     // SPLIT ANSWERS
echo "<BR><B>" . $show[0] . "</B><BR>";                         // SHOW QUESTION
for($i=1; $i < count($show); $i++){                                        // LOOP ANSWERS

foreach($show as $value){
}
echo "<INPUT type='radio' name='$value' value='$value'>" . $show[$i] . "<BR>"; // SHOW ANSWERS
}
}

echo "<INPUT type='submit' name='submit' value='Submit!'><BR><BR>";

if(isset($_POST['submit'])){
echo $_POST[$value] . "<BR>";
}
else
{
}
?>
</FORM>

 

Problems:

- after hitting submit, it only displays 'twelve'. If I select any of the LAST 4 options (nine,ten,eleven,twelve), it will always say I chose "twelve". If I select any of the answers for the first 3 questions, it does not echo any chosen value back. The radio button's name needs to have an individual name per question/answer set. I'm unsure how to achieve this exactly.

 

In the script I have printed out the arrays so I can reference how they're seen, and there does seem to be a problem with $show, as it's only displaying the 3rd question/answer set.

 

I have just been going in circles over a week and confusing myself to exhaustion. It's got to be something simple!

 

Thanks.

*UPDATE*

 

I figured out how to get it to echo what you chose, but it's only working for the 3rd question set (i.e. it will now show if I chose any of it's answers). Updated code of LINE 14:

 

echo "<INPUT type='radio' name='$value' value='".$show[$i]."'>" . $show[$i] . "<BR>"; // SHOW ANSWERS

 

Now I need to figure out why it's only accepting input for the 3rd question, and not all consecutively... must be a loop issue.

try something like

<?php
$file = array (
    'Question 1:,one,two,three,four',
    'Question 2:,five,six,seven,eight',
    'Question 3:,nine,ten,eleven,twelve'
);

echo '<form>';
foreach ($file as $k => $line)
{
    $arr = explode (',', $line);
    $q = $arr[0];
    $a = array_slice($arr, 1, 4);
    
    echo $q;
    foreach ($a as $answer)
    {
        echo "<input type='radio' name='ans[$k]' value='$answer'> $answer ";
    }
    echo '<br/>';
}
echo '<input type="submit" name="sub" value="Submit">';
echo '</form>';

if (isset($_GET['sub']))
{
    foreach ($_GET['ans'] as $ans)
    {
        echo $ans, '<br/>';
    }
}
?>  

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.