• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

How do I organize table rows as parent child relationship with a for loop and if statement

 
Ranch Hand
Posts: 31
Android Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a table with a PK, ID, ProductGroup

"1","10204","Regular"
"2","10203","Regular"
"3","10332","Regular"
"4","10201","Bold"
"5","10211","Bold"
"6","10130","New"
"7","10290","New"
"8","10126","New"
"9","10116","New"


I can obtain each row in a for loop, but how do I implement the logic to structure the data as:

Regular
10204
10203
10332
Bold
10201
10211
New
10130
10290
10126
10116


I've tried the following with the loop:



But I end up only getting the last two labels, if I set the header variable before the if statement inside or outside the for loop I end up printing out the header for each row.
 
Sheriff
Posts: 3042
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you initialize "header" to be before the loop starts? If you initialize to the value of the first header, then you'd definitely get the results you're seeing.
 
Todd Patrick
Ranch Hand
Posts: 31
Android Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've tried to initialize the "header" with the first header, which didn't work and I've tried it with an empty String.
 
Marshal
Posts: 27366
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I find it a bit curious that you declare the "header" variable there in line 5. Perhaps you're assigning an initial value to some other "header" variable in the start of the loop, before that declaration?
 
Todd Patrick
Ranch Hand
Posts: 31
Android Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What I ended up doing is running a SQL statement that returned the distinct header values and looped through those values and then pass the header value to the inside loop which allowed me to output the example above.

I appreciate the help!

Thanks.
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So if you have N productGroups you're doing N+1 queries while it could be done in 1 query. That isn't a great solution.
 
Marshal
Posts: 76397
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are we discussing how to get a query to return results grouped and ordered by headers in SQL, or how to order them in Java™?
 
Todd Patrick
Ranch Hand
Posts: 31
Android Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Wouter Oet wrote:So if you have N productGroups you're doing N+1 queries while it could be done in 1 query. That isn't a great solution.



Completely agree, but I had to keep moving forward and I couldn't figure this out.
 
Todd Patrick
Ranch Hand
Posts: 31
Android Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Are we discussing how to get a query to return results grouped and ordered by headers in SQL, or how to order them in Java™?



The query works fine, I believe. I just need to use the correct logic in Java to output what I've described above. This is on a mobile device, so I'd like to remove the extra query if at all possible.
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your original solution was in the good direction:
 
Have you no shame? Have you no decency? Have you no tiny ad?
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic