Current location: Hot Scripts Forums » Other Discussions » Database » Query -> While Loop -> Stopping Conditions


Query -> While Loop -> Stopping Conditions

Reply
  #1 (permalink)  
Old 05-22-09, 03:29 PM
protocode protocode is offline
Newbie Coder
 
Join Date: May 2009
Posts: 14
Thanks: 1
Thanked 0 Times in 0 Posts
Query -> While Loop -> Stopping Conditions

The page I'm writing has a query that pulls from three tables. That part is done correctly. Now, I need to list the queried info in a table, so I used a while loop.

For the ease of the reader I wanted to set the table up in a very specific manner, and that's where the problem comes in.

PHP Code:

require('./Connections/db.php');


$pull mysql_query("SELECT * FROM table1, table2, table3 WHERE table1.field1 = table2.field1_2 AND table2.field3 = table3.field2_4 AND table1.field5 = '$var1' ORDER BY table3.field6, table3.field7, table3.field8");

while (
$stored mysql_fetch_array$pull ); 
All of the desired information is pulled and ordered correctly, but I'm looking to make the display/layout of said info in a hierarchical format. Meaning I need to show the value of table3.field6 once and all of the info therefrom continuously until it is finished, then move onto the next value for table3.field6.

Visually the information should be like this

TABLE3.FIELD6_1ST VALUE
OUTPUT_1ST VALUE_ROW1_INFO_A
OUTPUT_1ST VALUE_ROW2_INFO_B
ETC...

TABLE3.FIELD6_2ND VALUE
OUTPUT_2ND VALUE_ROW3_INFO_A
OUTPUT_2ND VALUE_ROW4_INFO_B
ETC...

What I get right now is

TABLE3.FIELD6_1ST VALUE OUTPUT_1ST VALUE_ROW1_INFO_A
TABLE3.FIELD6_1ST VALUE OUTPUT_1ST VALUE_ROW2_INFO_B
TABLE3.FIELD6_2ND VALUE OUTPUT_2ND VALUE_ROW3_INFO_A
TABLE3.FIELD6_2ND VALUE OUTPUT_2ND VALUE_ROW4_INFO_B

It displays the value of Table3.field6 with each row output. Should I be using a 'for' statement or stick with 'while' or a combination of the two?

Thank you in advance.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 05-23-09, 05:05 AM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
have you tried adding the DISTINCT modifier to your query? like this:
Code:
SELECT DISTINCT table3.field6, * FROM table1, table2, table3 WHERE table1.field1 = table2.field1_2 AND table2.field3 = table3.field2_4 AND table1.field5 = '$var1' ORDER BY table3.field6, table3.field7, table3.field8
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 05-24-09, 03:27 PM
Pat Phelan Pat Phelan is offline
Newbie Coder
 
Join Date: Apr 2009
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
What you are trying to do is called group supression, and it is very difficult to do at the database level compared to doing it at the application level. The simple answer is to hold a copy of the column(s) that you want to suprress, and only echo the values when they are different than in the previous row.

-PatP
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Iterating Associative arrays with foreach divya PHP 13 11-21-07 02:12 PM
How to do SQL query in a ASP while loop superman2004 ASP 2 09-09-04 04:35 AM
PHP > MSQL - Loop date system dihan PHP 0 06-12-04 06:11 PM
Declared Functions skipper23 PHP 4 12-17-03 11:06 AM
index page not showing up skipper23 PHP 3 12-15-03 02:10 PM


All times are GMT -5. The time now is 09:21 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.