0

I've got this array:

$profession_type = array(
   'Professional Engineers',
   'Accountants',
   'Insurance Professionals',
   'Attorneys',
   'Certified Hazardous Materials Managers',
   'Safety Professional',
   'Industrial Hygienists',
   'IT Professionals',
   'Human Resource'
);

I am display the contents of the array as the options for the select tag:

                        <select name="profession_type[]">
                            <option value=""></option>
EOL;
    foreach ($profession_type as $p){
        print "<option value='" . $p . "'>" . $p . "</option>";
    }
print <<<EOL
                        </select>

I've never pre-filled a drop down box with dynamic values. The values in $profession_type will change frequently (and will eventually be driven from a table in the db), so I can't do hard code it.

EDIT: Sorry my question was unclear.

  • The user will select a value from a previous screen (say it's called id) and hit submit.
  • Before the HTML is rendered to the screen, PHP makes a stored procedure call based on the id they selected.
  • The values that the stored procedures returns will prefill the "profession_type[]" form field.
  • I would like the <option value='accountants' selected>Accountants</option> if the stored procedure returns "Accountants" for the value of "profession_type" based on the id.

Is that more clear? Sorry.

Any suggestions?

7
  • Well why not just fetch profession_type from the database and populate the dropdown with those professions rather than hard coding them ? Commented Sep 1, 2011 at 18:18
  • 1
    What exactly is your question? Commented Sep 1, 2011 at 18:18
  • No suggestions really. What you have now should work just fine. You can interpolate the $p in the double-quoted string for a little bit better readability, but it's fine as you have it. Commented Sep 1, 2011 at 18:19
  • 2
    Either you have to hard code it, or read it in from somewhere else (DB, Text File, etc.). If you're asking what you appear to be doing wrong, there's no separation between your PHP and HTML. If you're not, you need to change your question. Commented Sep 1, 2011 at 18:22
  • Sorry folks, I didn't word my question correctly. I edited the question. Commented Sep 1, 2011 at 18:24

2 Answers 2

2

How about this:

print '<select name="profession_type">';
print '<option value=""></option>';
foreach ($profession_type as $p)
{
    if ($p == $chosen_profession)
        print "<option value='" . $p . "' selected='selected'>" . $p . "</option>";
    else
        print "<option value='" . $p . "'>" . $p . "</option>";
}
print '</select>';
Sign up to request clarification or add additional context in comments.

Comments

1

This goes in your .php file:

<!-- some HTML here -->

<?php
$profession_type = [result_from_database_query] ?>

<!-- more HTML here -->

<select name="profession_type">
<?php
   foreach ($profession_type as $p){
        print "<option value='" . $p . "'>" . $p . "</option>";
    }
?>
</select>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.