Forum Moderators: coopster

Message Too Old, No Replies

links in multiple sub categories

looking for the most efficient way

         

mattennant

12:57 pm on Jul 7, 2008 (gmt 0)

10+ Year Member



i am building a site with categorised links

some links appear in multiple categories.

i have successfully managed to do this as below. my question is. ...Is this the most efficient way to do this? i have read that it's not good form to store arrays in a database.

I have two tables

link_category made up of
linkcat_id
linkcategory e.g webdesigners

link_detail made up of
linkdetial_id
linkdtail_link
linkcat_id (istored in an array ie 1,3,6)

i am using the following query to pull out the relevant links in a category

SELECT * FROM `link_detail`
WHERE (FIND_IN_SET('1' , linkcat_id) > 0)

this pulls out all the links with a'1' in the array

any advice, Is there a alternative way to achieve the same result?

cheers

mat

cameraman

3:10 pm on Jul 7, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can create a third table which relates the first two that has just two fields, say linkcat_id and linkdetail_id (primary key is both fields). Then
SELECT c.linkcat_id AS cat, d.* FROM catmembers c, link_detail d WHERE ((c.linkcat_id=1) AND (d.linkdedtail_id=c.linkdetail_id))

The 'AS cat' above just keeps that field name from clashing with the one in the link_detail table.

mattennant

5:34 pm on Jul 7, 2008 (gmt 0)

10+ Year Member



That works a treat and makes sense, thanks so much cameraman. As usual one question leads to another...

would it be a huge leap to offer a user the ability to select link categories with checkboxes
[ ] webdesigner
[ ] webdesigner south london
[ ] webdesigner north london
presumably i would pass the selected items array to the results page, hmm not sure.

I tried adding an extra cat_id to the mysql statement with no success (below)
WHERE ((c.linkcat_id=1 )AND(c.linkcat_id=3 )AND (d.linkdetail_id=c.linkdetail_id))

This statement i realise would have to change dynamically.

Could this be a step too far i wonder.

cameraman

6:03 pm on Jul 7, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Naawww, it's never a step too far!

Roll back from your monitor just a bit to have a look and you'll see why that WHERE phrase doesn't work - the category can't be both 1 and 3 simultaneously. You could do an OR, or you could do it as an array:
WHERE ((c.linkcat_id IN (1,3)) AND (d.linkdetail_id=c.linkdetail_id))
You could build either one dynamically without too much trouble, although I think the array is 'tidier'.

To process the checkboxes, you would foreach through the posted array and add a row to catmembers for each checked category.

mattennant

7:20 pm on Jul 7, 2008 (gmt 0)

10+ Year Member



thanks cameraman, going to look at this with some fresh eyes in the morning, and see what i can manage. The array makes sense now.
I believe i need to look into achieving distinct results too, so a link in two categories is not repeated.
Thanks again for your help