html 使用PHP循环向元素添加Bootstrap行和适当的列编号

rggaifut  于 2023-03-27  发布在  PHP
关注(0)|答案(7)|浏览(135)

我尝试使用PHP循环和Twitter Bootstrap的12列网格系统创建以下前端:

HTML输出为:

<div class="row">
    <div class="col-lg-4">
        Content...
    </div>
    <div class="col-lg-4">
        Content...
    </div>
    <div class="col-lg-4">
        Content...
    </div>
</div>

<div class="row">
    <div class="col-lg-4">
        Content...
    </div>
    <div class="col-lg-4">
        Content...
    </div>
    <div class="col-lg-4">
        Content...
    </div>
</div>

<div class="row">
    <div class="col-lg-6">
        Content...
    </div>
    <div class="col-lg-6">
        Content...
    </div>
</div>

在PHP(WordPress)中,我将每3个项目 Package 在.row div中:

<?php $i=0; // counter ?>

<?php while ( have_posts() ) : the_post(); ?> 

    <?php if ($i%3==0) { // if counter is multiple of 3 ?>
    <div class="row">
    <?php } ?>

    <div class="col-md-4">
        Content...
    </div>        

    <?php $i++; ?>

    <?php if($i%3==0) { // if counter is multiple of 3 ?>
    </div>
    <?php } ?>

<?php endwhile; ?>

<?php if($i%3!=0) { // put closing div if loop is not exactly a multiple of 3 ?>
</div>
<?php } ?>

问题:

我不知道如何为最后一行的项目添加适当的列号,以便它们填充12列网格。
例如,在我上面的插图中,最后一行的每个项目都有col-6(扩展6列)填充12网格系统。作为另一个例子,如果最后一行有1个项目,它应该有col-12

**注意:**每行最多3项,如图和PHP所示。
我知道以下内容:

  • 项目总数$loop->post_count
  • 产品编号$i
  • 最后一行剩余项目数$loop->post_count%3(我认为)
  • 列总数12(12可以除以剩余项的数量,以计算出要给予它们的列号)
    问题:

我如何在上面的PHP中使用这些数据来更改最后一行中项目的列号,以便它们填充12网格(使它们居中)?

mi7gmzs6

mi7gmzs61#

我喜欢你的问题,因为我正在处理一个非常相似的情况。由于其他答案有点长,我决定把我的留在这里供你考虑。对我来说,你使用的变量越少,解决方案就越好。

BootstrapContentArranger.php

<?php
function BootstrapContentArrange($i) {
    $items = $i;                // qnt of items
    $rows = ceil($items/3);     // rows to fill
    $lr = $items%3;             // last row items
    $lrc = $lr;                 // counter to last row

    while ($items > 0) {        // while still have items
        $cell = 0;
        if ($rows > 1) {        // if not last row...
            echo '<div class="row">'.PHP_EOL;
            while ($cell < 3) {     // iterate with 3x4 cols
                echo '<div class="col-md-4">Content</div>'.PHP_EOL;
                $cell++;
            }
            echo "</div>".PHP_EOL;
        $rows--;        // end a row
        } elseif ($rows == 1 && $lr > 0) {      // if last row and still has items
            echo '<div class="row">'.PHP_EOL;
            while ($lrc > 0) {      // iterate over qnt of remaining items
                $lr == 2 ?      // is it two?
                    print('<div class="col-md-6">Content</div>'.PHP_EOL) :  // makes 2x6 row
                    print('<div class="col-md-12">Content</div>'.PHP_EOL); // makes 1x12 row
                $lrc--;
            } 
            echo "</div>".PHP_EOL;
            break;
        } else {        // if round qnt of items (exact multiple of 3)
            echo '<div class="row">'.PHP_EOL;
            while ($cell < 3) {     // iterate as usual
                echo '<div class="col-md-4">Content</div>'.PHP_EOL;
                $cell++;
            }
            echo "</div>".PHP_EOL;
            break;
        }
        $items--;       // decrement items until it's over or it breaks
    }
}

测试用例

BootstrapContentArrange(3);
BootstrapContentArrange(11);
BootstrapContentArrange(1);

1.3项,输出:

<div class="row">
<div class="col-md-4">Content</div>
<div class="col-md-4">Content</div>
<div class="col-md-4">Content</div>
</div>

1.11项,输出:

<div class="row">
<div class="col-md-4">Content</div>
<div class="col-md-4">Content</div>
<div class="col-md-4">Content</div>
</div>
<div class="row">
<div class="col-md-4">Content</div>
<div class="col-md-4">Content</div>
<div class="col-md-4">Content</div>
</div>
<div class="row">
<div class="col-md-4">Content</div>
<div class="col-md-4">Content</div>
<div class="col-md-4">Content</div>
</div>
<div class="row">
<div class="col-md-6">Content</div>
<div class="col-md-6">Content</div>
</div>

1.单项,输出:

<div class="row">
<div class="col-md-12">Content</div>
</div>

注意:你可以去掉PHP_EOL,我用它来更好的阅读源码。

x33g5p2x

x33g5p2x2#

每次我需要这样做时,我只是使用array_chunk为我的行和列构建一个合适的数组块。
例如,您有:
$posts = [['id' => 1], ['id' => 2] ...]
与其循环计算是否添加行,不如将你的帖子分成块:

$posts = [['id' => 1], ['id' => 2] ...]

$postChunks = array_chunk($posts, 4); // 4 is used to have 4 items in a row
foreach ($postChunks as $posts) {
    <div class="row">
        foreach ($posts as $post) {
            <div class="col-md-3">
                <?=$post['id'];?>
            </div>     
        }
    </div>
}
whitzsjs

whitzsjs3#

我想我找到了解决方案,首先找到了最后一行开始的项目,并将适当的列号应用于该行中的所有项目:

<?php
$max_columns = 3; //columns will arrange to any number (as long as it is evenly divisible by 12)
$column = 12/$max_columns; //column number
$total_items = $loop->post_count;
$remainder = $loop->post_count%$max_columns; //how many items are in the last row
$first_row_item = ($total_items - $remainder); //first item in the last row
?>

<?php $i=0; // counter ?>

<?php while ( have_posts() ) : the_post(); ?> 

    <?php if ($i%$max_columns==0) { // if counter is multiple of 3 ?>
    <div class="row">
    <?php } ?>

    <?php if ($i >= $first_row_item) { //if in last row ?>   
    <div class="col-md-<?php echo 12/$remainder; ?>">
    <?php } else { ?>
    <div class="col-md-<?php echo $column; ?>">
    <?php } ?>
        Content...
    </div>        

    <?php $i++; ?>

    <?php if($i%$max_columns==0) { // if counter is multiple of 3 ?>
    </div>
    <?php } ?>

<?php endwhile; ?>

<?php if($i%$max_columns!=0) { // put closing div if loop is not exactly a multiple of 3 ?>
</div>
<?php } ?>

优点是任何数字(可被12整除)都可以添加到$max_columns,并且它将应用适当的列。

n3h0vuf2

n3h0vuf24#

你为什么不计算你的模?

$two = false; 
if($i%3 == 2)
{
      <div class="col-md-6">
         Content...
      </div>
      $two = true;  
}

if($i%3 == 1)
{
      if($two)
      {
          <div class="col-md-6">
              Content...
          </div>
      }
      else
      {
          <div class="col-md-12">
              Content...
          </div>              
      }          
}
uinbv5nw

uinbv5nw5#

一次打印一行,根据行的填充程度确定每个元素的HTML类;for 0 col-md-4,for 1 col-md-12...你需要一些helper结构。最后打印最后一行,如果缓冲区中有东西。

/**
 * Prints the row in a grid
 * @param array $posts
 * @param string $class
 */
function printRow($posts, $class) {
    echo '<div class="row">';

    foreach ($posts as $post) {
        echo '<div class="' . $class . '">' . $post . '</div>';
    }

    echo '</div>';
}

$i = 0;
$htmlClasses = ['col-md-4', 'col-md-12', 'col-md-6']; //helper for setting html classes
$buffer = []; //helper array to hold row elements

while (have_posts()) {
    the_post();
    $i++;

    $mod = $i % 3;

    //determine html class
    $htmlClass = $htmlClasses[$mod];

    if ($mod > 0) {
        $buffer[] = $currentPost; //this is the post content
    } else {
        printRow($buffer, $htmlClass);
        $buffer = [];
    }
}

//printing final row if there are elements
if (!empty($buffer)) {
    printRow($buffer, $htmlClass);
}
jfgube3f

jfgube3f6#

<?php
            //total products or items you have
            $total_pr = count($products);

            //grid of columns you want 
            $grid = 3;
            $tol_raw = ceil($total_pr / $grid);
            $count =0;
        ?>

        <?php for($i=0;$i<$tol_raw;$i++): ?>
            <?php

            $repeat = $grid;
            if($total_pr<$grid)$repeat = $total_pr;
            $total_pr -= $repeat;

            ?>
            <div class="row">
                <?php for($pr=0;$pr<$repeat;$pr++):?>
                    <?php $product = $products[$count]; ?>
                     <!-- column selection is based onn your grid -->
                    <div class="col-md-4">
                         //do whatever you want to do here
                    </div>
                    <?php $count++; ?>
                <?php endfor; ?>
            </div>
        <?php endfor; ?>
sg3maiej

sg3maiej7#

对于那些不希望元素跨越所有列而只是一个简单网格的人来说,下面是如何在中实现的。

<?php
        $users = $db->run('SELECT * FROM users');
        $counter = 0;

        foreach ($users as $row) {
          if ($counter % 3 == 0 && $printed) {
            echo '</div>';
          }
          if ($counter % 3 == 0) {
            $printed = true;
            echo '<div class="row pb-4">';
          }
        ?>
        <!-- print your data start -->
          <div class="col-md-4">
            <div class="shadow-sm p-3  rounded m-1">
              <h5>Hello Header</h5>
              <div>Hello Body</div>
            </div>
          </div>
        <!-- print your data end -->
        <?php
          $counter += 1;
        }
        ?>

结果

相关问题