[Php] match multi dimensional array with associative array

Gaurish

ex-Mod
Hi,

I am trying to save response out of HTML form which has about 100questions. In question user has to select 1 option, out of three.Now the Objective is to save the responses of the user, so we admin can view them later.I am more interested in their actual response rather than values field. my HTML form goes something like this:

PHP:
<form method="post"  action="save.php"> 

<table width="100%" border="0" cellspacing="0" cellpadding="5" class="main">

  <tr>

    <td colspan="3"> </td>

  </tr>

  <tr>

    <td width="160" valign="top">

[b]Select the most appropriate option[/b]

<table border="0">

<tr><td rowspan="4">

<table border="0">

<tr><td><input type="hidden" name="question-15-order" value="0" /><input type="radio" name="question-15-answer" value="1" /></td><td>I like new ideas when they have practical applications.</td></tr>

<tr><td><input type="radio" name="question-15-answer" value="2" /></td><td>I fall in love with new ideas and concepts for their own sake.</td></tr>

<tr><td><input type="radio" name="question-15-answer" value="3" /></td><td>I'm really in between.</td></tr>

</table>
<table border="0">

<tr><td><input type="hidden" name="question-11-order" value="0" /><input type="radio" name="question-11-answer" value="1" /></td><td>I am a mixer and mingler at parties.</td></tr>

<tr><td><input type="radio" name="question-11-answer" value="2" /></td><td>I prefer one-on-one conversations.</td></tr>

<tr><td><input type="radio" name="question-11-answer" value="3" /></td><td>I'm really in between.</td></tr>

</table>
<snip>

......and so on

Now,to keep track of the actual answers I created an array which hold all the questions and their 3 respective options:

PHP:
$ questions = Array(

    [question-15-answer] => Array

        (

            [1] => I like new ideas when they have practical applications.

            [2] => I fall in love with new ideas and concepts for their own sake.

            [3] => I am really in between.

        )

    [question-11-answer] => Array

        (

            [1] => I am a mixer and mingle at parties.

            [2] => I prefer one-on-one conversations.

            [3] => I am really in between

        )

<snip>

......and so on

also, there $_POST array

PHP:
Array

(

    [question-15-order] => 0

    [question-15-answer] => 3

    [question-11-order] => 0

    [question-11-answer] => 3

<snip>

......and so on

Now, what I want is to match $_POST array's [question-15-answer] value with $questions array's [question-15-answer] value and store that somewhere e.g database table. so admin that view them later. Also, need to ignore the order field on $_POST array but that's not important.

Hoping to find some php coders, so you can guide me as I just started coding with php(4-5days back):ashamed:

{edit}

I need something like this but not sure How do I loop over this:

PHP:
$selected = $_POST[$value];

$choice = $questions[$key][$selected];

echo $choice;


--- Updated Post - Automerged ---

Nobody into php?

anyways, I figured it out:D
 
How did u do it?

My Solution: You can entirely use database for this task, both for generating the HTML form which has questions and also saving the users responses. In this way, a separate GUI to change questions and options can be made later and it can be a good system to design questionnaires. So all the options and responses will be in the database itself, u can only record the users responses.
 
Hi,

Yes, you are right. Instead of writing html by hand, I dynamically generated it. However, I did not use a database. Instead used the $questions array, to generate that. simple and works! here is how my code looks in the end

for generating html

PHP:
<?php for($i=0;$i<46;$i++) { ?>

<table border="0">

<tr><td><input type="hidden" name="question-<?php echo $i; ?>-order" value="" /><input type="radio" name="question-<?php echo $i; ?>-answer" value="1" /></td><td><?php echo $questions[$i][1]; ?></td></tr>

<tr><td><input type="radio" name="question-<?php echo $i; ?>-answer" value="2" /></td><td><?php echo $questions[$i][2]; ?></td></tr>

<tr><td><input type="radio" name="question-<?php echo $i; ?>-answer" value="3" /></td><td><?php echo $questions[$i][3]; ?></td></tr>

</table>
<?php } ?>

Save.php - file which gets form responses via POST method

PHP:
<?php

$questions = array();

$questions = getOptions();

$ans = array();

$ans = $_POST;

$replies = array();

  for ($i = 0; $i < 46; $i++) {

    $selected = $ans['question-'.$i.'-answer'];

    $choice = $questions[$i][$selected];

    $replies[$i] = $choice;

  }

  while (list($key, $value) = each($replies)) {

    $message .= "Answer " . $key . ": " . $value . "\r\n";

  }

  

  while ($row_settings = mysql_fetch_array($rs))

if(mail($toemail, "Personality Test Results of {$cname}", "Personality Test result \r\nCandidate username: {$cname} \r\n \r\n\r\b\r\n $message",

    "From: \"Personality test Results\" <auto-reply@$host>\r\n" .

     "X-Mailer: PHP/" . phpversion())) {

    echo "<h1>Thank you!  </h1>Test Results have been sent to concerned department via Email";

    } else {

      echo "<h1>Message Delivery failed...Take test again or contact support</h1>";

    }

  

?>
 
That array thing is alright but then too how do u get questions and answers in the array? I assume that you do it from an external file.
Isn't the database approach a better option here?
 
Yes, question array is defined in external file. Storing them in database would have been overkill for my purpose. since my app was simple. But yeah, db could could be used to store these as well
 
Back
Top