Gather fields under category in loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bonnielass
    New Member
    • Feb 2010
    • 3

    Gather fields under category in loop

    I have mysql table as following.

    category
    title
    link

    and I wish to loop them sorted by category and then title asc. Is it possible to echo the category field only once and gather title and link under it?

    I guess I could do a loop for each category by sorting each in their own sql-query like: "SELECT category, title, link from TABLE order by category asc, title ASC WHERE category =bananas" but is there a more elegant way of doing it without splitting categories in different sql-queries?
  • dlite922
    Recognized Expert Top Contributor
    • Dec 2007
    • 1586

    #2
    Not sure what you want to do still.

    Give us an example of the output you expect.






    Dan

    Comment

    • bonnielass
      New Member
      • Feb 2010
      • 3

      #3
      I'll try to clarify.

      If, let's say, I have db as following:

      fields: country, name.
      inputs: Sweden, David Davidson
      Sweden, Curt Curtson
      Norway, Hugo Hugoson

      And wish to have an output like this:

      Sweden
      David Davidson
      Curt Curtson

      Norway
      Hugo Hugoson

      If I echo in a loop now i get

      Sweden
      David Davidson
      Sweden
      Curt Curtson
      Norway
      Hugo Hugoson

      and I dont want the country to be echoed for every row, only one time to create a heading for the other rows with the same country. I am an enthustiastic newbie and I might have missed something very obvious.

      Comment

      • zorgi
        Recognized Expert Contributor
        • Mar 2008
        • 431

        #4
        Well for starter I recommend not to have all data in one table. In the example you gave us I would have minimum 2 tables.

        Country
        country_id
        country_name

        Person
        person_id
        country_id (FK)
        person_name

        Or to complete normalization i would have 3 tables:

        Country
        country_id
        country_name

        Country_x_Perso n
        country_id (FK)
        person_id(FK)

        Person
        person_id
        person_name

        Its simplified but gives you an idea. You should do some reading on database normalization

        Comment

        • bonnielass
          New Member
          • Feb 2010
          • 3

          #5
          I suspected as much. Thank you!

          Comment

          Working...