473,387 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

grouping in a table

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

eg
something1,item1,somethingelse1
something1,item1,somethingelse2
something1,item2,somethingelse3
something1,item2,somethingelse4
something2,item1,somethingelse5
something2,item1,somethingelse6
something2,item2,somethingelse7
something2,item2,somethingelse8

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

something1
item1
somethingelse1
somethingelse2
item2
somethingelse3
somethingelse4

something2
item1
somethingelse5
somethingelse6
item2
somethingelse7
somethingelse8

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

Oct 2 '06 #1
4 1551

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

eg
something1,item1,somethingelse1
something1,item1,somethingelse2
something1,item2,somethingelse3
something1,item2,somethingelse4
something2,item1,somethingelse5
something2,item1,somethingelse6
something2,item2,somethingelse7
something2,item2,somethingelse8

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

something1
item1
somethingelse1
somethingelse2
item2
somethingelse3
somethingelse4

something2
item1
somethingelse5
somethingelse6
item2
somethingelse7
somethingelse8

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
why not do the grouping in sql. It might just be simpler abd quicker

Oct 2 '06 #2
On Mon, 02 Oct 2006 20:07:47 GMT, "mantrid" <ia********@virgin.netwrote:
>I have records from a database that are extracted with php and displayed in
a table.
Data in some of the fields is replicated

eg
something1,item1,somethingelse1
something1,item1,somethingelse2
something1,item2,somethingelse3
something1,item2,somethingelse4
something2,item1,somethingelse5
something2,item1,somethingelse6
something2,item2,somethingelse7
something2,item2,somethingelse8

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

something1
item1
somethingelse1
somethingelse2
item2
somethingelse3
somethingelse4

something2
item1
somethingelse5
somethingelse6
item2
somethingelse7
somethingelse8

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.
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 :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Oct 2 '06 #3
Andy Hassall wrote:
On Mon, 02 Oct 2006 20:07:47 GMT, "mantrid" <ia********@virgin.netwrote:
>I have records from a database that are extracted with php and displayed in
a table.
Data in some of the fields is replicated

eg
something1,item1,somethingelse1
something1,item1,somethingelse2
something1,item2,somethingelse3
something1,item2,somethingelse4
something2,item1,somethingelse5
something2,item1,somethingelse6
something2,item2,somethingelse7
something2,item2,somethingelse8

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

something1
item1
somethingelse1
somethingelse2
item2
somethingelse3
somethingelse4

something2
item1
somethingelse5
somethingelse6
item2
somethingelse7
somethingelse8

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.

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.
I would prefer to use associative arrays and array_key_exists. Something
like:

$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).

Colin
Oct 3 '06 #4
Anyone done anything similar, or seen an example out there?
Is xml out of the question? There should be lots of examples out there
of doing this kind of thing in xml.

Oct 3 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Debbie Davis | last post by:
Hi there, SQL 2000 I have the following query: SELECT sponsor, COUNT(sponsor) * 2 AS total FROM Referrals GROUP BY sponsor Works great, returns the sponsor and the total * 2 of their...
3
by: Kevin Brown | last post by:
Is there anyway to generate this type of resulting HTML table from this XML using XSLT? Basically I need to be able to consult 2 trees of data to generate the HTML, but I have not been able to...
3
by: Graham | last post by:
Hi, I am having trouble getting XSL to count the members of a group. What I am trying to do is group by <objectid.Contactid> and count the number of <activityid>'s for each <objectid.contactid>....
0
by: Stephen | last post by:
I have the following xslt and I am trying to use two levels of grouping. I am first grouping on the report category, then on the report type. The problem is I am sure I have to apply some sort of...
1
by: Brian Coy | last post by:
I am creating a database to track scrap on a daily basis at my plant. I have to provide a weekly scrap report with the amount of each part scrapped per day. I have the basic database set up, and...
3
by: ahaque38 | last post by:
Hello. Using A2K SP3, I am having the following problem with a report using "Sorting and Grouping". I have recently added a grouping in the reports for "Category2<>'CONTRACTS'". I have...
8
by: Mike MacSween | last post by:
tblCourses one to many to tblEvents. A course may have an intro workshop (a type of event), a mid course workshop, a final exam. Or any combination. Or something different in the future. At...
0
by: mafandon | last post by:
This seems easy, but can't seem to get past it. Using VB 2005. In my report viewer for a windows forms app not using sql 2005 reporting services. I can get the grouping to work just fine and...
0
by: asearle | last post by:
Hi everyone, I have a XML file that I need to filter and group. Apparently XSL 2.0 offers some good grouping features but unfortunately (for reasons of compatibility) I think I need to stay with...
3
by: Jimmy | last post by:
Is there a way to sort/group a report based on the second column of a combo box, i.e. the text associated with the primary key number?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.