Hello,

I have been working on this drupal form API script for past week and half. to give an insight into my problem.. the form below merely lists a host of database records which contain 5 individual scoring ranks. (mind, action, relationship, language and IT).

this code is apart of my own custom module where all values are listed from the database. the idea behind this module is to be able to edit these values on a large scale.

I am having trouble getting the values entered in the form to be passed to the variables inside of the marli_admin_submit function. the second problem is the assigning those values to their specific ID. for this purpose id like to add im merely trying to get just one score updated rather than all of them.

below is my code.

any advice appreciated.

function marli_scores(){
  $result = pager_query(db_rewrite_sql('SELECT * FROM marli WHERE value !=  " "'));

  while ($node = db_fetch_object($result)) {
    $attribute = $node->attribute;
    $field = $node->field_name;
    $item = $node->value;
    $mind = $node->mind;
    $action = $node->action;
    $relationship = $node->relationship;
    $language = $node->language;
    $it = $node->it;
    $form['field'][$node->marli_id] = array('#type' => 'markup', '#value' => $field, '#prefix' => '<b>', '#suffix' => '</b>');
    $form['title'][$node->marli_id] = array('#type' => 'markup', '#value' => $item, '#prefix' => '<b>', '#suffix' => '</b>');
    $form['mind'][$node->marli_id] =  array('#type' => 'textfield', '#maxlength' => '1', '#size' => '1', '#value' => $mind);
    $form['action'][$node->marli_id] = array('#type' => 'textfield', '#maxlength' => '1', '#size' => '1', '#value' => $action);
    $form['relationship'][$node->marli_id] =  array('#type' => 'textfield', '#maxlength' => '1', '#size' => '1', '#value' => $relationship);
    $form['language'][$node->marli_id] = array('#type' => 'textfield', '#maxlength' => '1', '#size' => '1', '#value' => $language);
    $form['it'][$node->marli_id] =  array('#type' => 'textfield', '#maxlength' => '1', '#size' => '1', '#value' => $it);

  }
  $form['pager'] = array('#value' => theme('pager', NULL, 50, 0));
  $form['save'] = array('#type' => 'submit', '#value' => t('Save'));
  $form['#theme'] = 'marli_scores';
  return $form;
}

function marli_admin_submit($form, &$form_state) {

    $marli_id = 4;
    $submit_mind = $form_state['values']['mind'][$marli_id];
    $submit_action = $form_state['values']['action'][$marli_id];
    $submit_relationship = $form_state['values']['relationship'][$marli_id];
    $submit_language = $form_state['values']['language'][$marli_id];
    $submit_it = $form_state['values']['it'][$marli_id];

    $sql_query = "UPDATE  {marli} SET mind =  %d, action =  %d, relationship = %d, language =  %d, it =  %d WHERE  marli_id = %d";


  if ($success = db_query($sql_query, $submit_mind, $submit_action, $submit_relationship, $submit_language, $submit_it)) {

    drupal_set_message(t(' Values have been saved.'));
  }
  else {
    drupal_set_message(t('There was an error saving your data. Please try again.'));
  }

}

Comments

vasi1186’s picture

Hi,

in function "marli_scores", between $form['#theme'] = 'marli_scores'; and "return $form" put "$form['#tree'] = TRUE;" And the function "marli_admin_submit" shouldn't be named "marli_scores_submit"? Or do you change in another place the $form['base'] value?

Vasi.

gaxze’s picture

Below is the rest of the code omitted, the process basically goes:

View form, database collects current scores, I edit values, submit edited form, update database, show changes

sadly im lacking on the latter due to being abit green to Form Api

this whole issue is more complex because im collecting around 500 rows.

function marli_admin() {


$fields = array('ac_sk', 'club_society', 'as_levels', 'a_levels', 'com_vol', 'expiditions', 'gap_year', 'languages', 'pg1', 'pu', 'school', 'sport', 'sport_minor', 'ug1', 'ug2', 'ug3', 'uni_modules', 'work_culture', 'work_exp', 'year1');

foreach ($fields as $selected_field)
{
//get all fields
$sql_q = mysql_query("select field_name from content_node_field_instance WHERE type_name = '$selected_field'");

	while ($check = mysql_fetch_array($sql_q)) {
		$res = $check['field_name'];
		$sql2 = mysql_query("SELECT field_name, global_settings FROM content_node_field WHERE field_name = '$res'");
			while($get = mysql_fetch_array($sql2)) {
			
			$field_name = $get['field_name'];
			$global_settings = $get['global_settings'];
			
			$unserialized_data = unserialize($global_settings);
			
			foreach(explode("\n", $unserialized_data['allowed_values']) as $value) {
			
				$insert_attr = $field_name . " - " . $value;
				
				
				$search = mysql_query("SELECT * FROM marli WHERE attribute = '$insert_attr'");
				$counter = (mysql_num_rows($search));
				
				if($counter == 0) {
				db_query("INSERT INTO `drupal`.`marli` (field_name, value, attribute) VALUES ( '$field_name', '$value', '$insert_attr')");
				
				}
			
			}
			
			
			
			}
		
		
		
		
	}
}


return marli_scores($form);
}

function theme_marli_scores($form) {
// If there are rows in this form, then $form['title'] contains a list of
  // the title form elements.
  $has_posts = isset($form['title']) && is_array($form['title']);
  $select_header = $has_posts ? theme('table_select_header_cell') : '';
  $header = array(t('Field_name'), t('Attribute'), t('Mind'), t('Action'), t('Relationship'), t('Language'), t('IT'));

  if ($has_posts) {
    foreach (element_children($form['title']) as $key) {
      $row = array();
	  $row[] = drupal_render($form['field'][$key]);
	  $row[] = drupal_render($form['title'][$key]);
      $row[] = drupal_render($form['mind'][$key]);
      $row[] = drupal_render($form['action'][$key]);
      $row[] = drupal_render($form['relationship'][$key]);
      $row[] = drupal_render($form['language'][$key]);
	  $row[] = drupal_render($form['it'][$key]);
      $rows[] = $row;
    }

  }
  else {
    $rows[] = array(array('data' => t('No posts available.'), 'colspan' => '7'));
  }

  $output .= theme('table', $header, $rows);
  if ($form['pager']['#value']) {
    $output .= drupal_render($form['pager']);
  }

  $output .= drupal_render($form);

  return $output;
}






gaxze’s picture

Im finding with abit of tweaking the values being returned are always 0. regardless of what i enter into the fields.

vasi1186’s picture

Have you put put $form['#TREE'] = TRUE, as I said in the previous comment?

gaxze’s picture

yes still outputting 0