<p>The thing to keep in mind here is that a table field contains a simple <strong>JSON string</strong>. So you can (and should) use PHP's native <a href="http://php.net/manual/en/function.json-encode.php"><code>json_encode</code></a> function to prepare your data...</p>

<pre class="lang-php prettyprint-override"><code>// Your data will be an array of arrays:
$tableData = array(
    array(
        "col1" =&gt; "data",
        "col2" =&gt; "data",
    )
);

// Then simply encode it when you set your field value:
$entry-&gt;getContent()-&gt;setAttributes(array(
    'tableFieldHandle' =&gt; json_encode($tableData)
));
</code></pre>

<p>This will also solve any string escaping concerns for you!</p>

CC BY-SA 4.0
2020-03-10T23:52:10.310Z
Just incase anyone else stumbles on to this - You actually have to use "col1" etc and not the handle of your column...
mylesthe.dev
CC BY-SA 3.0
2015-02-01T04:08:11.783Z
That makes total sense. Thank you Lindsey!
Douglas McDonald
CC BY-SA 3.0
2015-02-01T02:31:58.130Z
2015-02-01T02:44:48.213Z
2015-02-01T02:44:48.213Z
Lindsey D
<p>I'm trying to import data from another database into a 'table' fieldtype via a plugin, and have a couple questions.</p>

<p>When setting the content on the EntryModel, are there any methods to assist in setting the rows and/or columns, or do I need to just format the data as a string in the expected format?</p>

<pre><code>    $entry-&gt;getContent()-&gt;setAttributes(array(
        'tableFieldHandle' =&gt; '[{"col1":"data","col2":"data"}]'
    ));
</code></pre>

<p>Secondly, what's the recommended method to prepare strings for a table field (i.e. so as to escape or encode double and single quotes)?</p>

<plugin-development><import><table-field>
Inserting data into a table fieldtype via plugin?
# Inserting data into a table fieldtype via plugin?

(Asked by: username_0 on 2015-02-01)

I'm trying to import data from another database into a 'table' fieldtype via a plugin, and have a couple questions.
When setting the content on the EntryModel, are there any methods to assist in setting the rows and/or columns, or do I need to just format the data as a string in the expected format?
    $entry->getContent()->setAttributes(array(
        'tableFieldHandle' => '[{"col1":"data","col2":"data"}]'
    ));

Secondly, what's the recommended method to prepare strings for a table field (i.e. so as to escape or encode double and single quotes)?

**Tags:** plugin-development, import, table-field

---

## 1 answers

### Answer by username_1 on 2015-02-01. Score: 7 | Accepted answer

The thing to keep in mind here is that a table field contains a simple JSON string. So you can (and should) use PHP's native json_encode function to prepare your data...
// Your data will be an array of arrays:
$tableData = array(
    array(
        "col1" => "data",
        "col2" => "data",
    )
);

// Then simply encode it when you set your field value:
$entry->getContent()->setAttributes(array(
    'tableFieldHandle' => json_encode($tableData)
));

This will also solve any string escaping concerns for you!

> **Comments:**
> **username_0**: That makes total sense. Thank you Lindsey!  
> **username_2**: Just incase anyone else stumbles on to this - You actually have to use "col1" etc and not the handle of your column...  

---

