<p>When saving data to a Table field, here's the catch... Your field data is being condensed into <strong>JSON</strong> and stored in a <strong>single database table cell</strong>.</p>

<p>So ultimately, you'll want to have your Table field data arranged in an array, then use <code>json_encode</code> to prep it for entry into the database.</p>

<p>Here's an example of data for a Table field... This is a hypothetical field containing products, with a <em>Product Name</em> and prices for <em>Small</em>, <em>Medium</em>, and <em>Large</em>...</p>

<pre><code>[
  {
    "col1": "Product #1",
    "col2": "5",
    "col3": "10",
    "col4": "15"
  },
  {
    "col1": "Product #2",
    "col2": "7",
    "col3": "14",
    "col4": "21"
  },
  {
    "col1": "Product #3",
    "col2": "7",
    "col3": "14",
    "col4": "21"
  }
]
</code></pre>

<p><strong>An important note...</strong> The column names you established when setting up your Table field are not relevant here. Instead, each columnn will be referred to as "col1", "col2", "col3", etc.</p>

CC BY-SA 3.0
2017-10-09T15:38:24.773Z
Is this still a viable solution? I'm using similare json structure but it doesn't work.
Alessio Breviglieri
CC BY-SA 3.0
2017-10-09T23:15:19.567Z
Yes, this should still work. If you are having a similar issue, I recommend you [ask a new question...](https://craftcms.stackexchange.com/questions/ask)
Lindsey D
CC BY-SA 3.0
2018-03-07T13:23:05.403Z
Just a note in case anyone finds this - you do not want to use `json_encode` - use the actual array to set the value.
Dan Ott
CC BY-SA 3.0
2014-07-23T01:46:51.570Z
2014-07-23T01:46:51.570Z
Lindsey D
<p>I now know that you can enter data into an entry's field by using <code>$event-&gt;params['entry']-&gt;getContent()-&gt;handle</code> </p>

<p>But let's say I have a foreach loop and I want to enter each line into an entry that is a table field. So for example:</p>

<pre><code>foreach ($apigrab['properties'] as $specs) {
    $event-&gt;params['entry']-&gt;getContent()-&gt;specs == ??
}
</code></pre>

<p>Obviously each table heading has it's own handle and I don't imagine specs.handle would work in this case, and also how would you prevent it from just overwriting the same line over and over again? Would I have to count how many first and tell it specifically where to go?</p>

<p>Thanks in advance. </p>

CC BY-SA 3.0
2014-07-18T01:11:12.550Z
To clarify, are you saying that your "specs" field is a **table** field type?
Lindsey D
CC BY-SA 3.0
2014-07-18T05:01:55.533Z
@LindseyD Yep that's what I mean.
Chris
<plugin-development><entries>
Programmatically entering data into a table field for an entry
# Programmatically entering data into a table field for an entry

(Asked by: username_0 on 2014-07-17)

I now know that you can enter data into an entry's field by using $event->params['entry']->getContent()->handle 
But let's say I have a foreach loop and I want to enter each line into an entry that is a table field. So for example:
foreach ($apigrab['properties'] as $specs) {
    $event->params['entry']->getContent()->specs == ??
}

Obviously each table heading has it's own handle and I don't imagine specs.handle would work in this case, and also how would you prevent it from just overwriting the same line over and over again? Would I have to count how many first and tell it specifically where to go?
Thanks in advance.

**Tags:** plugin-development, entries

> **Comments:**
> **username_1**: To clarify, are you saying that your "specs" field is a **table** field type?  
> **username_0**: @LindseyD Yep that's what I mean.  

---

## 1 answers

### Answer by username_1 on 2014-07-23. Score: 4 | Accepted answer

When saving data to a Table field, here's the catch... Your field data is being condensed into JSON and stored in a single database table cell.
So ultimately, you'll want to have your Table field data arranged in an array, then use json_encode to prep it for entry into the database.
Here's an example of data for a Table field... This is a hypothetical field containing products, with a Product Name and prices for Small, Medium, and Large...
[
  {
    "col1": "Product #1",
    "col2": "5",
    "col3": "10",
    "col4": "15"
  },
  {
    "col1": "Product #2",
    "col2": "7",
    "col3": "14",
    "col4": "21"
  },
  {
    "col1": "Product #3",
    "col2": "7",
    "col3": "14",
    "col4": "21"
  }
]

An important note... The column names you established when setting up your Table field are not relevant here. Instead, each columnn will be referred to as "col1", "col2", "col3", etc.

> **Comments:**
> **username_2**: Is this still a viable solution? I'm using similare json structure but it doesn't work.  
> **username_1**: Yes, this should still work. If you are having a similar issue, I recommend you [ask a new question...](https://craftcms.stackexchange.com/questions/ask)  
> **username_3**: Just a note in case anyone finds this - you do not want to use `json_encode` - use the actual array to set the value.  

---

