Results 1 to 10 of 10
  1. #1

    Displaying groups on a page without a lot of queries? Should be easy

    I just realized I do not think there is any php book that really explains how to do this. How can I sort the mysql results on a page but categorize them by groups and use html inbetween them? For example, if the mysql DB table contained items, group_name, type, how can I display it such as:

    (html and text here echos group name) Group 1
    item A
    item B
    item C

    (html and text here echos group name) Group 2
    item A
    item B
    item C
    I had a bud do something like this a few years ago and he used a ton of while statements and the page took about 45 seconds to load. When I look at other scripts they use two queries and one displays the group name and the other selects all of the items in alphabetical order that belong in that group. Could anyone provide me with a working example on how to do this? I actually managed to get close to getting this to work but the problem is that the html part is echoed on every line within the while loop and not just when it is the start of a new group. Thanks!

  2. #2
    Join Date
    Aug 2002
    Location
    Canada
    Posts
    665
    Problem description is slightly vague, but here's how I understand it:

    PHP Code:
    $res mysql_query"SELECT * FROM table ORDER BY groupcol"DB );
    $last_group null;
    while( 
    $row mysql_fetch_assoc$res ) ){
      if( 
    $row['groupcol'] != $last_group ){
         echo 
    "<h1>New group</h1>";
         
    $last_group $row['groupcol'];
      }

      
    var_dump$row );

    (adjust query, groupcol, etc)

    HTH
    Alex
    circlical - hosting software development
    forums * blog

  3. #3
    Thanks that was a fast reply Not sure what DB means in your query though I will try to integrate what you wrote into my script though.

  4. #4
    Join Date
    Aug 2002
    Location
    Canada
    Posts
    665
    second arg to MySQL query == database connection handle.

    Good luck!
    Alex
    circlical - hosting software development
    forums * blog

  5. #5
    A lot easier than I thought thanks a lot

  6. #6
    Sorry one last question. Is it possible to group two different types into one group? Because this is an online game, one "weapons" have types such as "WEAPON" and "CURSE". Can I place both WEAPONS and CURSE in the same Weapons group? I did use a switch but it displays two separate groups named weapons then since it is ordered by type DESC.

  7. #7
    Join Date
    Aug 2002
    Location
    Canada
    Posts
    665
    You'll need to do some preprocessing in this case, or adjust your query. Fundamentally though, as constructive criticism, these kinds of patterns are generally attributed to a lack of proper normalization at the database level.

    But to patch things up, if I understand still..you want items with groupcol of "CURSE" and "WEAPON" to be in the same group? You can do something like this then, barring any memory constraints:

    PHP Code:

    /**
     * Create key associations
     */
    function equiv$k ){
      if( 
    $k == "CURSE" )
          return 
    "WEAPON";
      return 
    $k;
    }

    $res mysql_query"SELECT * FROM table"DB );
    $dat = array();
    while( 
    $row mysql_fetch_assoc$res ) ){
      if( !isset( 
    $dat[equiv($row['groupcol'])] ) )
        
    $dat[equiv($row['groupcol'])] = array();

      
    array_push(equiv($row['groupcol']), $row );      
    }

    // you can sub-sort each member of $dat[] if you want here

    foreach( array_keys$dat ) as $group ){
      echo 
    "<h1>$group</h1>";

      foreach( 
    $dat[$group] as $row ){
        
    var_dump$row );
      }


    Untested, but should be fine, or close to fine
    circlical - hosting software development
    forums * blog

  8. #8
    Ok thanks and sorry for the delay in replying for some reason WHT stops sending me email notifications after a certain amount of posts or something. Hmm you bring up a good point perhaps I can modify the script and create a new "group" field or something and run a query to select all items within that group after I assign all of those items to that group. I will try that thanks again
    Last edited by lexington; 05-27-2008 at 12:38 AM.

  9. #9
    oops double post by accident.

  10. #10

    Mysql HEAP (MEMORY) overhead?

    (oops meant to create a new topic)

Posting Permissions

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