Easy Way of converting string into categories, subcategories ?

Discussion in 'PHP' started by kutchbhi, Jan 27, 2014.

  1. #1
    Say we have a csv file :
    Is there a php solution to dynamically convert the category string into mysql nested categories and subcategories ? something like

    categories table :
    id | cate | parent_id
    1 | toys | 0
    2 | baby toys | 1
    3 | Baby Rattles | 2

    and so on
    Further categories are first checked if they exist , and if they do just return their id.

    I have implemented this solution before , but it was a bunch of messy copy paste code. Wondering if there is an existing script or a known method to code this ?

    Thanks
     
    kutchbhi, Jan 27, 2014 IP
  2. TIEro

    TIEro Active Member

    Messages:
    741
    Likes Received:
    177
    Best Answers:
    5
    Trophy Points:
    70
    #2
    Grab the WordPress plugin "Batch category import" - even if you don't use WordPress. It's written in php and it takes a string like that (though it's category->subcat) and processes it into WP categories and subcats.

    You should be able to decipher the code and redo it a bit to do what you want, though you might have a stumble over the WP terms. It'll be a good starting point, at least.
     
    TIEro, Jan 27, 2014 IP
  3. kutchbhi

    kutchbhi Active Member

    Messages:
    130
    Likes Received:
    4
    Best Answers:
    2
    Trophy Points:
    70
    #3
    Thanks, I'll try and see if I can decipher that plugin.
     
    kutchbhi, Jan 27, 2014 IP
  4. TIEro

    TIEro Active Member

    Messages:
    741
    Likes Received:
    177
    Best Answers:
    5
    Trophy Points:
    70
    #4
    If you run into significant problems, pop me a PM or contact me through my site (signature). I'm no pro, but I know WP/SQL fairly well. It's obviously better if you can produce your own (especially for maintenance!) but I'm always happy to try to lend a hand.
     
    TIEro, Jan 27, 2014 IP
  5. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #5
    Preferrably, you'd create several tables for this in the database - that's what relational databases is for.
    Table 1: categories - id, name
    Table 2: sub_categories - parent_id (from categories table), id, name
    Table 3 to infinity: name_of_final_category - parent_id (from sub_categories table), id, name
     
    PoPSiCLe, Jan 27, 2014 IP
  6. kutchbhi

    kutchbhi Active Member

    Messages:
    130
    Likes Received:
    4
    Best Answers:
    2
    Trophy Points:
    70
    #6
    ^I was thinking of following the adjacency list model. Besides wouldn't dividing into several tables won't be expensive when trying to retrieve the full tree ?

    THANKS that is very generous of you :)
     
    kutchbhi, Jan 27, 2014 IP
  7. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #7
    No. Not really. Normally, you wouldn't want to get the whole three, either. You'll pull the main categories, and if one of them are clicked, pull the sub-categories, and so forth. However, pulling all categories won't be that much more expensive, it's just a matter of joining a few tables together.
     
    PoPSiCLe, Jan 27, 2014 IP
  8. kutchbhi

    kutchbhi Active Member

    Messages:
    130
    Likes Received:
    4
    Best Answers:
    2
    Trophy Points:
    70
    #8
    ^I see . I guess I just have to finally learn how to use JOIN's
     
    kutchbhi, Jan 27, 2014 IP