Saturday 15 February 2014

php - Modify function which dynamically populates select elements to use arrays from db -


I am trying to modify a function that I dynamically call & lt; Select & gt; element to use the array from a database Hard-coded arrays were used to populate the original function elements, and the option to match the dib value was pre-selected.

The modified function creates the element, but it is only adding the first value to the DB. How can I modify it so that it is looping through all the values ​​which & lt; Select & gt; element should be added?

PHP function and query

  & lt ;? Php function print selection option ($ data array, $ current selection) {foreach ($ dataArray $ key = & gt; $ value) {echo '& lt; Option '(($ key == $ current selection))' value = ''. $ Key. ' '& Gt;' . $ Value '& lt; / Option & gt; '; }} {$ Stmt = $ conn- & gt; Try ("Select From Student"); $ Stmt- & gt; Executed (); } Hold (PDOException $ e) {echo $ e-> GetMessage (); } $ Line = $ stmt-> get receive (); ? & Gt;   

Select Paste element

    

Basic Function & amp; Code for a element population

  function print selection option ($ data array, $ current selection) {foreach ($ data array $ key = & gt; $ value) {echo ' Lt; Option '(($ key == $ current selection)?' Selected = 'selected' ':' '). 'Value =' '. $ Key. ' '& Gt;' . $ Value '& lt; / Option & gt; '; }} & Lt; Select name = "fname" & gt; & Lt ;? Php $ options = array ("John" = & gt; "John", "Mary" => "Mary", "Elizabeth" => "Elizabeth"); $ Selected = $ line ['fname']; Echo print selection option ($ option, $ selected); ? & Gt; & Lt; / Select & gt;    

Since you have just received a line through fetch () < / Code>, only one value is being passed in your function printSelectOptions () . Instead, get all the rows through the full line and modify your function, plus an string which is the name of the column (array key) that you want to print.

  // In all rows $ rows ... $ rows = $ stmt- & gt; get receive; all (); // function Accept the full 2D array, and // a string key that is to list the field name: function print select option ($ data array, $ current selection, $ field name) {// string out Output to $ output = ''; Foreach ($ dataArray $ key = & gt; $ value) {// Instead of resonating here, use every $ $ output string in the string / $ field name to use the $ key that is now an array ... $ output. = '& Lt; Option '(($ key == $ current selection))' value = ''. $ Key. ' '& Gt;' . Htmlspecialchars ($ value [$ fieldname], ENT_QUOTES) '& lt; / Option & gt; '; } Return $ Output; }   

Then call the function as:

  echo print selection options ($ rows, $ current selection, 'fname');   

The way in which it is just now, the value of the option is populated by the array array key, which will be counted as zero, it is similar to your original array version, but it is a key column And can be more useful to specify column names like id .

  // It also takes a $ value to use in place of the $ key ... function print selection option ($ data array, $ current selection, $ valuename, $ fieldname) { // output to capture string = output = ''; Foreach ($ dataArray $ key = & gt; $ value) {// Instead of resonating here, use every $ $ output string in the string / $ field name to use the $ key that is now an array ... $ output. = '& Lt; Option '(($ value [$ valuename] == $ current selection))' value = ''. $ Value [$ valuename]. " & Gt; '. Htmlspecialchars ($ value [$ fieldname], ENT_QUOTES) '& lt; / Option & gt; '; } Return $ Output; }   

and it will be called as:

  echo print selection option ($ rows, $ current selection, 'id', 'FNN') ;    

No comments:

Post a Comment