首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >从后台获取数据到一行中的三列

从后台获取数据到一行中的三列
EN

Stack Overflow用户
提问于 2014-01-11 16:06:52
回答 1查看 207关注 0票数 0

我正在做一个函数,在这个函数中,我试图获取的数据以one rowtwo columns格式出现,并且生成了多个行。数据是以表格的形式排列的,我试着用three columns代替two来制作表格,但是做不出来。有时在某些行中它跳过一列,有时在一行中输出完美的3列。我下面的代码非常适合每一行中的two columns,但是如何让数据出现在three column中的一行中呢?

代码语言:javascript
复制
$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列在一行。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-01-11 16:19:10

在这里:)模运算符在这里提供帮助。

http://en.wikipedia.org/wiki/Modulo_operation

代码语言:javascript
复制
$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>';
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21060063

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档