I have a form with a multiple select values. I need to put a default value with #default_value, but I need that the default is also selected to be multiple. These values are caught from db where I saved before.

Right now I have a function that returns an array with the ID terms. The function is as follows;

$form ['group1']['country'] = array(
  '#type' => 'select',
  '#title' => t('Countries'),
  '#required' => TRUE,
  '#multiple' => TRUE,
  '#default_value' => default_list($list = 'country', $node_name = 'case_type')
);
function default_list function($list, $node_name) {
  $result1 = db_query( "SELECT tid FROM "$node_name."_selects WHERE sid = '%s'", $list);
  $default_values = db_fetch_array($result1);
  return $default_values;
}

I found that I returned values in an array, but it do not put in the default form.

Thanks.

Comments

gpk’s picture

You do not seem to have defined #options for the select? http://api.drupal.org/api/file/developer/topics/forms_api_reference.html...

Also when you call default_list() here:

  '#default_value' => default_list ($list = 'country', $node_name = 'case_type')

putting the variable names in the arguments list is redundant (maybe you knew that already..). Just looks a bit odd to me!

gpk
----
www.alexoria.co.uk

atomicman’s picture

I had already filled the array with a function that reflect the values of a taxonomy. What I want is that when the user to select values and returns are selected to form again. So I look in the db values that had selected him and return an array, but are not selected.

gpk’s picture

OK, what does #options look like and what does #default_value look like?

gpk
----
www.alexoria.co.uk

atomicman’s picture

Right now I have a list of countries with a multi-selector:

Europ
  Spain
  England
  France
  Itali
  ...
North America
  USA
  Mexico
  Canada
  ...

It lists continents as non-selectable and countries as selectable values. It catch this values from taxonomy vocabulary. Parent terms are titles, and child terms are selectable values.

When the user selects values of this list, so many values as he wants, these are stored in a table in my db.

So far everything is working well.

My problem is that when I return to edit the form are not selected defaults. I tried to return an array with the results of the db, but I do not select the default. I need these values are selected.

Thank you for your answers, I need help because I just start with Drupal. Thanks.

gpk’s picture

OK I didn't know you could make some of the options non-selectable. How do you do that?

Can you post here the print_r() output of the array that you use to define #options?

And also the array that you are trying to use to set #default?

Without seeing the exact values I can only guess as to what's going on... ;)

gpk
----
www.alexoria.co.uk

atomicman’s picture

Hi! gpk;

I explain how I have done. However, my functions are not yet finished. Since I put it on the forum, if someone knows how I can optimize the queries because I do a number of queries and I imagine that Drupal has a better system for obtaining information from the db, that I don't know yet.

I am preparing a node module. This module has its own table with fields {node_name} but also have another table with fields of selects {node_name_selects}, save when the user selects. The name of the field in the db save in a column called sid (select id), also vid, nid, and tid (for my taxonomy).

This is my form field:

$form['group1']['country'] = array(
			'#type' => 'select',
			'#title' => t('Countries'),
			'#required => TRUE,
			'#multiple' => TRUE,
			'#description' => t('Please identify the country'),
			'#options' => list_of_categories($voc = 'Countries'),
			'#default_value' => default_list($list = 'country',$node_name = 'case_type'),
			'#size' => 10,
			'#ahah' => array(),
		);

Thats my function:

function list_of_categories($voc){
	$result1 	= db_query("SELECT vid FROM {vocabulary} WHERE name = '%s'", $voc);
	$voc_id 	= db_result($result1);
	$result2 	= db_query("SELECT name, tid FROM {term_data} WHERE vid = '%d' ORDER BY tid ASC", $voc_id);
	$hijos = array();
	$papas = array();
	while ($row = db_fetch_array($result2)) {
		$id_row = $row['tid'];
		$result4 	= db_query("SELECT parent FROM {term_hierarchy} WHERE tid = '%s' ORDER BY parent ASC", $id_row);
		$parent_row	= db_result($result4);
		if($parent_row == 0){
			$papas[$id_row]['name'] = $row['name'];
		}
	}
	$result2 	= db_query("SELECT name, tid FROM {term_data} WHERE vid = '%d' ORDER BY tid ASC", $voc_id);
	while ($row = db_fetch_array($result2)) {
		$id_row = $row['tid'];
		$result4 	= db_query("SELECT parent FROM {term_hierarchy} WHERE tid = '%s' ORDER BY parent ASC", $id_row);
		$parent_row	= db_result($result4);
		if($parent_row > 0){
			$name = $papas[$parent_row]['name'];
			$child_name = $row['name'];
			$hijos[$name][$id_row] = $child_name;
		}
	}
	return $hijos;
}

I have another functions that returns diferent types of array. All of this functions need a variable with the name of the vocabulary that I want to show:

This is a function that returns a list of parent terms selectable and child terms with a sign:

function list_of_types($voc){
	$result1 	= db_query("SELECT vid FROM {vocabulary} WHERE name = '%s'", $voc);
	$voc_id 	= db_result($result1);
	$result2 	= db_query("SELECT DISTINCT name, tid FROM {term_data} WHERE vid = '%d' ORDER BY tid", $voc_id);
	$values 	= array();
	while ($row = db_fetch_array($result2)) {
		$id_row = $row['tid'];
		$result4 	= db_query("SELECT parent FROM {term_hierarchy} WHERE tid = '%s' ORDER BY parent ASC", $id_row);
		$parent_row	= db_result($result4);
		if($parent_row == 0){
			$values[$row['tid']] = $row['name'];
		}else{
			$values[$row['tid']] = '- '.$row['name'];
		}
	}
	return $values;
}

This is another functions that returns only parent terms like selectable:

function list_of_papas($voc){
	$result1 	= db_query("SELECT vid FROM {vocabulary} WHERE name = '%s'", $voc);
	$voc_id 	= db_result($result1);
	$result2 	= db_query("SELECT name, tid FROM {term_data} WHERE vid = '%d' ORDER BY tid ASC", $voc_id);
	$papas = array();
	while ($row = db_fetch_array($result2)) {
		$id_row = $row['tid'];
		$result4 	= db_query("SELECT parent FROM {term_hierarchy} WHERE tid = '%s' ORDER BY parent ASC", $id_row);
		$parent_row	= db_result($result4);
		if($parent_row == 0){
			$parent_name = $row['name'];
			$papas[$id_row] = $parent_name;
		}
	}
	return $papas;
}

And this returns the list of my vocabulary.

function list_of_vocabulary(){
	$result1 	= db_query("SELECT name FROM {vocabulary}");
	$list = db_fetch_array($result1);
	return $list;
}

The function that returns me an array with default values are that follows:

function default_list($list, $node_name){
	$result1 	= db_query("SELECT tid FROM {".$node_name."_selects} WHERE sid = '%s'", $list);
	$default_values = db_fetch_array($result1);
	return $default_values;
}

This function needs the name of vocabulary ($list) and the name of the node (my type of content, $node_name).

I suppose that can be improved, if you want to collaborate on what would help. However, my main concern right now is that I return the default values that I ask, namely that I have in the database.

Thanks for your answers and patience.

gpk’s picture

And thanks for your patience too, my only worry is that I may not solve this problem for you!!

The thing I would still like to see is a print_r() or var_dump() of #options, and also #default_value, for a typical case.

Ta,

gpk
----
www.alexoria.co.uk

atomicman’s picture

This is the var_dump of the array. I hope you serve.

Not worry about not solve my problem, investigate and if I find something I would put it in this forum.

Anyway, I have more doubts about drupal, perhaps you can help some. See you later.

array(2) { ["European Countries"]=>  array(36) { [325]=>  string(7) "Austria" [326]=>  string(7) "Belgium" [327]=>  string(22) "Bosnia and Herzegovina" [328]=>  string(8) "Bulgaria" [329]=>  string(7) "Croatia" [330]=>  string(6) "Cyprus" [331]=>  string(14) "Czech Republic" [332]=>  string(7) "Denmark" [333]=>  string(7) "Estonia" [334]=>  string(7) "Finland" [335]=>  string(6) "France" [336]=>  string(16) "FYR of Macedonia" [337]=>  string(7) "Germany" [338]=>  string(6) "Greece" [339]=>  string(7) "Hungary" [340]=>  string(7) "Iceland" [341]=>  string(7) "Ireland" [342]=>  string(5) "Italy" [343]=>  string(6) "Latvia" [344]=>  string(13) "Liechtenstein" [345]=>  string(9) "Lithuania" [346]=>  string(10) "Luxembourg" [347]=>  string(5) "Malta" [348]=>  string(11) "Netherlands" [349]=>  string(6) "Norway" [350]=>  string(6) "Poland" [351]=>  string(8) "Portugal" [352]=>  string(7) "Romania" [353]=>  string(6) "Serbia" [354]=>  string(8) "Slovakia" [355]=>  string(8) "Slovenia" [356]=>  string(5) "Spain" [357]=>  string(6) "Sweden" [358]=>  string(11) "Switzerland" [359]=>  string(6) "Turkey" [360]=>  string(14) "United Kingdom" } ["Other Countries"]=>  array(9) { [362]=>  string(24) "Other european countries" [363]=>  string(12) "Pan european" [364]=>  string(15) "EU Institutions" [365]=>  string(27) "International Organizations" [366]=>  string(6) "Africa" [367]=>  string(4) "Asia" [368]=>  string(25) "Central and South America" [369]=>  string(13) "North America" [370]=>  string(7) "Oceania" } } 
gpk’s picture

OK I think the problem may be that you are using http://api.drupal.org/api/function/db_fetch_array/6 to get the default values, but this fetches one result row from the previous query. What you need to do is fetch all the result rows (each of which contains a single tid), by iterating over $result1, and stick them all into an array.

gpk
----
www.alexoria.co.uk

broon’s picture

Hey there,

I have similar problem and this is the closest thread I found.
I am writing on a content type module and one field contains a multiple select list where you can choose as many options as you want. Saving and retrieving data is no problem but when editing the node how do I define multiple pre-selected values?

I have two arrays, one containing all existing users ($userarray) and one containing only users related to this node ($nodeusers). For a single select I did:

	$form['internal']['uid'] = array(
		'#type' => 'select',
		'#options' => $userarray,
		'#description' => t('Select ONE user.'),
		'#title' => t('Project leader'),
		'#default_value' => isset($node->uid)?$node->uid:''
	);

For a multiple select I add '#multiple' => TRUE, but what should go to '#default_value'?

Thanks in advance,
sin

bestrank’s picture

Just supply an array of values for the '#default_value', for example
'#default_value' => array('1','2')
where 1 and 2 are the option values on your select element