在 drupal 视图中创建的行太多

发布于 2024-12-13 02:09:51 字数 400 浏览 0 评论 0原文

我已经修改了用于网格视图的 tpl 以显示 3 列内容,我唯一的问题是下面的代码为视图创建了不需要的 div。我最多有 9 个项目,应以 3 行、每列 3 个输出。修改下面的代码的最佳方法是什么?以防止输出额外的 div。

<?php foreach ($rows as $row_number => $columns): ?>
  <div>
    <?php foreach ($columns as $column_number => $item): ?>

        <?php print $item; ?>

    <?php endforeach; ?>
  </div>
<?php endforeach; ?>

I have modified the tpl used for the grid view to show 3 columns of content, my only issue that the code below creates unneeded divs for the view. I have a maximum of 9 items that should be output in 3 rows, 3 per column. What's the best way to modify the code below? to prevent the extra divs from being output.

<?php foreach ($rows as $row_number => $columns): ?>
  <div>
    <?php foreach ($columns as $column_number => $item): ?>

        <?php print $item; ?>

    <?php endforeach; ?>
  </div>
<?php endforeach; ?>

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

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

发布评论

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

评论(1

多彩岁月 2024-12-20 02:09:51

我会放弃 foreach 语法的表示法(但我认为这是非常个人化的:-))

您可以使用模数来检查您是否只有 3 列(如果我理解您的问题)。

<?php

// I´m assuming that $column_number is a zero based index
// if thats not the case you should add a counter to keep track of column numbers or if it is in sequence but isn't zero based you could easily update the calculates based in your starting index

foreach ($rows as $row_number => $columns) {
    foreach ($columns as $column_number => $item) {
        if ($column_number == 0 || $column_number%3 == 0) {
            print '<div>';
        }
        print $item;
        if ($column_number == 2 || $column_number%3 == 2) {
            print '</div>';
        }
    }
    // prevent open div tags
    $total_columns = count($columns);
    if ($total_columns > 0 && ($total_columns < 3 || $total_columns%3 != 0)) {
        print('<div>');
    }
}

为了便于阅读,我还删除了 php 的所有开始和结束标记。

I would drop that notation of foreach syntax (but I think that's pretty personal :-) )

You could use the modulo to check if you just had 3 columns (if I understood your question).

<?php

// I´m assuming that $column_number is a zero based index
// if thats not the case you should add a counter to keep track of column numbers or if it is in sequence but isn't zero based you could easily update the calculates based in your starting index

foreach ($rows as $row_number => $columns) {
    foreach ($columns as $column_number => $item) {
        if ($column_number == 0 || $column_number%3 == 0) {
            print '<div>';
        }
        print $item;
        if ($column_number == 2 || $column_number%3 == 2) {
            print '</div>';
        }
    }
    // prevent open div tags
    $total_columns = count($columns);
    if ($total_columns > 0 && ($total_columns < 3 || $total_columns%3 != 0)) {
        print('<div>');
    }
}

I've also dropped all those opening and closing tags of php for readability.

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