volosoft / jtable

A JQuery plugin to create AJAX based CRUD tables.
http://www.jtable.org
1.1k stars 505 forks source link

Drop downs depending on another table of mysql [Solution] #660

Open ghost opened 11 years ago

ghost commented 11 years ago
function get_dropdown_fromothertable($db_table,$column1, $column1_value,$column2){
$database = new medoo(DB_NAME);
$db_columns = array ($column1,$column2);
$where_parameters = array($column1 => $column1_value);
$select_table = $database->select($db_table,$db_columns,$where_parameters);
$count_table = $database->count($db_table, $where_parameters);
$select_array=array();
foreach($select_table as $data)
{
$select_array[$data[$column]] =  $data[$column2];
}
return json_encode($select_array);
}

I used medoo.php class for a faster solution. $column1 could be for example "id" and $column2 could be the names. So you have an array like $select_array[1]=name1,$select_array[2]=name2 and so on... Then we return the array with json_encode. Finally we can use:

table_columnn: {title: 'a title',width: '20%',listClass: 'center',options: '<?=get_dropdown_fromothertable(PARAMETERS)?>},

to show the options

Please close the issue as this is a solution in php. Outside jtable but effective.

Xinne commented 11 years ago

If you'd like to make an help-article, could you explain the solution a bit better so that no that experienced users can experiment with it as well?

This might work in your situation, but is not compatible with other setups :-(

besides that, props for being prepared to help other enthusiast jtable users!

ghost commented 11 years ago

Let's say you have a table somewhere in your mysql database where you have id's and name's. Id's are numerical values (unique values) and names are the possible options for a select box.

query this table in php and create an array in php format as $array["id"]="name". json_encode the array and echo the results where you would write the options for the select in the first place.

This is plain stuff for not writing 20 or 30 options for a select. Get them from mysql. The function above does just that in a fast way using medoo.php as mysql class and where clauses. Posible for user_id fields.