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