Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I faced the same problem once and solved it by creating a function (or a class static method) to handle my option lists.</p> <p>To print out your options you will have to cycle through them anyway, so while you cycle there is nothing bad in checking for each one if it must be the currently selected.</p> <p>Just create a function like:</p> <pre><code>function printSelectOptions($dataArray, $currentSelection) { foreach ($dataArray as $key =&gt; $value) { echo '&lt;option ' . (($key == $currentSelection) ? 'selected="selected"' : '') . ' value="' . $key . '"&gt;' . $value . '&lt;/option&gt;'; } } </code></pre> <p>Giving <code>$dataArray</code> as your key/value options array, and <code>$currentSelection</code> as the key of the selected option.</p> <p>So in your code you will have just calls to this function, like:</p> <pre><code>&lt;select id='mySelect' name='mySelect'&gt; &lt;?php echo printSelectOptions($gifts, $gift); ?&gt; &lt;/select&gt; </code></pre> <p>This should do the work!</p> <p>EDIT: Adding sample to work with select multiple...</p> <pre><code>&lt;?php $options = array( 1 =&gt; "Test 1", 2 =&gt; "Test 2", 3 =&gt; "Test 3"); $selected = array(1, 3); function printSelectOptions($dataArray, $selectionArray) { foreach ($dataArray as $key =&gt; $value) { echo '&lt;option ' . (in_array($key, $selectionArray) ? 'selected="selected"' : '') . ' value="' . $key . '"&gt;' . $value . '&lt;/option&gt;'; } } ?&gt; &lt;select multiple&gt; &lt;?php echo printSelectOptions($options, $selected); ?&gt; &lt;/select&gt; </code></pre> <p>The trick is passing the selected values as an array of keys, instead of a single key. In the sample I provided dummy arrays, but obviously you'll have to build yours from database... :)</p> <p>EDIT: Select multi with arrays taken from the question...</p> <pre><code>&lt;?php $gifts = array("Standard" =&gt; "Standard", "Gift_ID_Req" =&gt; "Require Program ID", "Not_Enrolled" =&gt; "Do not Enroll"); $gift = array($row['gift_privacy']); function printSelectOptions($dataArray, $selectionArray) { foreach ($dataArray as $key =&gt; $value) { echo '&lt;option ' . (in_array($key, $selectionArray) ? 'selected="selected"' : '') . ' value="' . $key . '"&gt;' . $value . '&lt;/option&gt;'; } } ?&gt; &lt;select multiple&gt; &lt;?php echo printSelectOptions($gifts, $gift); ?&gt; &lt;/select&gt; </code></pre> <p>EDIT: Explode multiple selected values</p> <p>If you have mutliple selected values as a comma separated string, you just have to explode it before passing to the function I provided...</p> <pre><code>$selectedOptionsArray = explode(',', $selectedOptionsString); </code></pre> <p>So the ending code will look like...</p> <pre><code>&lt;?php $gifts = array("Standard" =&gt; "Standard", "Gift_ID_Req" =&gt; "Require Program ID", "Not_Enrolled" =&gt; "Do not Enroll"); $gift = explode(',', $row['gift_privacy']); function printSelectOptions($dataArray, $selectionArray) { foreach ($dataArray as $key =&gt; $value) { echo '&lt;option ' . (in_array($key, $selectionArray) ? 'selected="selected"' : '') . ' value="' . $key . '"&gt;' . $value . '&lt;/option&gt;'; } } ?&gt; &lt;select multiple&gt; &lt;?php echo printSelectOptions($gifts, $gift); ?&gt; &lt;/select&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload