使用PHP循环将Bootstrap行和正确的列号添加到元素

时间:2015-09-24 16:25:30

标签: php html wordpress loops twitter-bootstrap-3

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

enter image description here

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)中,我将.row div中的每3个项目包装起来:

<?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个网格(使它们居中)?

6 个答案:

答案 0 :(得分:8)

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

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项,输出:
  2. &#13;
    &#13;
    <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>
    &#13;
    &#13;
    &#13;

    1. 11项,输出:
    2. &#13;
      &#13;
      <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>
      &#13;
      &#13;
      &#13;

      1. 单项,输出:
      2. &#13;
        &#13;
        <div class="row">
        <div class="col-md-12">Content</div>
        </div>
        &#13;
        &#13;
        &#13;

        注意:您可以删除PHP_EOL,我用它来更好地阅读来源。

答案 1 :(得分:6)

每次我需要这样做时,我只需使用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>
}

答案 2 :(得分:2)

我想通过首先找到最后一行开始的项目并将相应的列号应用于该行中的所有项目来找到解决方案:

<?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,它将应用正确的列。

答案 3 :(得分:1)

为什么不评估你的模数?

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

答案 4 :(得分:1)

一次打印一行,确定每个元素的html类,具体取决于行的完整程度;对于0 col-md-4,对于1 col-md-12 ...你需要一些辅助结构。如果缓冲区中存在某些内容,最后打印最后一行。

/**
 * 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);
}

答案 5 :(得分:0)

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