Jump to content

SQL Categories


wemustdesign

Recommended Posts

Hi,

 

I have two tables and I am wanting to output them all on a page but I would like to do this by sorting them by cat name so would look like:

 

Animals

  Tigers

  Dogs

Buildings

  Castles

 

This is how I have my tables set up:

 

tbl: subject

 

id              name                  cat

1              Tigers                  1

 

tbl: cat

 

id              name

1                Animals

 

any ideas are much appreciated

Link to comment
Share on other sites

Hi,

 

Thanks for the reply, yes I am going to code this in PHP. I know you can order by 2 columns but how would this work is what I am wandering?

 

So if I 'ORDER BY name ' for the categories how do I then output all of the subjects under that category before the mysql outputs the next  category?

 

This is the part I cannot get my head round?

Link to comment
Share on other sites

I have a category table to categorise all of my subjects. This way they can output (when I get it working) in a category structure and will be used for other things as well.

 

Animals (Cat)

    Tiger (Subject)

    Birds

    Lion

Buildings (Cat)

    Castles (Subject)

    Church

     

Link to comment
Share on other sites

Oh, 'cat' like 'category', not like the animal.  That's why abbreviations are evil.

 

So, back to your original question -- simply use a seen hash, and create a structure accordingly (push()-ing onto a keyed hash by each category for each animal), and then the output it is a breeze.

 

That way, you can drop the ORDER BY, too -- and save the DB some work.

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.