By caelon on
I've googled a bunch of stuff and tried to get it to work but I keep getting hung up.
If I do a single dimension it works:
function setlineup_form($form_state)
{
...
$form['lastname'] = array(
'#type' => 'markup',
'#value' => $player_lastname,
);
$form['firstname'] = array(
'#type' => 'markup',
'#value' => $player_firstname,
);
...
function theme_setlineup_form($form)
{
$output = '<table>';
$output .= '<tr><td>';
$output .= drupal_render($form['lastname']);
$output .= ', ';
$output .= drupal_render($form['firstname']);
$output .= '</td></tr>';
$output .= '</table>';
return $output;
But if I try to feed it multiple elements, it trips:
function setlineup_form($form_state)
{
...
$form[$playerid]['lastname'] = array(
'#type' => 'markup',
'#value' => $player_lastname,
);
$form[$playerid]['firstname'] = array(
'#type' => 'markup',
'#value' => $player_firstname,
);
...
function theme_setlineup_form($form)
{
$output = '<table>';
foreach ($form as $player)
{
$output .= '<tr><td>';
$output .= drupal_render($player['lastname']);
$output .= ', ';
$output .= drupal_render($player['firstname']);
$output .= '</td></tr>';
}
$output .= '</table>';
return $output;
This generates Fatal error: Only variables can be passed by reference on the first drupal_render line.
I've tried flipping the dimensions on the form array to no avail. I've tried to use the foreach as a key=>value and that doesn't help.
I'm sure it's something simple but I've tried just about every variation I can think of to no avail. Most of the examples I found on Google didn't deal with multiple dimensions.
Thoughts?
Comments
Kept looking and came across
Kept looking and came across this:
This works fine although there are some artifacts that I have to parse through. Goes to show there's always more to learn as I have no idea what element_children does; just that it worked in the example I found and now works here.
Implementing something similar...
Hi Caelon:
I am a newbie for drupal and I am trying to implement something like what you have done above. Unfortunately, I am getting the following error, when trying to access my form:
Fatal error: Cannot unset string offsets in /var/www//includes/form.inc on line 490
I would appreciate it if you could share your thoughts on what this might be causing it.
Thanks in advance.
Arun M
Attaching code...
I am getting the following error for the below mentioned code:
Fatal error: Cannot unset string offsets in /var/www/website/includes/form.inc on line 490
hi i have created form like
hi
i have created form like this in table format with 10 text fields
but iam unable to insert values to data base
only last record is inserted
how to insert multiple records from one insert query
Loop through the values and
Loop through the values and either run a query on each loop, or build a long query with your loop and execute the query after the loops.
Contact me to contract me for D7 -> D10/11 migrations.
this is my form where to add
this is my form
where to add loop
<?php
function weatherivr_form() {
$form['weather_values'] = array(
'#type' => 'fieldset',
'#title' => t('weatherivr'),
//'#tree' => TRUE,
'#theme' => 'weatherivr_form',
'#description' => '',
);
$sql = 'select tid, name from term_data where vid = 17';
$db_result = db_query($sql, $vid);
while($row = db_fetch_object($db_result)) {
$val=$row->tid;
$form['weather_values'][$val]['sum'] = array(
'#type' => 'textfield',
'#size' => 10,
'#maxlength' => 20,
'#default_value' => $row->name,
);
$form['weather_values'][$val]['day'] = array(
'#type' => 'textfield',
'#size' => 5,
'#default_value' => $edit['day'],
'#maxlength' => 5,
);
$form['weather_values'][$val]['night'] = array(
'#type' => 'textfield',
'#size' => 5,
//'#default_value' => $val['night'],
'#maxlength' => 5,
);
$form['weather_values'][$val]['forecast'] = array(
'#type' => 'textfield',
'#size' => 5,
'#maxlength' => 5,
);
$form['weather_values'][$val]['wind'] = array(
'#type' => 'textfield',
'#size' => 5,
'#maxlength' => 5,
);
}
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('save'),
'#submit' => array('weatherivr_form_submit'),
);
return $form;
}
function weatherivr_form_submit($form, &$form_state) {
$val1 = array(
array($$form['weather_values'][$val]['sum'],$form['weather_values'][$val]['day'],$form['weather_values'][$val]['night'],$form['weather_values'][$val]['forcast'],$form['weather_values'][$val]['wind']),
array($$form['weather_values'][$val]['sum'],$form['weather_values'][$val]['day'],$form['weather_values'][$val]['night'],$form['weather_values'][$val]['forcast'],$form['weather_values'][$val]['wind']),
array($$form['weather_values'][$val]['sum'],$form['weather_values'][$val]['day'],$form['weather_values'][$val]['night'],$form['weather_values'][$val]['forcast'],$form['weather_values'][$val]['wind']),
);
$sums = $form_state['values']['sum'];
$day = $form_state['values']['day'];
$night = $form_state['values']['night'];
$forecast = $form_state['values']['forecast'];
$wind = $form_state['values']['wind'];
$val1=array(
//array($sums,$day,$night,$forecast,$wind),
array($edit-sum,$edit-day,$edit-night,$edit-forecast,$edit-wind),
);
for ($i=1; $i<=8; $i++) {
foreach ($val1 as $record) {
//$sql="Insert into weatherivr(sums,day,night,forecast,wind) values ('%s', %d, %d, %d, %d)".$sums .$day .$night .$forecast .$wind;
$sql="Insert into weatherivr(sums,day,night,forecast,wind) values ('".$sums."','".$day."','".$night."','".$forecast."','".$wind."')";
db_query($sql, $record['sums'], $record['day'], $record['night'], $record['forcast'], $record['wind']);
echo $sql;
}
}
drupal_set_message('values saved');
}
function theme_weatherivr_form(&$form) {
$header = array(t('sum'), t('day'), t('night'), t('forecast'), t('wind'));
foreach (element_children($form) as $key) {
$row = array();
$row[] = drupal_render($form[$key]['sum']);
$row[] = drupal_render($form[$key]['day']);
$row[] = drupal_render($form[$key]['night']);
$row[] = drupal_render($form[$key]['forecast']);
$row[] = drupal_render($form[$key]['wind']);
$rows[] = $row;
}
//$output = '';
$output = theme('table', $header, $rows);
$output .= drupal_render($form);
return $output;
}
function weatherivr_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'weatherivr-form') {
//$form_state['values']['day']= 'hidden';
}
}
You don't put your loop in
You don't put your loop in the form, you do it in the submit function. Loop through the submitted values and perform the action upon them.
Contact me to contract me for D7 -> D10/11 migrations.
how to assign id for each
how to assign id for each text field or how to create array for each textfield
please it is critical for me