You are here:  » global drop for categories ?

Support Forum



global drop for categories ?

Submitted by alecs on Fri, 2009-09-11 16:49 in

hi david,

is there any chance to get a solution for global droping categories/names ?
i got so many categories that comes with the feeds witch doesnt fits the
content of my comparsion. but to drop any single categorie (with filters) costs me ours.

thanks
alecs

Submitted by support on Sat, 2009-09-12 08:26

Hi Alecs,

It's straight forward to add global drop code to the import record handler function. To do this, look for the following code starting at line 170 of includes/admin.php

    if ($admin_importFeed["field_category"])
    {
      $record[$admin_importFeed["field_category"]] = tapestry_normalise($record[$admin_importFeed["field_category"]]);
    }

...and then ADD the following new code immediately afterwards...

$dropCategories = array("Category 1","Category 2");
if (in_array($record[$admin_importFeed["field_category"]],$dropCategories)) return;

...simply edit the values in the $dropCategories array exactly as they appear imported (i.e. after normalisation) as required...

Hope this helps!

Cheers,
David.

Submitted by coyote on Thu, 2009-10-15 05:55

Hi David

how are you ;)

I was looking for the same thing although my search was a little bit different :

actullay i dont want to drop exact categories names, i want to drop record IF the categories names contain a list of terms

(exactly as does the function filter but it would be easier to do it directly in admin.php as there are many)

so something like

if category contains any of these terms ("term1","term2","term3","term4","term5","term6","term7","term8" etc)
then drop record of theses categories but record the others

Another question :

Same thing with the filter search and replace would be possible ?

as i have for some feeds like 200 search and replace to do (on title, description etc) it would be perfect to do it in admin.php (i am also thinking it would be also interesting for the use of synonyms for products descriptions to fight against duplicate content ;) )

Thanks for your help :)

Coyote

Submitted by support on Thu, 2009-10-15 07:29

Hi Coyote,

Yes - both are straight forward to do within the import record handler in includes/admin.php.

As an alternative to the modification described above; use something like:

$dropWords = array("Term 1","Term 2","Term 3");
foreach($dropWords as $dropWord)
{
  if (strpos($record[$admin_importFeed["field_category"]],$dropWord) !== FALSE) return;
}

For search and replace, the same point in the code is as good as anywhere. Use something like this:

$searchReplace = array(
"Search 1" => "Replace 1",
"Search 2" => "Replace 2",
"Search 3" => "Replace 3"
) // ^ remember no comma after the last one
foreach($searchReplace as $search => $replace)
{
  $record[$admin_importFeed["field_name"]] =
    str_replace($search,$replace,$record[$admin_importFeed["field_name"]]);
  $record[$admin_importFeed["field_description"]] =
    str_replace($search,$replace,$record[$admin_importFeed["field_description"]]);
}

Cheers,
David.

Submitted by coyote on Thu, 2009-10-15 08:41

Hi David

I just tested the first code, i works like a charm tx ;)

For the "Search and replace" code

if i want to do replace in name first and then other search and replace in description should i do like this ? :

$searchReplacename = array(
"Search 1" => "Replace 1",
"Search 2" => "Replace 2",
"Search 3" => "Replace 3"
) // ^ remember no comma after the last one
foreach($searchReplacename as $search => $replace)
{
  $record[$admin_importFeed["field_name"]] =
    str_replace($search,$replace,$record[$admin_importFeed["field_name"]]);
}
$searchReplacedescription = array(
"Search 1" => "Replace 1",
"Search 2" => "Replace 2",
"Search 3" => "Replace 3"
) // ^ remember no comma after the last one
foreach($searchReplacedescription as $search => $replace)
{
  $record[$admin_importFeed["field_description"]] =
    str_replace($search,$replace,$record[$admin_importFeed["field_description"]]);
}

Submitted by support on Thu, 2009-10-15 09:35

Hello Coyote,

Yes - that would work fine!

Cheers,
David.

Submitted by coyote on Thu, 2009-10-15 09:51

I just tested :

when i try to import a feed it tells me :

Parse error: syntax error, unexpected T_FOREACH in /home/xxxxxxx/admin.php on line 196

$searchReplacename = array(
"ê" => "e",
"â" => "a",
"ç" => "c",
"´" => " ",
" r " => " a ",
"crcme" => "creme"
) // ^ remember NO COMMA after the last one
foreach($searchReplacename as $search => $replace) <-------------------- line 196
{
  $record[$admin_importFeed["field_name"]] =
    str_replace($search,$replace,$record[$admin_importFeed["field_name"]]);
}

Submitted by support on Thu, 2009-10-15 09:54

Hi Coyote,

My apologies - there are semi-colons missing after the arrays; it should be:

$searchReplacename = array(
"Search 1" => "Replace 1",
"Search 2" => "Replace 2",
"Search 3" => "Replace 3"
); // ^ remember no comma after the last one
foreach($searchReplacename as $search => $replace)
{
  $record[$admin_importFeed["field_name"]] =
    str_replace($search,$replace,$record[$admin_importFeed["field_name"]]);
}
$searchReplacedescription = array(
"Search 1" => "Replace 1",
"Search 2" => "Replace 2",
"Search 3" => "Replace 3"
); // ^ remember no comma after the last one
foreach($searchReplacedescription as $search => $replace)
{
  $record[$admin_importFeed["field_description"]] =
    str_replace($search,$replace,$record[$admin_importFeed["field_description"]]);
}

(see immediately infront of the "remember no comma after the last one" comments!)

Cheers,
David.

Submitted by coyote on Thu, 2009-10-15 10:15

It works perfectly !

Tx David ;)

Submitted by coyote on Thu, 2009-10-15 12:57

Hi again

Just a thought :

In terms of performance, is it possible to have lets say 1000 entries
"word 1" => "word 2",

for example with synonyms

Is it a problem ? is it longer to import if they are 1000 replace in admin.php ?

Tx.
Coyote

Submitted by support on Thu, 2009-10-15 13:04

Hi Coyote,

It should be fine, but for that number, I would move the array to an external file so that it is only included (and PHP has to build the array) only once, rather than every record.

To do this, create a new file called strings.php in the /includes/ folder.

In the new file, just have these parts of the code:

<?php
$searchReplacename 
= array(
"Search 1" => "Replace 1",
"Search 2" => "Replace 2",
"Search 3" => "Replace 3"
); // ^ remember no comma after the last one
$searchReplacedescription = array(
"Search 1" => "Replace 1",
"Search 2" => "Replace 2",
"Search 3" => "Replace 3"
); // ^ remember no comma after the last one
?>

And replace where each section came from (in includes/admin.php) with:

  global $searchReplacename;
  global $searchReplacedescription;

Finally, at the very top of includes/admin.php (on the line after the opening PHP tag) add:

  require("../includes/strings.php");

Cheers,
David.

Submitted by coyote on Thu, 2009-10-15 15:16

Hi David

I understand for the first part of what you say

but what do you mean by "And replace where each section came from"

where should i put these :

  global $searchReplacename;
  global $searchReplacedescription;

anywhere in admin.php ?

is it the same as what you gave me before ? :

foreach($searchReplacedescription as $search => $replace)
{
  $record[$admin_importFeed["field_description"]] =
    str_replace($search,$replace,$record[$admin_importFeed["field_description"]]);
}

Tx for your help

Coyote

Submitted by support on Thu, 2009-10-15 15:22

Hi Coyote,

It's the array's themselves that want to go in the new file (strings.php); whereas the PHP code that actually does the search and replace needs to stay in admin.php.

Overall, based on the examples above you would have in strings.php:

<?php
$searchReplacename = array(
"Search 1" => "Replace 1",
"Search 2" => "Replace 2",
"Search 3" => "Replace 3"
); // ^ remember no comma after the last one
$searchReplacedescription = array(
"Search 1" => "Replace 1",
"Search 2" => "Replace 2",
"Search 3" => "Replace 3"
); // ^ remember no comma after the last one
?>

...and then in admin.php:

global $searchReplacename;
foreach($searchReplacename as $search => $replace)
{
  $record[$admin_importFeed["field_name"]] =
    str_replace($search,$replace,$record[$admin_importFeed["field_name"]]);
}
global $searchReplacedescription;
foreach($searchReplacedescription as $search => $replace)
{
  $record[$admin_importFeed["field_description"]] =
    str_replace($search,$replace,$record[$admin_importFeed["field_description"]]);
}

In other words; each of the array() constructions has been replaced with the global statement, so that that the array is called in from the external strings.php...

Cheers,
David.

Submitted by coyote on Fri, 2009-10-16 01:23

Tx David ;)

Works nice

Submitted by HJW on Mon, 2009-11-30 11:50

Hi David,

I've been wondering if it would be possible to use the search and replace mod as above, except I would like the replacement word to be a hyperlink.
Is this possible?

Regards

Hayden

Submitted by support on Mon, 2009-11-30 11:53

Hi Hayden,

That should work fine with regards to the description - just make sure that the code goes into admin.php after the following line:

$record[$admin_importFeed["field_description"]] = strip_tags($record[$admin_importFeed["field_description"]]);

(line 163 in the distribution)

As the above line removes HTML from the description field it would remove your links if it occurred after the search and replace code...

Cheers,
David.

Submitted by HJW on Mon, 2009-11-30 15:21

Hello again,

I'm trying this out on the new distribution and i can't find

$record[$admin_importFeed["field_description"]] = strip_tags($record[$admin_importFeed["field_description"]]);

in admin.php? Should I looks somewhere else?
Also i'm a bit slow, what code would I need to use for the hyperlinks?

Regards

Hayden

Submitted by support on Mon, 2009-11-30 15:28

Hi Hayden,

It works slightly differently in the new version - HTML is not stripped from the description by default so that line no longer exists. Also, a slightly different variable name will be required to access the description field. Here's an example of the code to use in the new version, replacing keywords with a link:

Firstly, in your strings.php:

$searchReplacedescription = array(
"Red" => "<a href='http://www.example.com/red.html'>Red</a>",
"Green" => "<a href='http://www.example.com/green.html'>Green</a>",
"Blue" => "<a href='http://www.example.com/blue.html'>Blue</a>"
); // ^ remember no comma after the last one

And then in includes/admin.php, look for the following comment around line 186:

/* apply user filters */

..and ADD the following new code at that point:

global $searchReplacedescription;
foreach($searchReplacedescription as $search => $replace)
{
  $importRecord["description"] = str_replace($search,$replace,$importRecord["description"]);
}

Hope this helps!

Cheers,
David.

Submitted by HJW on Mon, 2009-11-30 16:17

Hi David,

Many thanks, I had used the correct code after all, but the code missing from the new distribution threw me a bit!
Helpful as ever,thank you.

Regards

Submitted by HJW on Fri, 2009-12-04 20:41

Hello David,

I've been using this code with limited success, it works for some links but not others, for example if using a url such as
http://www.xxxxxx
the code breaks because of the forward slashes '//' it comments the remainder of the line out (as in php).
I've been trying to find a way around this but not with much luck, is there a way around this that you are aware of?

Regards

Hayden

Submitted by support on Sat, 2009-12-05 09:18

Hello Hayden,

PHP should know that it is within a string at that point, so the // should not be behaving as a comment - but having said that I have recently had a support issue with one of my other scripts where a similar thing was happening - very strange.

The solution is to break it up into 2 strings so that that // does not appear in one place - for example:

"Red" => "<a href='http:/"."/www.example.com/red.html'>Red</a>",

Hope this helps!

Cheers,
David.

Submitted by HJW on Sat, 2009-12-05 10:21

Thanks David,

That done the trick, I had tried so many things, in the end it was so simple!

Thanks again,

Hayden

Submitted by FirstByte on Sun, 2010-02-07 04:12

Hi David,

I'm trying to implement the first line of code. When i searched for "if ($admin_importFeed["field_category"])" code I found the following code below from the "includes/admin.php" file. I currently have the latest version installed.

    if ($admin_importFeed["field_category"])
    {
      $importRecord["category"] = $record[$admin_importFeed["field_category"]];
    }

The code is slightly different but was wondering who do i apply the global drop feature to it? Do I still add the new code to just below the above code?

Cheers
Fb

Submitted by support on Mon, 2010-02-08 09:35

Hi,

It's very similar - for the latest version, immediately after the point that you have identified; use:

$dropCategories = array("Category 1","Category 2");
if (in_array($importRecord["category"],$dropCategories)) return;

Cheers,
David.

Submitted by babrees on Thu, 2010-07-29 05:48

Hi David

How would I do the opposite? ie include these categories instead of dropping

Cheers

---------
Jill

Submitted by support on Thu, 2010-07-29 08:39

Hi Jill,

The ! operator (not) in-front of in_array will do the trick. Have a go with:

$dropCategories = array("Category 1","Category 2");
if (!in_array($importRecord["category"],$dropCategories)) return;

Cheers,
David.