Forum Moderators: coopster & phranque

Message Too Old, No Replies

Assigning a category

unknown levels for different cats

         

ukgimp

12:49 pm on Sep 20, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello all

I was wondering if I could trouble some of you for some advice. I have been working on this problem for a number if days, but I cannot figure out the best/slickest way (or even one that works :()

I have a directory of events and wish to be able to assign a category to each one via FK. Simple if I only had one level to go to, but it has more than one and some categories have more than others.

I only want an event to be assingned a botton level id, so something like the phpHoo system will not really work for me.

Ideally I would like users of the site to submit their own events once they have authenticated and give there event a classification. I am looking for the slickest way of doing with. Dropdpowns would be my favoured approach as it is difficult to get that wrong. I have seen the nested java drop downs but I am not too keen on that.

I have created a page that posts to itself when you pull the box down and finds all the children of a cat, but that seems messy too (and throws an error when there are no lower cats)

I suppose I could produce a big mammoth list of radio buttons but that could get messy if I have hundreds of cats.

Any advice/examples sites (sticky if necessary) - my head hurts.

cheers

jatar_k

6:10 pm on Sep 20, 2002 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You could have it check for child cats when they make a selection in the drop down. If there are no child cats then it doesn't need to repost. If there are you can repost it and add the new dropdown.

transistor

6:51 pm on Sep 20, 2002 (gmt 0)

10+ Year Member



Hello Ukgimp,
If I understand correctly, you want an event to have a category and then a sub-category, am I correct? (or maybe even more sub-categories).

I have something of this sort, to classify news on my site.

I set up a table, let's call it CATEGORIES (suprisingly creative, I know :)).
It has a NAME field and you might want to add others.

On another table, I have SUBCATEGORIES (I'm quite creative today!), this one has a field NAME and CATEGORIE (to know which categorie it belongs... but you already knew that, didn't you?).

Now, I wrote a function that makes a JavaScript array to fill a select field with the sub-categories of the selected categorie (chosen in another select field).
If you'd like to have it, I can Sticky it to you.

I only have 2 levels, but I'm sure it can easily "deepened" to fit your needs.

Questions? send 'em over, I'll try to confuse... er.. help you more.

sausho

5:42 am on Sep 24, 2002 (gmt 0)

10+ Year Member



My idea is to first ensure youhave a logically correct and recommended database structure for this problem:

TABLE1 (categories)
--------------------
id cat_name is_under
----------------------
1 HOT 0
2 COLD 0
3 Pizza 1
4 Cold-drink 2
5 Coke 4

This structure means that top level categories have a 0 in their 'is_under' coloumn

TABLE2 (cat_lookup)
------------------------
id cat_id member_id
--------------------------
1 5 263
2 3 123
---------------------------

For user interface one idea may be to


if (!isset($_SESSION('cat_level')))
{
$sql='SELECT id,cat_name FROM categories WHERE is_under=0';
//show radiobuttns or whatever with catnames and id to choose from
//lets say on submission the selected categoruies id is posted in variable 'catid'
$_SESSION['cat_level']=0;
}
else
{
$sql='SELECT id,cat_name FROM categories WHERE is_under='.$_POST['catid'];
if (rows returned are 0)
{
//category chosen finally
$sql='INSERT INTO cat_lookup(mem_id,cat_id) VALUES(...)';
header('Location: thankyou.htm');
exit;
}
//show radiobuttns or whatever with catnames and id to choose from
//lets say on submission the selected categoruies id is posted in variable 'catid'
}

Just my idea of implementing yuor problem...this code assumes that if a category has subcategories, only the last category in a tree can be selected by a user. Like in FOOD > HOT > Chinese > Chow-mein ..the user can only choose chowmein ..not HOT
You may alter the code as per your requriement. Hope this helps.

jatar_k

6:48 am on Sep 24, 2002 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld [webmasterworld.com] sausho

sausho

1:17 pm on Sep 24, 2002 (gmt 0)

10+ Year Member



Thank you jatar_k.
This board is very friendly. More than 1 moderators I met in threads welcomed me :)