Skip to main content

PHP Grouping Results 88 Responses

Last post: 8 months, 3 weeks ago | Thread started: Mar 4, 08, 9:48 a.m.

RespondNew TopicDisable Images

  • seed

    Can anyone tell me how to group an output to only show the related category name once and display all items in that category? Example:

    Category 1
    Item 1
    Item 2
    Item 3

    Category 2
    Item 1
    Item 2
    Item 3
    Item 4

    In the database, item.cat_id = cat.id

    Mar 4, 08, 9:48 a.m. – Permalink
  • drgs

    tarzan not understand...

    next note >add note

    You must be logged in to add a note. Login now or register for an account.

    Cancel
    Dog-earMar 4, 08, 9:52 a.m. – Permalink
  • cosmoo

    http://dev.mysql.com/doc/refman/…

    • this is all you need.cosmoo
    + add note

    You must be logged in to add a note. Login now or register for an account.

    Cancel
    Dog-earMar 4, 08, 9:53 a.m. – Permalink
  • hyt

    SELECT i.tem, c.category FROM item AS i, cat AS c WHERE i.cat_ID = $categoryID AND c.ID = $categoryID

    $categoryID needs to come from somewhere though...

    (Also the above is probably somewhat convoluted but I'm tired...)

    next note >add note

    You must be logged in to add a note. Login now or register for an account.

    Cancel
    Dog-earMar 4, 08, 9:54 a.m. – Permalink
  • maximillion_

    look up "group by" clause on mysql.com

    next note >add note

    You must be logged in to add a note. Login now or register for an account.

    Cancel
    Dog-earMar 4, 08, 9:56 a.m. – Permalink
  • seed

    I get the idea of 'group by' but Im not sure how to make the result output the Category name once above each grouping. I am looking at the links above also.

    next note >add note

    You must be logged in to add a note. Login now or register for an account.

    Cancel
    Dog-earMar 4, 08, 10:21 a.m. – Permalink
  • maximillion_

    assuming you link the categories to the items via an id, i would put the category names into an array with the id as key. do yr query and loop through the items detecting when the cat id changes (record the last id used and then detect when its different) then use the id to ref the name in the array you made

    next note >add note

    You must be logged in to add a note. Login now or register for an account.

    Cancel
    Dog-earMar 4, 08, 10:25 a.m. – Permalink
  • heavyt

    Agree with Maxi.
    What I have done in the past is to use a var to hold the current category name. As you loop through your results, check each time if the category == that var. If it does, you are still in the same category. If it is not equal, you know you've moved on to the next cat.

    next note >add note

    You must be logged in to add a note. Login now or register for an account.

    Cancel
    Dog-earMar 4, 08, 10:28 a.m. – Permalink
  • seed

    That sounds like the way to go. I am weak on arrays. I'll work on that. Thanks.

    next note >add note

    You must be logged in to add a note. Login now or register for an account.

    Cancel
    Dog-earMar 4, 08, 10:28 a.m. – Permalink

Login or Register to respond to this

Skip to main content