A more complete example with functionality to remove items:
function MYMODULE_form($form, &$form_state) {
// If we have some session data set, build a table
if (isset($_SESSION['MYMODULE_data'])) {
$header = array('Food', 'Calories');
$options = array();
$total = 0;
foreach ($_SESSION['MYMODULE_data'] as $item) {
$options[$item['identifier']] = array(
$item['food'],
$item['calories']
);
// Keep a running total
$total += $item['calories'];
}
// Add the total row
$options[] = array('Total calories', $total);
$form['existing_data'] = array(
'#type' => 'tableselect',
'#options' => $options,
'#header' => $header
);
$form['remove_item'] = array(
'#type' => 'submit',
'#value' => 'Remove Selected',
'#submit' => 'MYMODULE_form_remove_item'
);
}
$form['food'] = array(
'#type' => 'textfield',
'#title' => 'Food'
);
$form['calories'] = array(
'#type' => 'textfield',
'#title' => 'Calories'
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit'
);
return $form;
}
function MYMODULE_form_remove_item($form, &$form_state) {
$remove = array_filter($form_state['values']['existing_data']);
foreach ($remove as $key) {
unset($_SESSION['MYMODULE_data'][$key]);
}
$form_state['rebuild'] = TRUE;
}
function my_form_submit($form, &$form_state) {
// this example will just use the food name as the identifier
$_SESSION['MYMODULE_data'][] = array(
'identifier' => $form_state['values']['food'],
'food' => $form_state['values']['food'],
'calories' => $form_state['values']['calories']
);
$form_state['rebuild'] = TRUE;
}
I haven't tested that but it should give you a good starting point even if there are a few bugs.