如何对foreach循环的输出进行分组?

时间:2016-08-07 00:07:43

标签: php html twitter-bootstrap foreach

我正在使用Bootstrap的网格,我正在迭代一系列名称。问题是我不知道如何为每个父div设置X量的结果。

$ names =包含9个名字的mysql结果。

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
 <ipython-input-20-63f086b8a252> in Equ1(nQ, t)
  9 def Equ1(nQ,t):
 10     x1,y1,z1,lamb1 = nQ
 ---> 11     dx = lambdify((x,y,z,lam[0]), field[x.diff(t)],'numpy' )(x1,y1,z1,lamb1)
 12     dy = lambdify((x,y,z,lam[0]), field[y.diff(t)],'numpy' )(x1,y1,z1,lamb1)
 13     dz = lambdify((x,y,z,lam[0]), field[z.diff(t)],'numpy' )(x1,y1,z1,lamb1)

/usr/local/lib/python2.7/dist-packages/sympy/core/expr.pyc in diff(self, *symbols, **assumptions)
2864         new_symbols = list(map(sympify, symbols))  # e.g. x, 2, y, z---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-20-63f086b8a252> in Equ1(nQ, t)
  9 def Equ1(nQ,t):
 10     x1,y1,z1,lamb1 = nQ
 ---> 11     dx = lambdify((x,y,z,lam[0]), field[x.diff(t)],'numpy' )(x1,y1,z1,lamb1)
 12     dy = lambdify((x,y,z,lam[0]), field[y.diff(t)],'numpy' )(x1,y1,z1,lamb1)
 13     dz = lambdify((x,y,z,lam[0]), field[z.diff(t)],'numpy' )(x1,y1,z1,lamb1)

  /usr/local/lib/python2.7/dist-packages/sympy/core/expr.pyc in diff(self, *symbols, **assumptions)
  2864         new_symbols = list(map(sympify, symbols))  # e.g. x, 2, y, z
2865         assumptions.setdefault("evaluate", True)
---> 2866         return Derivative(self, *new_symbols, **assumptions)
2867 
2868     ###########################################################################

/usr/local/lib/python2.7/dist-packages/sympy/core/function.pyc in __new__(cls, expr, *variables, **assumptions)
1068                 ordinal = 'st' if last_digit == 1 else 'nd' if last_digit == 2 else 'rd' if last_digit == 3 else 'th'
1069                 raise ValueError(filldedent('''
---> 1070                 Can\'t calculate %s%s derivative wrt %s.''' % (count, ordinal, v)))
1071 
1072             if all_zero and not count == 0:

ValueError: 
Can't calculate 1st derivative wrt 0.0.

2865         assumptions.setdefault("evaluate", True)
---> 2866         return Derivative(self, *new_symbols, **assumptions)
2867 
2868     ###########################################################################

/usr/local/lib/python2.7/dist-packages/sympy/core/function.pyc in __new__(cls, expr, *variables, **assumptions)
1068                 ordinal = 'st' if last_digit == 1 else 'nd' if last_digit == 2 else 'rd' if last_digit == 3 else 'th'
1069                 raise ValueError(filldedent('''
---> 1070                 Can\'t calculate %s%s derivative wrt %s.''' % (count, ordinal, v)))
1071 
1072             if all_zero and not count == 0:

ValueError: 
Can't calculate 1st derivative wrt 0.0.

这将输出9次。

<div class="container">
  <div class="row">
    <?php
    foreach ($names as $name){
     echo '
      <div class="col-xs-3 col-sm-3">
        <p>Customer name:'.$name.'</p>
      </div>';  
     }  
   </div>
</div>

如何使每个<div class="col-xs-3 col-sm-3"> <p> Customer name: Foo </p> </div> <div class="col-xs-3 col-sm-3"> <p> Customer name: bob </p> </div> <div class="col-xs-3 col-sm-3"> <p> Customer name: jim </p> </div> <div class="col-xs-3 col-sm-3"> <p> Customer name: dave </p> </div> <div class="col-xs-3 col-sm-3"> <p> Customer name: lucy </p> </div> <div class="col-xs-3 col-sm-3"> <p> Customer name: sarah </p> </div> <div class="col-xs-3 col-sm-3"> <p> Customer name: geoff </p> </div> <div class="col-xs-3 col-sm-3"> <p> Customer name: matt </p> </div> <div class="col-xs-3 col-sm-3"> <p> Customer name: alex </p> </div> 输出3个列div?

.row

3 个答案:

答案 0 :(得分:3)

你可能想要考虑array_chunk()来处理不同数量的名字:

$names  =   array('Dave','Frank','Sarah','Dan','Andrew','Jessica','Alena','Debbie','Jeff');
$split  =   array_chunk($names,3);
?>
<div class="container">
<?php
    foreach ($split as $group){
        echo '
    <div class="row">';
        foreach ($group as $name){
            echo '
        <div class="col-xs-3 col-sm-3">
            <p>Customer name:'.$name.'</p>
        </div>';
        }
        echo '
    </div>';
    } 
?>
</div>

答案 1 :(得分:1)

在循环中使用显式变量,并使用基于变量值的条件。例如:

$i = 0;

foreach ($names as $name)
{
    if($i == 0)
    {
        echo '<div class="row">';
    }

    echo
    '<div class="col-xs-3 col-sm-3">
        <p>Customer name:'.$name.'</p>
    </div>';
    $i++;

    if($i == 2)
    {
        echo '</div>';
        $i = 0;
    }
}

答案 2 :(得分:1)

您可以尝试删除<div class="row">并让它们堆叠。如果您确保设置所有选项,请说出<div class="col-xs-3 col-sm-3 col-md-3 col-lg-3">,那么它们应该相应地堆叠。除非有单独的需要将它们分成精确的行。这也是对头部的快速思考..未经测试,也许不是需要的。

此外,还有这个位

if (($counter + 1) % 3 == 0) {
  //do code here
}

if ($counter % 3 == 0) {
      //do code 
    }
$counter++;