Results 1 to 4 of 4
  1. #1
    ABW Ambassador Doug247's Avatar
    Join Date
    January 18th, 2005
    Location
    DE USA
    Posts
    931
    Grouping with PHP and MySQL
    Does anyone know how to have Records grouped??

    Ex:
    Select * from tbl_shoes group by fld_color;


    The output should be something like:

    RED SHOES
    Red Shoe1
    Red Shoe2
    Red Shoe3
    Red Shoe4

    BLUE SHOES
    Blue Shoe1
    Blue Shoe2
    Blue Shoe3
    Blue Shoe4


    And So on

    Thanks,
    Doug

  2. #2
    Resident Genius and Staunch Capitalist Leader's Avatar
    Join Date
    January 18th, 2005
    Location
    Florida
    Posts
    12,817
    I'm no PHP expert yet but this one looks easy enough...

    Assuming you have a shoe-color column, this should work:

    Code:
    SELECT * from table_name ORDER BY column_name
    (note: not "group by")
    (That's the straight MySQL query. [Now I know why people say, "it depends on what you want to do!" IF you want to have it so the user affects the page's output by selecting something, you'll need to pass the user's selection into the code with a variable. But IF you just want the page to do the same thing every time, for instance for a site's main index, then this query should do ya... and there's probably a few other "ifs" I'm forgetting about!])

    It wouldn't put those headers you showed in, though, if they don't exist in your table. You'd get something like:

    BlueShoe A
    BlueShoe B
    RedShoe A
    RedShoe B
    RedShoe C
    YellowShoe A
    YellowShoe B

    etc. etc.
    There is no knowledge that is not power. ~Hemingway

  3. #3
    Not Verif-Lidated infoTim's Avatar
    Join Date
    January 18th, 2005
    Location
    Sunny Florida
    Posts
    1,021
    While you're in a loop fetching the rows from the query, check to see if the current color is different from the last color (keep a "last color" variable) and print a heading.

    - Tim
    Tim
    consultant by day, affiliate by night

  4. #4
    ABW Ambassador Doug247's Avatar
    Join Date
    January 18th, 2005
    Location
    DE USA
    Posts
    931
    [QUOTE=infospongellc]While you're in a loop fetching the rows from the query, check to see if the current color is different from the last color (keep a "last color" variable) and print a heading.

    Any chance at getting an example of what that script would look like??


    TIA,
    Doug

  5. Newsletter Signup

+ Reply to Thread

Similar Threads

  1. PHP and MySQL Knowledge
    By scm21st in forum Midnight Cafe'
    Replies: 5
    Last Post: August 5th, 2007, 10:30 PM
  2. looking for php/mysql work
    By spiderninja in forum Midnight Cafe'
    Replies: 0
    Last Post: January 31st, 2007, 11:12 PM
  3. Php Mysql - Repeating..??
    By AddHandler in forum Programming / Datafeeds / Tools
    Replies: 13
    Last Post: May 7th, 2005, 03:25 PM
  4. PHP & mySQL but....
    By perfectG in forum Programming / Datafeeds / Tools
    Replies: 1
    Last Post: November 6th, 2002, 06:58 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •