0

I'm trying to make Drupal print a list with two different fields in the same array. So first comes field A, then field B and it prints in this way until the whole array is printed.

The result I'm trying to get is something like

<tr>
  <td>Field_1[value1]</td>
  <td>Field_2[value1]</td>
</tr><tr>
  <td>Field_1[**value'n'**]</td>
  <td>Field_2[**value'n'**]</td>
</tr>

Until all values are printed.

EDIT. Figured out one way of achieving this directly in node--testnode.tpl.php.

<table>
<?php if ($content['field_test'][1]): ?>
        <tr><td><?php print render($content['field_test'][0])?></td><td><?php print render($content['field_test'][1])?></td></tr>
      <?php endif; ?>
<?php if ($content['field_test'][3]): ?>
        <tr><td><?php print render($content['field_test'][2])?></td><td><?php print render($content['field_test'][3])?></td></tr>
      <?php endif; ?>     
<?php if ($content['field_test'][5]): ?>
        <tr><td><?php print render($content['field_test'][4])?></td><td><?php print render($content['field_test'][5])?></td></tr>
      <?php endif; ?>   
</table>

Second fix, only manual work is to say how many repetitions you want.

<dl class="My Class">
        <?php
            $i = 0;
            $counter = 2 * render ($content['field_counter_slides'][0]) -1;
                while ($i <= $counter):
                    if ($content['field_test'][1]){
                        echo "<dt>";
                        print render ($content['field_test'][$i]);
                        $i++;
                        echo "</dt><dd>";
                        print render ($content['field_test'][$i]);
                        echo "</dd>";
                        $i++;
                    }
                endwhile;
        ?>
    </dl>
25r43q
  • 613
  • 4
  • 16

1 Answers1

0

It's quite hard to answer without knowing more but something like this would probably do it:

$header = array();
$rows = array();
foreach ($node->field_my_field[$langcode] as $key => $field_entry) {
  $rows[] = array(
    $field_entry['value'],
    $node->field_my_second_field[$langcode][$key]['value']
  );
}

$output = theme('table', array('header' => $header, 'rows' => $rows));

You'd have to make sure yourself that there are equal numbers of entries for each field so that there will always be a field_my_second_field entry for each run through the foreach loop.

Clive
  • 36,918
  • 8
  • 87
  • 113
  • Yep me again...can't get off this bloomin' site for some reason! – Clive Sep 13 '11 at 00:41
  • Clive, you're back :) Where should I put this code? Does it all go in to the node--type.tpl-file as it is? (I'm not used to the foreach, dowhiles and if-statements in Drupal/PHP yet) Sorry for my poor engrish, not my native tounge. I'll try this tomorrow and will tell the result. Thanks a lot. – 25r43q Sep 13 '11 at 00:45
  • The solution above works (if-statements) but it's not a well coded one (even though the output is correct). Is that more specific? – 25r43q Sep 13 '11 at 12:24