SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Jan 2007
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Grouping of search results from an array

    I have code that is something like this:

    Code:
    $projects = mysql_query($select . $from . $where. $order);
    
    while ($project = mysql_fetch_array($projects)) {
    
      $PY		= $project["PY"];
      $PN 		= $project["PN"];
      $Title  	= $project["Title"];
    }
    And it gives me results are such as:

    PY(1) PN(1) Title(1)
    PY(1) PN(1) Title(2) ...and so on

    But what I want is if the PY is the same, then all the different PN's or Title's are grouped in the same output like this:

    PY(1) PN(1) Title(1) Tilte (2) ...and so on

    I know that this can be done, but I am having a heck of a time searching for an example on how to do this. For one thing, I do can't figure a way to clearly discribe this.

  2. #2
    Non-Member
    Join Date
    Apr 2006
    Location
    Scotland
    Posts
    325
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Didnt quite understand what you mean but try this:
    http://www.tizag.com/mysqlTutorial/mysqlgroupby.php

  3. #3
    SitePoint Member
    Join Date
    Jan 2007
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Great tutorial pages! But I haven't found ehat I am looking For.

    Let me try again: If my search results give me PY(1), PY(3) and PY(4) and each one Thas multiple "Title"'s, The Code I Posted would give results like the following:

    PY(1) Title(1)
    PY(1) Title(2)
    PY(3) Title(3)
    PY(3) Title(4)
    PY(3) Title(5)
    PY(4) Title(6)

    What I want is:

    PY(1) Title(1) Title(2)
    PY(3) Title(3) Title(4) Title(5)
    PY(4) Title(6)

    I want the Titles grouped with only one PY.

    Did I explain this any better this time?

  4. #4
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    What you are looking for is the category/ subcategory model. I cant remeber off the top of my head what the code is but if you search this forum you should turn up a few threads that should help
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  5. #5
    SitePoint Member
    Join Date
    Jan 2007
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I found this by searching for Catagory/Subcatagory, as you suggested: http://www.sitepoint.com/forums/show...ry+subcategory

    This should work, I'll give it a try tomorrow. Thanks

  6. #6
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    It does but putting a query in a while loop from another query isn't really ideal.
    If I get chance tomorrow I will write something up for you.

    Cheers

    Spike
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  7. #7
    SitePoint Member
    Join Date
    Jan 2007
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ""It does but putting a query in a while loop from another query isn't really ideal."

    I've discovered that also. If I have have search by "Title" (in my example) in my first query, I end up with duplicate reply's for the number of title's that each "PY" brings up. If I move the code for the search by title in the second query, I only get one response per "PY", but it takes 20-30 seconds.

    If you would like, I can put the actual code that I am using. I haven so far, since I have multiple tables with multiple fields, and have wanted to keep it simple.

    -Yerac

    SOLVED DUPLICATE PROBLEM -Just found out about the "Group By" command. So now everything works. But if there is a better way, as you suggested, I am still interested.
    Last edited by Yerac; Feb 4, 2007 at 15:26. Reason: I don't want people kind enough to help me to be spinning there wheels


Bookmarks

Posting Permissions

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