You are here:  » Explode Filter - result into a seperate designated field

Support Forum



Explode Filter - result into a seperate designated field

Submitted by ChrisNBC on Thu, 2014-11-06 14:17 in

Hi David,

Hope all is going well.

I wondered if you could tell me if there is a way to use the explode filter so that the result is placed into a designated field (i.e not the original one holding the unexploded data)?

Thanks in advance.

Regards
Chris

Submitted by support on Thu, 2014-11-06 14:41

Hi Chris,

You could create a new Explode From Field filter as follows;

  /*************************************************/
  /* Explode From Field */
  /*************************************************/
  $filter_names["explodeFromField"] = "Explode From Field";
  function filter_explodeFromFieldConfigure($filter_data)
  {
    print "Field Name:<br />";
    print "<input type='text' size='40' name='field' value='".widget_safe($filter_data["field"])."' />";
    widget_errorGet("field");
    print "<br />";
    print "Explode Character or String:<br />";
    print "<input type='text' size='40' name='text' value='".widget_safe($filter_data["text"])."' />";
    widget_errorGet("text");
    print "<br />";
    print "Return Index: (zero based)<br />";
    print "<input type='text' size='3' name='index' value='".widget_safe($filter_data["index"])."' />";
    widget_errorGet("index");
  }
  function filter_explodeFromFieldValidate($filter_data)
  {
    if (!$filter_data["field"])
    {
      widget_errorSet("field","required field");
    }
    if (!$filter_data["text"])
    {
      widget_errorSet("text","required field");
    }
  }
  function filter_explodeFromFieldExec($filter_data,$text)
  {
    global $filter_record;
    $parts = explode($filter_data["text"],$filter_record[$filter_data["field"]]);
    $index = intval($filter_data["index"]);
    if ($index < 0)
    {
      $index = count($parts) + $index;
    }
    return $parts[$index];
  }

Attach to the field you want to contain the result, but then on the configuration page you can enter the name of an alternative field on which to perform the Explode and extract the result from. Field name, as with placeholders, exactly as appears in the sample data on Feed Registration Step 2...

Cheers,
David.
--
PriceTapestry.com

Submitted by ChrisNBC on Thu, 2014-11-06 16:00

Hi David,

Thanks for the attached. I just added the new filter but something seems to be wrong as no configuration fields are displayed when I try to use it. I initially thought this was because the filter clashed with the existing 'Explode' filter so I replaced the original with the new one above but it still doesn't work.

Would be grateful if you could suggest how I might resolve this.

Thanks in advance.

Regards
Chris

Submitted by support on Thu, 2014-11-06 16:08

My apologies Chris, it was the $filter_names entry that was wrong;

   $filter_names["explodeFromField"] = "Explode From Field";

...corrected above.

Cheers,
David.
--
PriceTapestry.com

Submitted by ChrisNBC on Fri, 2014-11-07 11:47

Hi David,

Thanks for the update. The filter now works perfectly.

Best regards
Chris

Submitted by ChrisNBC on Thu, 2014-12-18 17:27

Hi David,

I'm using the above filter to explode the product colour in some merchants feeds. Where the colour is after the specified separator the filter works beautifully. The problem arises where the specified separator is not present, in this scenario the filter takes the full contents of the searched field (product_name) and places it into the 'colour' field. I wondered if you could suggest a way I might remedy this so if the specified explode character or string is not present the filter ignores the row?

Thanks in advance.

Regards
Chris

Submitted by support on Thu, 2014-12-18 19:27

Hi Chris,

Sure - the following replacement will only have any effect if the Explode Character or String is present:

  /*************************************************/
  /* Explode From Field */
  /*************************************************/
  $filter_names["explodeFromField"] = "Explode From Field";
  function filter_explodeFromFieldConfigure($filter_data)
  {
    print "Field Name:<br />";
    print "<input type='text' size='40' name='field' value='".widget_safe($filter_data["field"])."' />";
    widget_errorGet("field");
    print "<br />";
    print "Explode Character or String:<br />";
    print "<input type='text' size='40' name='text' value='".widget_safe($filter_data["text"])."' />";
    widget_errorGet("text");
    print "<br />";
    print "Return Index: (zero based)<br />";
    print "<input type='text' size='3' name='index' value='".widget_safe($filter_data["index"])."' />";
    widget_errorGet("index");
  }
  function filter_explodeFromFieldValidate($filter_data)
  {
    if (!$filter_data["field"])
    {
      widget_errorSet("field","required field");
    }
    if (!$filter_data["text"])
    {
      widget_errorSet("text","required field");
    }
  }
  function filter_explodeFromFieldExec($filter_data,$text)
  {
    global $filter_record;
    if (strpos($filter_record[$filter_data["field"]],$filter_data["text"])===FALSE) return $text;
    $parts = explode($filter_data["text"],$filter_record[$filter_data["field"]]);
    $index = intval($filter_data["index"]);
    if ($index < 0)
    {
      $index = count($parts) + $index;
    }
    return $parts[$index];
  }

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by ChrisNBC on Fri, 2014-12-19 12:23

Hi David,

Thanks for the quick response and for the new filter code. I have tried it a couple of times now and it seems to have no effect. I wondered if you could suggest what might be going wrong and how I might resolve it.

Thanks in advance.

Regards
Chris

Submitted by support on Fri, 2014-12-19 12:41

Hi Chris,

Sorry about that - the test to ignore wasn't using the correct fields - corrected above!

Cheers,
David.
--
PriceTapestry.com

Submitted by ChrisNBC on Fri, 2014-12-19 15:28

Hi David,

The above code looks identical? Did you update it?

Regards
Chris

Submitted by support on Fri, 2014-12-19 15:41

Hi Chris,

This is the modified line:

     if (strpos($filter_record[$filter_data["field"]],$filter_data["text"])===FALSE) return $text;

...so it's checking if the Explode Character or String exists in the field being tested first, and if not returns the field value to which the filter is being applied unmodified...

Are you still seeing no effect from the change?

Cheers,
David.
--
PriceTapestry.com

Submitted by ChrisNBC on Fri, 2014-12-19 16:49

Hi David,

Thanks as always for your quick response. Apologies, I just noticed a 1k difference between my local copy (larger) and my remote server copy of the filter file, the upload must have failed (even though my editor confirmed the file transfer). I just ran the filters again and they work perfectly.

Thanks again.

Best regards
Chris