I have a table that will have multiple table rows that are generated dynamically using a foreach loop. In these rows will be textfields using the Drupal forms API.


$f = '<table>';
$f .= '<tr><td>Something...</td></tr>';

foreach($somethingAwesome as $val){
    $f .= '<tr><td>';

    $form['blah'][$val] = array(
        '#type' => 'textfield',

    $f .= '</td></tr>';

$f .= '<tr><td>Something else...</td></tr>';
$f .= '</table>';

$form['woot'] = array(
    '#type' => 'whatever',
    '#prefix' => $f

This winds up outputting the form elements after the table in the page. This is not what I am expecting.

The output looks something like:

Something else...


It should look like this:



Something else...

I have tried using the #prefix in the textfield form elements but that produces some odd unexpected (at least for me) behavior.

I am using Drupal 7, if that makes any difference.

Edit: Added that I am in fact adding the $val to the $form element in the foreach loop

  • Sorry I thought I had "Drupal 7" in the title. I am sure of it... was it stripped out? Either way, thank you for the edit @kiamlaluno – rf43 Apr 3 '12 at 21:41
  • 1
    It was in the title; I removed it because the answers are not specific for Drupal 7. Putting "Drupal 7" in the title would make future readers think the answers are specifically for Drupal 7. – apaderno Apr 3 '12 at 21:48
  • Ah, yes... makes perfect sense. Thank you for that insight. – rf43 Apr 3 '12 at 21:50
  • @kiamlaluno that seems like the perfect thing to put in the faq ;-) – Chapabu Apr 3 '12 at 21:56

You could try this if I understand correctly:

$header = '<table>';
$header .= '<tr><td>Something...</td></tr>';

$form['header'] = array(
  '#markup' => $header;

$i = 1;

foreach($somethingAwesome as $val){

    $form['blah_' . $i] = array(
        '#type' => 'textfield';
        '#prefix' => '<tr><td>'
        '#suffix' => '</td></tr>'


$footer = '<tr><td>Something else...</td></tr>';
$footer .= '</table>';

$form['footer'] = array(
    '#markup' => $footer,

This should give you the output:



Something else...

You'll need to do something to differentiate each $form['blah'] part too, otherwise you'll be running a foreach loop that overwrites that specific part of your form on every iteration.

You're running your foreach loop in the place where you want it, but all of the data you want surrounding the form is being put into one variable, and then added in one go as a prefix. Suffix will add data to the end.

General Reference (in case you didn't know):

FormAPI Reference

Specific References:



  • That is how I am doing some other stuff and it works for sure. However, where would I put $f so that it is output? – rf43 Apr 3 '12 at 21:38
  • Aaah...I see what you're trying to do! Give me 5 minutes and I'll edit my answer! – Chapabu Apr 3 '12 at 21:39
  • I think that should work. You mentioned using #prefix gave odd results - hopefully using #suffix as well should rectify this. – Chapabu Apr 3 '12 at 21:48
  • That doesn't work either. It outputs the form and then the textfields just as before, except this time it puts all the stuff within the foreach loop together. That is a positive change lol – rf43 Apr 3 '12 at 22:03
  • lol...oh dear. I think the answer I provided earlier may actually still apply you know. Other than that I'll have to come back to this tomorrow morning unless someone gets here in between. I'm really sorry. One more edit before bed :p – Chapabu Apr 3 '12 at 22:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.