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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.