简体   繁体   中英

Concatenate mysql column values in php

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.):

<?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'.

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

The answer to your question in SQL is:

select name, group_concat(distinct bank_name)
from t
group by name;

I don't understand how that fits into the rest of your application. I can say that you should be doing this processing in the database rather than using loops on the application side.

您可以在SQL中执行此操作:

SELECT name, GROUP_CONCAT(DISTINCT bank_name) FROM table GROUP BY client_id;

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

<?
    $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()

<?
    $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

<?

/**
 * 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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM