This maybe a very simple question but I am very new to Drupal and have not figured it out yet.
The whole code is listed below. All I need to do in the first step right now is add values the row lets say I want it to say 'test1' in Row1 Column 1 and test2 in Row1 Column 2 and so on how do I do it? ( I have tried using each of the following but they did not work for me: '#value' => t('Row 2 Column 2'), and also tried '#data' => t('Row 2 Column 2'), and also tried '#markup' => t('Row 2 Column 2'), but none of that showed up with any value. What did I do wrong?

my_table.info

name = my_table
description = Example of using a form and then viewing a table. 
core = 7.x

my_table.module:


/**
* Implementation of hook_menu().
*/
function my_table_menu() {
  $items['my_table'] = array(
		'title' => 'Drupal Form example', 
        'type' => MENU_CALLBACK,
        'access arguments' => array('access content'),
        'page callback' => 'drupal_get_form',
        'page arguments'=>array('my_table_form'));

  return $items;
}


function my_table_form($form, &$form_state)
{
  $form['some_value'] = array
  (
    '#type' => 'textfield',
  );
  $form['some_submit'] = array
  (
    '#type' => 'submit',
    '#value' => t('Submit'),
  );
  

  // This is where you'll create your table after submission
  if(isset($form_state['storage']['some_value']))
  {
  $form['table1'] = array(
				'#theme' => 'table',
				'#header' => array(t('Column 1'), t('Column 2')),
				'#rows' => array(
				  // First row.
				  'r1' => array(
					'c1' => array(
					  '#type' => 'textfield',
					  '#title' => t('Row 1 Column 1')
					),
					'c2' => array(
					  '#type' => 'textfield',
					  '#title' => t('Row 1 Column 2'),
					),
				  ),
				  // Second row.
				  'r2' => array(
					'c1' => array(
					  '#type' => 'textfield',
					  '#title' => t('Row 2 Column 1')
					),
					'c2' => array(
					  '#type' => 'textfield',
					  '#title' => t('Row 2 Column 2'),
					),
				  ),
				),
			  );
			  
	
    // Create your table here. The below code is just
    // and example to show that the data exists after submit
    $form['display_value'] = array
    (
      '#markup' => t('The value you entered was !value', array('!value' => $form_state['storage']['some_value'])),
      '#prefix' => '<p>',
      '#suffix' => '</p>',
    );
  }

  return $form;
}



function my_table_form_submit($form, &$form_state)
{
  // Save the value in storage
  $form_state['storage']['some_value'] = $form_state['values']['some_value'];

  // Required - rebuild form. The value will not exist in storage if you do not do this
  $form_state['rebuild'] = TRUE;
}

Comments

refaktor’s picture

I think #default_value is what you're looking for:

'#default_value' => t('Row 2 Column 2')
Random1000000000’s picture

Good Afternoon and thank you for your response.
I have changed the code to include your response but the value is still not displaying. Is there something else I have to do for the code to work?

my_table.module


/**
* Implementation of hook_menu().
*/
function my_table_menu() {
  $items['my_table'] = array(
		'title' => 'Drupal Form example', 
        'type' => MENU_CALLBACK,
        'access arguments' => array('access content'),
        'page callback' => 'drupal_get_form',
        'page arguments'=>array('my_table_form'));

  return $items;
}


function my_table_form($form, &$form_state)
{
  $form['some_value'] = array
  (
    '#type' => 'textfield',
  );
  $form['some_submit'] = array
  (
    '#type' => 'submit',
    '#value' => t('Submit'),
  );
  

  // This is where you'll create your table after submission
  if(isset($form_state['storage']['some_value']))
  {
  $form['table1'] = array(
				'#theme' => 'table',
				'#header' => array(t('Column 1'), t('Column 2')),
				'#rows' => array(
				  // First row.
				  'r1' => array(
					'c1' => array(
					  '#type' => 'textfield',
					  '#title' => t('Row 1 Column 1'),
					  '#default_value' => t('Row 1 Column 1'),
					),
					'c2' => array(
					  '#type' => 'textfield',
					  '#title' => t('Row 1 Column 2'),
					  '#default_value' => t('Row 1 Column 2'),
					),
				  ),
				  // Second row.
				  'r2' => array(
					'c1' => array(
					  '#type' => 'textfield',
					  '#title' => t('Row 2 Column 1'),
					  '#default_value' => t('Row 2 Column 1'),
					),
					'c2' => array(
					  '#type' => 'textfield',
					  '#title' => t('Row 2 Column 2'),
					  '#default_value' => t('Row 2 Column 2'),
					),
				  ),
				),
			  );
			  
	
    // Create your table here. The below code is just
    // and example to show that the data exists after submit
    $form['display_value'] = array
    (
      '#markup' => t('The value you entered was !value', array('!value' => $form_state['storage']['some_value'])),
      '#prefix' => '<p>',
      '#suffix' => '</p>',
    );
  }

  return $form;
}



function my_table_form_submit($form, &$form_state)
{
  // Save the value in storage
  $form_state['storage']['some_value'] = $form_state['values']['some_value'];

  // Required - rebuild form. The value will not exist in storage if you do not do this
  $form_state['rebuild'] = TRUE;
}

refaktor’s picture

Ah sorry I wasn't entirely sure what you were trying to do. If you're simply wanting to display those values in the table you're going to want to make your $form['table1'] like so:

$form['table1'] = array(
  '#theme' => 'table',
  '#header' => array(t('Column 1'), t('Column 2')),
  '#rows' => array(
    array("Row 1 Column 1",'Row 1 Column 2'),
    array("Row 2 Column 1",'Row 2 Column 2'),
  ),
);

You may also want to look at https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_ta...

Random1000000000’s picture

Thank you very much! That worked perfectly.
I have one more question. Can I use that code inside a function? Then call the function inside the main form?

I could not get it to work (if I write the code directly it works but if I call it as a function it does not show any table) but what I am thinking is:
If there is no way to do it like this then what would be the best way to have the code get values from a different function (getting values inside a while loop (I could probably do call by reference but then I would only get the last set of values not the whole loop)?

my_table.module


/**
* Implementation of hook_menu().
*/
function my_table_menu() {
  $items['my_table'] = array(
		'title' => 'Drupal Form example', 
        'type' => MENU_CALLBACK,
        'access arguments' => array('access content'),
        'page callback' => 'drupal_get_form',
        'page arguments'=>array('my_table_form'));

  return $items;
}


function my_table_form($form, &$form_state)
{
  $form['some_value'] = array
  (
    '#type' => 'textfield',
  );
  $form['some_submit'] = array
  (
    '#type' => 'submit',
    '#value' => t('Submit'),
  );
  

  // This is where you'll create your table after submission
  if(isset($form_state['storage']['some_value']))
  {
  		  
	resultsTableExample();  
			  
	
    // Create your table here. The below code is just
    // and example to show that the data exists after submit
    $form['display_value'] = array
    (
      '#markup' => t('The value you entered was !value', array('!value' => $form_state['storage']['some_value'])),
      '#prefix' => '<p>',
      '#suffix' => '</p>',
    );
  }

  return $form;
}

function my_table_form_submit($form, &$form_state)
{
  // Save the value in storage
  $form_state['storage']['some_value'] = $form_state['values']['some_value'];

  // Required - rebuild form. The value will not exist in storage if you do not do this
  $form_state['rebuild'] = TRUE;
}


function resultsTableExample()
{
$form['table2'] = array(
				  '#theme' => 'table',
				  '#header' => array(t('Column 1'), t('Column 2')),
				  '#rows' => array(
					array("Row 1 Column 1",'Row 1 Column 2'),
					array("Row 2 Column 1",'Row 2 Column 2'),
				  ),
				);
}

refaktor’s picture

You'll need to do something like this for your function:

function resultsTableExample() {
  return array(
    '#theme' => 'table',
    '#header' => array(t('Column 1'), t('Column 2')),
    '#rows' => array(
      array("Row 1 Column 1",'Row 1 Column 2'),
      array("Row 2 Column 1",'Row 2 Column 2'),
    ),
  );    
}

And then assign it to the form via

$form['table2'] = resultsTableExample();

You talked about wanting to get external values. You can pass arguments to your function, for example:

function resultsTableExample($rows) {
  return array(
    '#theme' => 'table',
    '#header' => array(t('Column 1'), t('Column 2')),
    '#rows' => $rows,
  );    
}

And when you call it:

$form['table2'] = resultsTableExample($rows);

I did not show it but in this example but $rows was just assigned the array from the previous version of the function at the top of the my_table_form() function. This is just a brief example mind you, I'm not sure exactly what your end goal is here but hopefully this helps you get going.

Random1000000000’s picture

Thank you very much for your above response! I will look at it closer sometime today and give my feedback, I am stuck on a different part now.

On this part my end goal is I have to grab values form a MS SQL server and put them in a table (I have no problem getting the values from MS SQL and assigning them to variables. I can even get them into the table now with a great help from the code you provided but I want to be able to do this as a form (this way I can have multiple tables return on the same page). Right now I only get the first row into the table it is not looping for some reason, I should be seeing two rows not just the first one).

function dco_verification_forms_InventoryTableLookup_form() {
//....does a bunch of stuff like setting up connection string and what not....then we get to the below code
while( $obj = mssql_fetch_object($stmt1)) 

					$form['Inventory_Table_Output'] = array(
						'#theme' => 'table',
						'#header' => array( 
						// creating array that contains data from first cell
						array('data' => t('Assigned Date')),   
						// second cell 
						array('data' => t('Location Name')),   
						// third cell
						array('data' => t('Serial Number')),
						// forth cell 
						array('data' => t('UM Tag Number')), 
						// fifth cell 
						array('data' => t('Category Name')),
						// sixth cell 
						array('data' => t('Catalog Part number')),
						// seventh cell 
						array('data' => t('Note')),
							 
					  ),
					  
						'#rows' => array(
						array("$inv_assigned_date", "$loc_location_name", "$item_serial_number", "$item_um_tag_number", "$category_category_name", "$catalog_part_number", "$inv_note"),
						
					  ),
					  
				  );
				 return $form;
}

Above I'm inside a while loop so why is it only showing the first row in the database?

Now the other way I can actually get this table to work correctly is:

while( $obj = mssql_fetch_object($stmt1)) 
				{
				  
				// creating value that contains array  
				  $header = array( 
					// creating array that contains data from first cell
					array('data' => t('Assigned Date')),   
					// second cell 
					array('data' => t('Location Name')),   
					// third cell
					array('data' => t('Serial Number')),
					// forth cell 
					array('data' => t('UM Tag Number')), 
					// fifth cell 
					array('data' => t('Category Name')),
					// sixth cell 
						array('data' => t('Catalog Part number')),
					// seventh cell 
						array('data' => t('Note')) 	 	
				  ); 
				  // creating first row 
				  $rows[] = array( 
					// output of first cell in 1 row 
					array('data' => t($inv_assigned_date)),  
					// second cell 
					array('data' => t($loc_location_name)),  
					// third cell  
					array('data' => t($item_serial_number)),
					//fourth cell
					array('data' => t($item_um_tag_number)),  
					// fifth cell  
					array('data' => t($category_category_name)),
					// sixth cell  
					array('data' => t($catalog_part_number)),
					// seventh cell  
					array('data' => t($inv_note))   	
				  ); 
				  return theme('table', array('header' => $header, 'rows'=> $rows));
 
				 
				  
				}

Now I did notice in my menu option to get the second method to work I had to do this and with this method I can switch between the two methods mentioned above depending on which return statement I comment out:

'page callback' => 'dco_verification_forms_InventoryTableLookup_Form',

But in the menu if I put it as follows then it only works for the the first option (the one with $form) I'm not exactly sure why that is as I as still learning how Drupal works.

'page callback' => 'drupal_get_form', 
'page arguments'=>array('dco_verification_forms_InventoryTableLookup_Form'),

Now alternatively if we can't get the first method mentioned above to work (the one that has $form) then what I was trying to do (because at least in this way I can get it to loop though the table and show multiple rows was the following (the issue here is I have two tables I need to display on the same page and when I call the first return statement it will only show the first table (it shows it correctly with the loop but only the first how can I show multiple table?)

while( $obj = mssql_fetch_object($stmt1)) 
	{
	  
	// creating value that contains array  
	  $header = array( 
		// creating array that contains data from first cell
		array('data' => t('Assigned Date')),   
		// second cell 
		array('data' => t('Location Name')),   
		// third cell
		array('data' => t('Serial Number')),
		// forth cell 
		array('data' => t('UM Tag Number')), 
		// fifth cell 
		array('data' => t('Category Name')),
		// sixth cell 
			array('data' => t('Catalog Part number')),
		// seventh cell 
			array('data' => t('Note')) 	 	
	  ); 
	  // creating first row 
	  $rows[] = array( 
		// output of first cell in 1 row 
		array('data' => t($inv_assigned_date)),  
		// second cell 
		array('data' => t($loc_location_name)),  
		// third cell  
		array('data' => t($item_serial_number)),
		//fourth cell
		array('data' => t($item_um_tag_number)),  
		// fifth cell  
		array('data' => t($category_category_name)),
		// sixth cell  
		array('data' => t($catalog_part_number)),
		// seventh cell  
		array('data' => t($inv_note))   	
	  ); 
	}
	
while( $obj = mssql_fetch_object($stmt2))
	{
	  // creating value that contains array  
	  $header2 = array( 
		// creating array that contains data from first cell
		array('data' => t('Employee ID')),   
		// second cell 
		array('data' => t('UM Cell')),   
		// third cell s
		array('data' => t('UM Cell2')),
	  ); 
	  // creating first row 
	  $rows2[] = array( 
		// output of first cell in 1 row 
		array('data' => t($employee_id)),  
		// second cell 
		array('data' => t($um_cell)),  
		// third cell  
		array('data' => t($um_cell_2)),
			
	  ); 
	}
	return theme('table', array('header' => $header, 'rows'=> $rows));
	return theme('table', array('header' => $header2, 'rows'=> $rows2));

I apologize this is a lot of information, but I've been at it since yesterday and would really appreciate your insight (I have even read for method 1 the one with $from that I need to do the loop inside the submit statement but I couldn't get it to work), ideally I would want method one to work as it gives me more options to work with but if that is not possible could you tell me how I can have two tables show up on the same page I am guessing the return theme statement has to be combined/modified in some way to show both tables but I have not been successful in getting it to work. Any and all help will be greatly appreciated thank you very much in advance!

refaktor’s picture

Your issue with the first method is that you are returning the same form within the loop. The second method is working because you are running the theme function to create a table for each loop (which is creating separate tables).

For the first method to work I think you need to just run the loop to create multiple arrays for the rows. Remember in the modified version of your original function the rows were defined like so:

'#rows' => array(
  array("Row 1 Column 1",'Row 1 Column 2'),
  array("Row 2 Column 1",'Row 2 Column 2'),
),

So maybe your while loop should just simply do something like:

$rows = array();
while( $obj = mssql_fetch_object($stmt1))  {
  $rows[] = array("$inv_assigned_date", "$loc_location_name", "$item_serial_number", "$item_um_tag_number", "$category_category_name", "$catalog_part_number", "$inv_note"),
}

I'm guessing that those variables are getting different values each time the loop runs but based on the limited code you posted I'm not exactly sure how. Anyway this will then run through the loop and assign all the rows you need and then you just use '#rows' => $rows to display them in the single table defined outside the loop.

Random1000000000’s picture

I just wanted to stop by and say Thank you VERY much for all your help!!
What you suggested did the trick and the table is able to loop also code earlier worked and I passed in $rows with call by reference to the form I needed. and the table showed up on the originating page as expected.

Thanks again!!

refaktor’s picture

Awesome glad you were able to get everything working the way you wanted!