Hey People.
Ok, i'm really hurting here now - my head is about to pop! I'm having difficulties with my most recent project - a questionairring system. The bit which is hurting me is the bit where i have to look through the $POST variables and match these up with the question definitions. I have a series of arrays holding the 'names' of each question, and the user responses (if they exist) are in the $POST array, indexed under the VALUES of the names array. Here is a simple example for a single question, which is a group of three checkboxes:
NAMES ARRAY
option_name[1] = "q1"
option_name[2] = "q2"
option_name[3] = "q3"
$POST ARRAY
$POST[q1] = "monkey"
$POST[q2] = "squirrel"
$POST[q3] = "donkey"
Now, with check boxes, if the user selects all three animals, then the $POST array will have all three entries given above. If the user selects "Monkey" only, then the $POST vars for squirrel and donkey will not exist. I need a way of testing the $_POST array to see which of the possible responses have arrived on the server (i.e. which of the values the user selected).
Unfortunatly, i have decided to be a smart arse here, and put all my code into classes, so the answer to the above question is somewhat obscured. Here is a excert from my code:
<?php
class response_parser
{
var $post_id; //The post id of the post we are currently processing.
var $post_vars;
/**
* Function response_parser - constructor
* @param - $post_id - put into theb class variable. Each instance of this class
* will only deal with ONE post.
*/
function response_parser ($post_id)
{
global $_POST;
$this->post_id = $post_id;
$this->post_vars = $_POST;
}
/**
* Function response_factory
* This function creates the response_holder object for each <q_group>.
* The parameters are the values from the XML, and this function also
* passes the associated user response from $post_id to the response_holder
*/
function response_factory ($option_name)
{
$user_value = getuser_values_3($option_name);
}
function getuser_values_3 ($option_name)
{
$responses = array();
$bool = false;
//simple checkbox list. - user can specify multiple responses for this
//question group. Amalgamate each response into an array which is indexed
//by $option_name
foreach ($option_name as $i => $val) {
if (isset($this->post_vars['$val'])) {
$responses['$val'] = $this->post_vars['$val'];
$bool = true;
}
}
if ($bool == false) {
return $this->return_no_response();
}
else {
return $responses;
}
}
function return_no_response ( )
{
//this function to return an 'indicator array', which can be interpreted
//as meaning there is no $_POST var corresponding to a paticular question.
return array("none" => "none");
}
}
So, the problem i have is that the isset() function doesn't ever return true, therefor i am not picking up the users responses. I have tried using
if (isset(${$this->post_vars['$val']}))
instead, but this doesn't seem to work either. Is there some other function i could use, am i addressing the variable within the array incorrectly, or should i just re-think the whole thing?
Thankyou sanity.
robin