在drupal中打印字段

发布于 2024-12-04 14:46:00 字数 1935 浏览 1 评论 0原文

我试图让 Drupal 打印一个列表,其中同一数组中有两个不同的字段。所以首先是字段 A,然后是字段 B,以这种方式打印,直到打印整个数组。

我想要得到的结果类似于

<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>

直到打印所有值。

编辑。 想出了一种直接在节点中实现此目的的方法——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>

第二个修复,只有手动工作才是说出你想要多少次重复。

<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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沉鱼一梦 2024-12-11 14:46:00

如果不了解更多信息,很难回答,但类似这样的事情可能会做到这一点:

$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));

您必须确保自己每个字段都有相同数量的条目,以便始终有一个 field_my_second_field每次运行 foreach 循环的条目。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文