Jump to content

php csv import to mysql - explode categories array


quasiman

Recommended Posts

I am working on a csv file import to mysql, which will include items in one table (irrelevant here), and a hierarchical table of categories (id | name | parent).  I would think the simplest method to get each category name is too explode the categories from the csv into an array, for example:

$array = "root/Business/Employment";
$categories = explode("/", $array);

My problem though, is figuring out what the ID's are for each of the categories without knowing each name will be unique and not knowing for sure the full depth of each category hierarchy either.  So (I think) I need to rely on the category structure to define the lowest level ID.

 

So something like this:

SELECT
t1.parent AS p1,
t1.name AS lev1, 
t1.id AS cat1, 
t2.name AS lev2, 
t2.id AS cat2
FROM 
(categories AS t1)
LEFT JOIN categories AS t2 ON t2.parent = t1.id
WHERE t1.parent = '1'
AND t2.name = "Employment";

 

This gives me the ID of Employment, but it's easily possible that the name will be duplicated somewhere else in another heirarchy (in fact I get 4 results from phpmyadmin).  And since I don't know all these will be only 3 levels deep, it won't work all the time.

 

Any ideas help would be greatly appeciated!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.