grouping in a table

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I have records from a database that are extracted with php and displayed in
a table.
Data in some of the fields is replicated


i wish to tidy this up and make it more readable by putting repeated fields
as a header row or something along those lines



I think I can do it using about 3 sql statements two of which use grouping,
but I would like to know if there is a more elegant solution using the one
sql and somehow doing the grouping in the php.

Anyone done anything similar, or seen an example out there?

Thanks Ian

Re: grouping in a table

mantrid wrote:
Quoted text here. Click to load it

why not do the grouping in sql. It might just be simpler abd quicker

Re: grouping in a table

Quoted text here. Click to load it

 For each field that you're grouping by in your display, keep a variable for
the last seen value. When it changes, print a header row, and reset the
variable. So for example here, you'd have two variables keeping track of the
"somethingX" and "itemX" fields.

 When you see a change in the first-level heading also reset the second-level
variable (to empty string or something that won't appear in the data itself),
so you get a heading row for "something2/item1".

 Your SQL and data remains unchanged, it's just a matter of how you display it.

Andy Hassall :: :: :: disk and FTP usage analysis tool

Re: grouping in a table

Andy Hassall wrote:
Quoted text here. Click to load it
I would prefer to use associative arrays and array_key_exists. Something  

$root = array();

while ($record = /*retrieve a record from DB */) {
   $fields = /*fn. to split record into an array of fields*/;
   $current = &$root;
   foreach ($fields as $field) {
     if (!array_key_exists($field, $current) {
       $current[$field] = array();
     $current = &$current[$field];

This now has your entire data in a hierarchical array structure, and
it works however many fields there are in a record. (The last level
will be empty arrays, but that shouldn't be a problem).


Re: grouping in a table

Quoted text here. Click to load it

Is xml out of the question? There should be lots of examples out there
of doing this kind of thing in xml.

Site Timeline