0

I am working with a html select dropdown list <select></select> in php. I can create my dropdown lists fine as long as they hold just a single value but i am struggling to create the dropdown list if i need a value and a display value, where the value needs to be the id of a hospital and the display value being the hospital name. I am fairly new to php and feel the solution here is just out of my grasp. What i need is below result seen in image 2 but when hospital name is selected it is actualu storing the hospital id. Can someone please assist.....many thanks

Image2

...foreach($records as $record)
            {
                  $dataValues[] = $record->getField('hospitalId');
                  $dataValues[] = $record->getField('hospitalName');            
            }

foreach($dataValues as $vl)
            {
                  $output .= '<option value="' . $vl[0] . '">' . $vl[1] . '</option>';
            }
echo $output;...
2
  • 1
    The array index starts from 0. Did you try to put $vl[0] and $vl[1] instead? Commented Apr 30, 2020 at 15:17
  • Hi Thanks for the response yes you right. I did try that but still do not get a proper list. I get a few single letters Commented Apr 30, 2020 at 15:35

2 Answers 2

1

You can use the key and the value to complete the dropdown.

$output=' <!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
  </head>
  <body>
    <h2>Select box with a placeholder</h2>
    <select name="hospital" required>';
    foreach($records as $record)
            {
                  $dataValues[$record->getField('hospitalId')] = $record->getField('hospitalName');

            }

foreach($dataValues as $id=>$name)
            {
                  $output .= '<option value="' . $id . '">' . $name . '</option>';
            }

$output .= '</select>
  </body>
</html>';
echo $output;

so, i didn't see where you open o close de select tag Here is the html documentation: https://www.w3docs.com/snippets/css/how-to-create-a-placeholder-for-an-html5-select-box-by-using-only-html-and-css.html

Sign up to request clarification or add additional context in comments.

Comments

0
foreach ($records as $record) {
    $output .= '<option value="' . (integer)$record->getField('hospitalId') . '">' 
     . htmlentities($record->getField('hospitalName')) 
     . "</option\n";
}

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.