将数据从后端获取到一行中的三列中 [英] get data from backend into three columns in one row

查看:127
本文介绍了将数据从后端获取到一行中的三列中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个函数,我试图获取的数据来自两列一行并且有多行生成。数据安排在表格中,我试图用三列而不是两个来创建表格,但是它没有工作。有时在某些行中,它会跳过一列,有时会在一行中输出完美的3列。下面的代码适用于每行中的两列。但如何使数据在一行中出现在三列中?

I am making a function in which the data i am trying to get comes in two columns in one row and there are multiple rows generated . The data is arranged in table and i tried to make the table with three columns instead of two but its not working out . Sometimes in some rows it skips one column and sometimes it output perfect 3 columns in one row . I below code works perfect for two columns in each row . But how to make data appear in three column in one row ?

$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列。

Here is the output screenshot . Its coming in 2 columns in one row . I want 3 columns in one row.

推荐答案

在这里你去:)模运算符在这里帮助。

Here you go :) Modulus operator helps here.

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

这篇关于将数据从后端获取到一行中的三列中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆