I've been re doing this part of the code to try and map the sub category as well as the category. This part was working fine, basically splitting down the categories till i'm left with the last part. That's then mapped during import. If a mapping is not found a > is appended to the end so i can easily find the unmapped categories later on.
/* apply category mappings */
/* The category is selected from category alternates which have been mapped prior to import. */
$category = $importRecord["category"];
// Explode category using > and count how many sub cats and assign them to $subcat
$parts = explode(">",$category);
$maincat = $parts[0];
count($parts) > 1 ? $subcat = $parts[1] : $subcat = $parts[0];
if (isset($admin_importCategoryMappings["=".$importRecord["category"]]))
{
$importRecord["category"] = $admin_importCategoryMappings["=".$importRecord["category"]];
}
else
{
foreach($admin_importCategoryMappings as $k => $v)
{
$found = 0;
$words = explode(" ",$k);
foreach($words as $word)
{
if ($word)
{
if (strpos(strtolower($subcat),$word) !== FALSE) $found++;
}
}
if ($found == count($words))
{
$importRecord["category"] = $v;
break;
}
}
//If category mapping is not found append > to end so we know which one's aren't mapped.
if($found == 0)
{
$importRecord["category"] = $category." >";
}
}
The issue i'm having now that i've just noticed whilst trying to get the filter side results working. I have all the categories including the parent categories all in the categories field in the products table.
So i'm looking for a way to get the parent category using the subcategories parentid.
It's frying my brain a bit :(
$admin_importCategoryMappings = array();
$admin_importSubCategory = array();
/* Search through mapped categories and check alternate mappings for a match on category name */
$sql = "SELECT * FROM `mapped_categories`";
if (database_querySelect($sql,$rows))
{
foreach($rows as $category)
{
$alternates = explode("\n",$category["alternates"]);
foreach($alternates as $alternate)
{
$alternate = strtolower(trim($alternate));
$sql = "SELECT map.name as mapped_name, map.cat_id, map.alternates, cat.name as cat_name FROM mapped_categories as map, categories as cat WHERE cat.parentid = (SELECT parentid FROM categories WHERE name=".$category["name"].") AND map.name = cat.name LIMIT 1";
database_querySelect($sql,$rows);
$admin_importSubCategory[$category["name"]] = $rows[0]["mapped_name"];
$admin_importCategoryMappings[$alternate] = $category["name"];
}
}
}
^ i haven't tried if that works, somehow i doubt it. i was just wondering if you know of a better more efficient way of getting the parent category name without having to run that on every mapped item.
`categories` (`id`, `name`, `parentid`)
(1, 'main cat', NULL),
(2, 'sub cat', '1'),
The sub categories have the parentid set where as the parent categories have the field set as NULL.
`mapped_categories` (`id`, `name`, `alternates`, `cat_id`)
cat_id is the foreign key for id in the categories table.
I have it working when i update a category using a page i use for mapping the categories.
I'm just looking to get it automated during the import process.
If you could advise it would be brilliant. I did take a different approach originally but it didn't pan out.
Thank You
Steve
Great idea thanks.
Just wondering where the best place to have it in the admin.php. would be?
Hi Steve,
I tend to position helper functions just above the main function in which you use it but that's only for development convenience - PHP functions can be defined anywhere within the loaded scripts so top or end of includes/admin.php would be fine...
Cheers,
David.
--
PriceTapestry.com
Have it working perfect now thank you again.
Steve
Hi Steve,
One tidy way to go about this kind of thing is to create a helper function which will return the parent category name from the parentid field - and the function itself can use a global variable to store the lookup table so that the query only needs to be made once. Consider something like this:
function categoryName($id)
{
global $categoryNames;
if (!isset($categoryNames))
{
$sql = "SELECT id,name FROM `categories`";
if (database_querySelect($sql,$rows))
{
foreach($rows as $row)
{
$categoryNames[$row["id"]] = $row["name"];
}
}
}
return $categoryNames["id"];
}
Then in your script, wherever you want the parent category from a parentid in context, use something like:
$parentCategory = categoryName($row["parentid"]);
Hope this helps!
Cheers,
David.
--
PriceTapestry.com