I have a table like:

我有一张桌子:

client_id   |   name   |   bank_name
------------------------------------
    1       |   Steve  |    Bank 1 
    1       |   Steve  |    Bank 1
    1       |   Steve  |    Bank 3

I want to print out a statement as following:

我想打印一份声明如下:

Client: Steve

Current bank: Bank 1, Bank 3

Here is my current code. (Keep in mind I am printing this in a template pdf so ignore the php and div tags, they are correct as is.):

这是我当前的代码。(请记住,我是用模板pdf打印的,所以忽略php和div标签,它们是正确的。)

<?php
    $currentBank = '';
    foreach($data as $bank){
        $currentBank .= $bank->bank_name;

?>
            <div>Current bank: {{ $currentBank }} </div>
<?php
    }
?>

Can anyone help me achieve the wanted formatting, ignoring the duplicate items and printing out in the format : 'Bank 1, Bank2'.

谁能帮我实现想要的格式,忽略重复的项目和打印格式:“bank1, Bank2”。

Currently with my code, my output is:

目前我的代码输出是:

Current bank: Bank1
Current bank: Bank1Bank1
Current bank: Bank1Bank1Bank3

Note: I cannot use DISTINCT in my sql query as another section of the template requires me to print out everything including duplicates

注意:我不能在sql查询中使用DISTINCT,因为模板的另一部分要求我打印所有东西,包括副本

3 个解决方案

#1


1

You need to spend a entire for loop looping through all available data. Collect the names into a new array because this allows us an easy way to get uniques with array_unique. Another option would be to store the bankNames as the key in the array - see second example

您需要花费整个for循环循环遍历所有可用数据。将这些名称收集到一个新的数组中,因为这样可以方便地使用array_unique获得uniques。另一种选择是将bankNames存储为数组中的键——参见第二个示例

<?
    $currentBank = '';
    $bankNameList = [];
    foreach($data as $bank):
        $bankNameList[] = $bank->bank_name;
    }
    $bankNameList = array_unique($bankNameList);
    $currentBank = implode(', ', $bankNameList);

?>
            <div>Current bank: {{ $currentBank }} </div>
<? endfor; ?>

second example without array_unique()

第二个例子没有array_unique()

<?
    $currentBank = '';
    $bankNameList = [];
    foreach($data as $bank):
        $bankNameList[$bank->bank_name]++;
    }
    $currentBank = implode(', ', array_keys($bankNameList));

?>
            <div>Current bank: {{ $currentBank }} </div>
<? endfor; ?>

The even bestest way would be to organize this data outside of the template with named helper functions. That way, your template doesn't grow to thousands of undocumented lines

最好的方法是使用命名的helper函数在模板之外组织这些数据。这样,您的模板就不会增长到数千条没有文档的行

<?

/**
 * Return all $item->bank_names
 * as comma delimited string
 */
public function concatAllBankNames($data) {

        $bankNameList = [];
        foreach($data as $bank):
            $bankNameList[$bank->bank_name]++;
        }
        return implode(', ', array_keys($bankNameList));
}

//.. in some controller
foreach ($databasedata as $row) {
  $row[ $row['username'] ]['bank_names_concat'] = $this->concatAllBankNames($row['data']);

}
//assign data to template system somehow
$template->rows = $rows;
//or
$template['rows'] = $rows;

更多相关文章

  1. 要在SQL数据库中根据身份证号码查询出性别(有15位的,也有18位的),怎
  2. 一条牛B的SQL抵了我300多行的程序代码
  3. Android中RecyclerView的item中控件的点击事件添加删除一行、上
  4. Android Studio中如何编写JNI代码及编译so库
  5. 编译Android4.3内核源代码
  6. Java使用Velocity模板发送HTML格式邮件并解决中文乱码问题
  7. Java se之静态代码块、代码块、构造函数执行顺序问题
  8. Java普通代码块,构造代码块,静态代码块区别,执行顺序的代码实例
  9. 编写自己的代码库(javascript常用实例的实现与封装)[转]

随机推荐

  1. 使用nagios+python监控nginx进程数
  2. Python脚本如何获取当前环节和用户等信息
  3. 脚本结束后如何运行进程并退出?
  4. Python黑帽子——通过Paramiko使用SSH
  5. 如何在C中创建Python类?
  6. Python 英文词频统计
  7. 麻烦处理django中的泛型关系
  8. 腾讯应用宝采集数据分析
  9. Python根据第一项从2d数组中删除元素
  10. Django 1.5多用户身份验证或