php - Combine function that populates `select` elements with function that pre-selects options matching db values -


i want modify function checkselectedoption don't have hardcode array every time it's called.

the 1rst function pre-selects option matching information in db, uses hardcoded arrays populate select elements. 2nd function creates arrays db values , dynamically populates select elements. how can combine these 2 functions can populate select elements dynamically, , pre-select option matching information in db?

function checkselectedoption populates element values specified in array "$options", , pre-selects option matching information in "customer_info" table of db. function requires arrays hardcoded, , want replace part of function method function printselectoptions uses arrays created db values.

function printselectoptions populates select elements values fetched db. creating array values in column fetched "form_fields" table.

function checkselectedoption

function checkselectedoption($dataarray, $currentselection) {     foreach ($dataarray $key => $value) {         echo '<option ' . (($key == $currentselection) ? 'selected="selected"' : '') . ' value="' . $key . '">' . $value . '</option>';     } } try {   $stmt = $conn->prepare("select * customer_info user_id = :user_id");   $stmt->bindvalue(':user_id', $user_id);  $stmt->execute(); } catch(pdoexception $e) {     echo $e->getmessage(); }  $row = $stmt->fetch(); 

to call function

<select name="first_name">     <?php     // populates element values array $options              $options = array("john"=>"john", "robert"=>"robert");     $selected = $row['first_name'];     echo checkselectedoption($options, $selected);     ?> </select>             

function printselectoptions

function printselectoptions($dataarray, $currentselection, $fieldname) {     $output = '';     foreach ($dataarray $key => $value) {     // add array if row has value     if ($value[$fieldname] !="" && $value[$fieldname] !="null") {             $output .= '<option ' . (($key == $currentselection)) . ' value="' . $key . '">' . str_replace('_', ' ', $value[$fieldname]) . '</option>';         }     }     return $output; } try {       $stmt = $conn->prepare("select * form_fields");       $stmt->execute(); } catch(pdoexception $e) {     echo $e->getmessage(); }  $rows = $stmt->fetchall(); 

to call function

<select name="first_name">     <?php      // dynamically populates element array created column first_name     echo printselectoptions($rows, $currentselection, 'first_name');     ?> </select>   

@metal_fan, i'm not sure you're referring to, can elaborate bit?

600 chars won't enough, guess.

how can combine these 2 functions can populate select elements dynamically , pre-select option matching information in db?

you have fetch data need compare against value "inside" selectbox inside loop (or hell).

php meant used template engine. means, people should use template engine - don't print/echo html tags, inject php "things" instead.

all you're doing doesn't make sense @ all. common solution create helper function calculation.

remember, 1 function should single task. if have several ones, create function each one.


update
well, i'm kinda confused more now, after edit.


// assume array got table looks like: $array = array('john' => 'john', 'dave' => 'dave', 'jason' => 'jason');  $__against__ = 'jason'; // <-- important  function is_selected($expected, $actual){      if ( $expected === $actual ){          print 'selected="selected"';     } }  ?>  <select>      <?php foreach($array $key => $val):?>      <option value="<?php echo $key; ?>" <?php is_selected($key, $__against__); ?>><?php echo $val; ?></option>      <?php endforeach; ?>  </select> 

$__against__ take configuration or that. you're comparing against.

now replace $__against__ = 'jason'; $__against__ = 'dave'; see how works? core idea iterate on $array , compare $key against something. nothing more.


update 2

i guess got idea how "select against" works. didn't understand i've told @ :( @ this:

$output .= '<option ' . is_selected($key, $__against__) . ' value="' . $key . '">' . str_replace('_', ' ', $value[$fieldname]) . '</option>'; 

you're keep making same mistake again , again. php meant used template engine. should avoid printing/echoing html tags. why? if decide replace select element (say jquery combobox plugin). happens then? have re-factor "generation" logic presentation. sounds bad. why sounds bad? because presentation should totally decoupled application/generation logic. why should decoupled? flexibility, maintainability answer question, guess.

keep templates dumb possible should never calculations directly. if want calculate within html template, define function somewhere in bootstrap.php (or like), not inside markup itself.

php meant used template engine. but mean?

this means (we not in perl/python), should avoid code one:

$ouptut = '<option>' . $foo . '</option>'; echo '<table>'; echo '<tr>'; echo '<td>' . $bad . '</td>'; echo '</tr>'; 

but write instead:

<select>   <option><?php echo $foo; ?></option> </select> 

keeping thing in mind, keep html things totally decouple php parser. great future maintains.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -