Jump to content
Larry Ullman's Book Forums

Recommended Posts

I have a form with 20 select fields, each with 5 option values (1-5). For each field I need to get its option value, look up its score in a table, add the score to a total as well as insert all 20 option values into another table along with the other data from the form. I'm thinking array, but not sure how to extract the option value.

 

all the select fields have the same name, but different ids e.g.

<td><label for="moderation">Moderation</label></td>
<td><select name="skillLevel" id="moderation"><option value="1">Select One</option>
<option value="2">Very Confident</option>
<option value="3">Confident</option>
<option value="4">Competent</option>
<option value="5">Not Very Competent</option>
<option value="6">Not Very Confident</option>
</select></td>

 

The fields all have the same name as I'm using a function to create the form.

 

What would be the best way to program this and how do I access the option value? Thanks for any suggestions.

Link to comment
Share on other sites

Thanks Jonathon, could you be more specific? I was thinking along those lines. I can use a foreach loop to get the score for each option selected and add that score to the total score. but I'm not clear on how to get the option value - $_POST['skillLevel']['option']?

Link to comment
Share on other sites

I've tried several approaches with arrays and wildcards to access these select fields. Currently I'm playing around with using a create_form_input function to create the form as follows

if ($type == 'select') {
if ($name == 'skillLevel%') {
 $data = array(1 => 'Select One', 'Very Confident', 'Confident', 'Competent', 'Not Very Competent', 'Not Very Confident');
 }
 // end of $type = select if=elseif

 echo '<select name="' . $name . '" id="' . $label . '"';
  if (array_key_exists($name, $errors)) echo 'class="error"';
  echo '>';

  foreach ($data as $k => $v) {
  echo "<option value=\"$k\"";
  if ($value == $k) echo 'selected="selected"';
  echo ">$v</option>\n";
  } // end foreach
 echo '</select>';
 }

Then I was hoping to use substr to get the particular id of the select field and do the necessary processing. However, the % wildcard character is not being recognised and my form is not being created correctly. I get 2 errors -

Invalid argument supplied for foreach() and Undefined variable: data.

If I include the actual number e.g. skillLevel1, then it works, but there must be a way to use a wildcard? Should I be using regular expressions? Does php have wildcards - I haven't been able to find much online about wildcards. Thanks for any suggestions.

Link to comment
Share on other sites

Currently $name is equal to a literal string. You can just use a regex if you want to.

 

$data will never be created unless $name is equal to 'skillLevel%', which it won't be.

 

Regex's are a bit more intensive on resources, perhaps you could use the substr to achieve confirmation that skillLevel is in the string. Thinking ahead, perhaps it may be best to name your fields skillLevel_1, skillLevel_2 then you could explode() it to return the 'skillLevel' part, which would allow you to continue the process and also you could know the number of the skillLevel.

 

Does that make sense?

Link to comment
Share on other sites

I was able to get the create_form_input function working using

if (preg_match('/skillLevel\d{1,2}$/', $name)) {

but I think your suggestion is a far better approach, thanks Jonathon. I'm a little out of my depth here so will probably be back soon with more questions!

Link to comment
Share on other sites

Well I've got the create_form_input function working and have a form with the select drop downs but I don't know how to access the option value. I know I should know this but I'm drawing a blank.

 

here's the form input

<td><select name="skill_item4" id="moderation"><option value="0" selected="selected">Select One</option>
<option value="1">Very Confident</option>
<option value="2">Confident</option>
<option value="3">Competent</option>
<option value="4">Not Very Competent</option>
<option value="5">Not Very Confident</option>
</select></td>

I want to validate the option to force a selection and store the selection(which I'll call skill_level) along with the skill_item in a table. I've started along these lines

foreach($_POST as $key => $value) {
 if (preg_match('/skill_item\d{1,2}$/', $value) && (filter_var($_POST[how do I access the option value?], FILTER_VALIDATE_INT, array('min_range' => 1)))) {
  $skills = $_POST[$value];

I was thinking $skills would be an associative array which I could then loop through when I'm doing some other processing. Logically I think I'm on the right track but struggling with the code. Could you help? or if you have a better way of doing it, please let me know. Thanks

Link to comment
Share on other sites

if (preg_match('/skill_item\d{1,2}$/', $value)

 

Here $value is equal to the value set in the form. So that validation will always fail. Because the value will be taken from this

 

<option value="2">

 

ie the preg_match is checking to see if preg_match('/skill_item\d{1,2}$/' === 2

 

The $key will be skill_item4

 

so something more like this:

 

$skills = array(); // empty array to store values of multiple skill_item questions

foreach($_POST as $key => $value) {
 if (preg_match('/skill_item\d{1,2}$/', $key)) { // select element is skill_item field

$value = (int) $value; // or further validation if you wish.

 $skills[$key] = $value; // will generate an array like Array ( [skill_item4] => 5 )

} else {
  echo 'Wrong field element';
 }
}

 

It isn't perfect code but it may help you to see how it works

Link to comment
Share on other sites

I went back to basics and realised that I was confusing the array key and its value. Here's what I ended up with

foreach($_POST as $key => $value) {
 if (preg_match('/skill_item\d{1,2}$/', $key))  {
  if (filter_var($value, FILTER_VALIDATE_INT, array('min_range' => 1, 'max_range' => 5)) ){
  $skills = array(substr($key,8) => $value);
  }
 else {
  $survey_errors[$key] = 'Please select a competency level.';
  }
 } // end if preg_match
} //end foreach

Further to your previous post, I have read Larry's PHP and MySQL for Dynamic Web Sites and Effortless Ecommerce but need to firm up on them before moving on to Advanced PHP. It's when you apply the concepts and techniques to your own projects that you realise what you've learned (or not!). Thanks for your replies - I'm sure I'll need more help with this project as I progress.

Link to comment
Share on other sites

Glad you sorted it. Yes you're totally right, anyone can read books upon books about programming and follow it (more or less), but I personally find that I can't progress unless I try to put it into action on my own or use a combination of what I've read to try and provide a solution to whatever I'm trying to do. Most solutions I find, I know the code behind them, it's just i'd never have thought to do it myself, whcih I suppose is the battle. Best of luck with your project anyway :)

Link to comment
Share on other sites

 Share

×
×
  • Create New...