我正在做一个函数,在这个函数中,我试图获取的数据以one row
的two columns
格式出现,并且生成了多个行。数据是以表格的形式排列的,我试着用three columns
代替two
来制作表格,但是做不出来。有时在某些行中它跳过一列,有时在一行中输出完美的3列。我下面的代码非常适合每一行中的two columns
,但是如何让数据出现在three column
中的一行中呢?
$r = 1;
$html = "";
$html .= '<table class="table table-bordered">
<tbody><tr>';
foreach($products as $pr) {
if($r != 1) {$rw = (bool)($r & 1);
$html .= $rw ? '</tr><tr>' : ''; }
$html .= '<td><strong>'.$pr->name.'</strong><br>'.'<strong>Rs '.$pr->price.'</strong><br>'.$this->product_barcode($pr->code, 30, 147).'</td>';
$r++;
}
$html .= '</tr></tbody>
</table>';
$data['html'] = $html;
这是输出屏幕截图。它在一行中有两列。我想要3列在一行。
发布于 2014-01-11 16:19:10
在这里:)模运算符在这里提供帮助。
http://en.wikipedia.org/wiki/Modulo_operation
$html = '<table class="table table-bordered"><table>';
$r = 0;
$columns = 3;
foreach ($products as $pr) {
if ($r%$columns == 0) $html .= '<tr>';
$html .= '<td>';
$html .= '<strong>'.$pr->name.'</strong><br />';
$html .= $pr->price.'<br />';
$html .= $this->product_barcode($pr->code, 30, 147);
$html .= '</td>';
if ($r%$columns == $columns || $r++ == count($products)-1) $html .= '</tr>';
}
$html .= '</tbody></table>';
https://stackoverflow.com/questions/21060063
复制相似问题