Forums

Splitting comma separated values field into columns

David-E 17 Mar, 2015
Is it possible to split a field containing comma separated values into separate columns?

I have a column containing fields with variations of the following:
{"land-size":"2000","pricemin":"500","pricemax":"1000"}

I would like my table to show as follows:

<table>
  <thead>
    <tr>
      <th>
        land-size
      </th>
      <th>
        pricemin
      </th>
      <th>
        pricemax
      </th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>
        2000
      </td>
      <td>
        500
      </td>
      <td>
        1000
      </td>
    </tr>
    <tr>
      <td>
          1500
      </td>
      <td>
          1000
      </td>
      <td>
          2500
      </td>
    </tr>
  </tbody>
</table>


I would also like to include other normal fields from my Columns list within the table above.
GreyHead 17 Mar, 2015
Answer
Hi David_E,

I looks as though that is json_encoded data. There are a couple of ways that you might be able to handle it.

+ In the List Settings there is a PHP Functions box, I think that you could add three entries there - one for each field. The functions would have to json_decode the data and then pick the appropriate item from the result.

+ If you used a Custom Listing then you could use similar PHP in the Body box to create the whole <tr>. That might well be the simpler solution if you only want those three columns.

Bob
David-E 18 Mar, 2015
Hi Bob,

Yes you are correct it is json_encoded data.

Here is how I managed to get it working, in case anyone else has the same requirement:

Header Code:
<table class="table table-hover table-censored" id="gcore_table_list1">
<thead>
<tr>
 <th>
   Name
</th>
 <th>
   Price Min
</th>
 <th>
   Price Max
</th>
 <th>
   Land Size
</th>
</tr>
</thead>
<tbody>


Body Code:
<div style="display:none;">
<?php
$string =  $row['Modelname']['Fieldwithdata'];
$result = json_decode($string);
var_dump($result); ?>
</div>

<tr>
<td>
{Modelname.Namefield}
</td>
<td>
<?php echo $result->pricemin; ?>
</td>
<td>
<?php echo $result->pricemax; ?>
</td>
<td>
<?php echo $result->landsize; ?>
</td>
</tr>


Footer Code:
</tbody>
</table>


Notes:
I put the json_decode part within a div and set the style to display:none; as I was getting the decode information showing above the table.

I changed the value for land-size to landsize without the - as json_decode seemed to not work on that value.

All working perfectly now.

Thank you.
GreyHead 18 Mar, 2015
1 Likes
Hi David-E,

You can comment out the Var dump line - I imagine it's only there for debugging.

Bob
This topic is locked and no more replies can be posted.