nacho
nacho

Reputation: 31

Jqgrid populate a select from a MySql query

I have some issues trying to populate a select in the editform from a Mysql query. I was reading some blogs but I cant find the solution. Below I attach the some code.

This is the collmodel:

jQuery("#grid_pacientes").jqGrid({
    url:'conec_paciente.php',
datatype: "json",
//loadonce: true,
    //
    colNames:['id','Nombre','Apellido','Telefono','Obra Soc'],
colModel:[
    {name:'id_paciente',index:'id_paciente', width:40, sorttype:'int'},
    {name:'nombre',index:'nombre', width:100,editable:true,editoptions:{size:10},resizable:false, sorttype:'text'},
    {name:'apellido',index:'apellido',width:100, editable: true,resizable:false, sorttype:'text'},
    {name:'telefono',index:'telefono',width:100, editable: true,resizable:false, sorttype:'int'},
    {name:'id_obra',index:'id_obra',width:100, editable: true,resizable:false, sorttype:'int',edittype:"select"
           }
        ],

The last coll (id_obra) is the one that I need to fill.

This is the php page where I do the MySql query:

    <?php
include_once 'lib.php';
$conexion= mysql_connect($dbhost, $dbuser, $dbpassword);
mysql_select_db($database, $conexion);
$result = mysql_query("SELECT id_obra, nombre
                       FROM obras", $conexion) or die(mysql_error());
$i=0;
while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
    $responce->rows[$i]['id']=$row["id_obra"];
    $responce->rows[$i]['cell']=array($row["id_obra"],$row["nombre"]);
    $i++;
}
echo json_encode($responce);
?>

So... this is all the code, I think that I need a function but I don't know how to do it. thanks in advance, and sorry by my english

Upvotes: 1

Views: 2578

Answers (2)

nacho
nacho

Reputation: 31

thanks for your help. I found the solution This is the code that I put in the http://url_where_data_will_be_gathered that you told me:

    <select>

<?php
include_once 'lib.php';
$conexion= mysql_connect($dbhost, $dbuser, $dbpassword);
mysql_select_db($database, $conexion);
$result = mysql_query("SELECT id_obra, nombre
                       FROM obras", $conexion) or die(mysql_error());
$i=0;
while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
?>
<option value ="<?php echo $row['id_obra'] ?>"><?php echo $row['nombre'] ?></option>
<?php } ?>

</select>

Upvotes: 2

Diosney
Diosney

Reputation: 10580

@nacho

if you want to get dynamically the dropdown options you have to specify the parameter "editoptions->dataInit" in order to let jqGrid know where to get data.

{
      name:'id_obra',
      index:'id_obra',
      width:100, 
      editable: true,
      resizable:false, 
      sorttype:'int',
      edittype:"select",
      edioptions:{
            dataUrl: "http://url_where_data_will_be_gathered"
      }
}

Notice the editoption object added.

The returned data from the http://url_where_data_will_be_gathered url must be in the format:

 <select>
   <option value='1'>One</option>
   <option value='2'>Two</option>
 </select>

Hoe this help you.

Upvotes: 0

Related Questions