# Combine table fields in templates

**URL:** <https://discourse.cakephp.org/t/combine-table-fields-in-templates/12223>\
**Category:** Need Help\
**Created:** [September 3, 2024, 1:57pm UTC](https://discourse.cakephp.org/t/combine-table-fields-in-templates/12223 "2024-09-03T13:57:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Howan](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/howan/32/3182_2.png) [@Howan](https://discourse.cakephp.org/u/Howan)\
**Post date:** [September 3, 2024, 1:57pm UTC](https://discourse.cakephp.org/t/combine-table-fields-in-templates/12223/1 "2024-09-03T13:57:18Z")

</div>

Hey everyone,

i have a table where a Adress is splittet in different fields.  
street, house\_no, zip, city

If i now want to show them together on a page do i always do that?

```auto
$user->house->street.' '.$user->house->house_no.' '.$user->house->postcode.' '.$user->house->city

```

and if yes how do i produce a line break inside there.

`."\n".`  
or  
`.'\n'.`  
didn’t work for me.

Thank you !!

---

<div class="post-metadata">

**Author:** ![KevinPfeifer](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/kevinpfeifer/32/3365_2.png) [@KevinPfeifer](https://discourse.cakephp.org/u/KevinPfeifer)\
**Post date:** [September 3, 2024, 2:14pm UTC](https://discourse.cakephp.org/t/combine-table-fields-in-templates/12223/2 "2024-09-03T14:14:38Z")

</div>

See [Virtual Fields](https://book.cakephp.org/5/en/orm/entities.html#creating-virtual-fields)

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [September 3, 2024, 4:56pm UTC](https://discourse.cakephp.org/t/combine-table-fields-in-templates/12223/3 "2024-09-03T16:56:16Z")

</div>

`"\n"` will produce a line break in the generated HTML, but that doesn’t translate into a line break in what gets rendered. For that, you’ll need `<br/>`.

---

<div class="post-metadata">

**Author:** ![Howan](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/howan/32/3182_2.png) [@Howan](https://discourse.cakephp.org/u/Howan)\
**Post date:** [September 3, 2024, 5:12pm UTC](https://discourse.cakephp.org/t/combine-table-fields-in-templates/12223/4 "2024-09-03T17:12:51Z")

</div>

Hey Kevin, thank you so much.  
That worked so far and i understood the Virtual Fields. 🤩

Im still not sure how to proceed.

```auto
protected function _getFullAdress()
    {
        return $this->street . ' ' . $this->house_no . ' ' . $this->postcode . ' ' . $this->city;
    }

```

When i want to just show the Virtual Field i can simply

```auto
$user->house->full_adress

```

But when i want to show it in the EDIT or NEW i was struggeling what to do.

```auto
  echo $this->Form->control('house_id', [
                        'options' => $houses,

```

The only way i found was to set the DisplayField to my created Virtual Field

```auto
$this->setDisplayField('full_adress');

```

Is that the way you would do that?

---

<div class="post-metadata">

**Author:** ![Howan](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/howan/32/3182_2.png) [@Howan](https://discourse.cakephp.org/u/Howan)\
**Post date:** [September 3, 2024, 5:16pm UTC](https://discourse.cakephp.org/t/combine-table-fields-in-templates/12223/5 "2024-09-03T17:16:07Z")

</div>

Hi Zuluru,

```auto
protected function _getFullAdress() {
   return $this->street . ' ' . $this->house_no . "<br/>" . $this->postcode . ' ' . $this->city; 
}

```

doesn’t work here

 ![Bildschirmfoto 2024-09-03 um 19.14.58](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/5/5fe2a93a7d6239d18edf4b0a98f368ae5e68859a.png)  
 ![Bildschirmfoto 2024-09-03 um 19.15.08](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/0/02d72f8f9c36fff297365df1a1a068af3b7028d0.png)

---

<div class="post-metadata">

**Author:** ![KevinPfeifer](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/kevinpfeifer/32/3365_2.png) [@KevinPfeifer](https://discourse.cakephp.org/u/KevinPfeifer)\
**Post date:** [September 3, 2024, 5:57pm UTC](https://discourse.cakephp.org/t/combine-table-fields-in-templates/12223/6 "2024-09-03T17:57:36Z")

</div>

1. HTML `select` elements (or more directly `option` element) can’t contain further HTML. Its a browser standard as e.g. mobile devices show a completely different view for selecting an option as your desktop browser does. If you want to style a select, you need to use a JS library like select2 or slim-select to transform the HTML native `select` into “other” HTML which represents a select and can be styled.

2. Those links don’t “properly” use the `<br/>` because CakePHP automatically escapes Title’s for generated links to prevent XSS. If you don’t know what that is you have much to learn 😉  
If you don’t care about XSS or truley trust your users (which you shouldn’t) you can prevent that logic via e.g.

```auto
echo $this->Html->link(
    "Some Text with a <br/> break", 
    ['controller' => 'Pages', 'action' => 'display', 'home'], 
    ['escapeTitle' => false]
);

```
