html - PHP Loop inside array -


how can loop inside array in code?

this static version of script:

$val=array( array("value" => "male","label" => "male"),  array("value" => "female","label" => "femal"),   );  my_form("gender","select","gender",$val,"",5); 

then, need retrieve value , label database.

so customized previous code to:

$div=array( while($h = mysql_fetch_array($qwery)){ array("value" => "$h[id]","label" => "$h[div]"),     } );  my_form("division","select","id_div",$div,"",5); 

when run error message:

parse error: syntax error, unexpected 'while' (t_while), expecting ')'

can me?

i want loop join data database :

input type="select"

you can't use loop directly value in array. instead, loop on , add each array each iteration of while-loop.

using $div[] = "value", add value new element in array.

$div = array(); // initialize array   // loop through results while ($h = mysql_fetch_array($qwery)) {      // add new array each iteration     $div[] = array("value" => $h['id'],                     "label" => $h['div']); } 

this create two-dimensjonal array (as example) like

array(     array(         "value" => "male",         "label" => "male"     ),      array(         "value" => "female",         "label" => "female"     )   ); 

...which can through foreach want it.


if want output options in select-element, directly

<select name="sel">     <?php      while ($row = mysql_fetch_array($qwery)) {          echo '<option value="'.$row['id'].'">'.$row['div'].'</option>';     }     ?> </select> 

Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -