Current location: Hot Scripts Forums » Programming Languages » PHP » Show query results by in a different way


Show query results by in a different way

Reply
  #1 (permalink)  
Old 11-08-03, 03:52 PM
mdhall's Avatar
mdhall mdhall is offline
Aspiring Coder
 
Join Date: Oct 2003
Posts: 510
Thanks: 1
Thanked 1 Time in 1 Post
Show query results by in a different way

I'm helping a friend set up a result query, and want the results displayed a certain way. There is a table which contains manufactures and their products. Each entry/row has id, mfg, longproductname, shortname, etc. As it is, this query (intentionally) only brings up the manufacturer name and product name. The product name will link to another query page (passing the row "id" through an anchor tag) which brings up the entire row of that product. Fine, no problems there.
Where I'm stuck is on the first query. Currently, the query brings up id, mfg, and shortname, and displays as such...

Manufacturer "A" product 1
Manufacturer "A" product 2
Manufacturer "A" product 3
Manufacturer "B" product 1
Manufacturer "B" product 2

Ideally, we want the display this way...

Manufacturer "A"
product 1
product 2
product 3

Manufacturer "B"
product 1
product 2

etc., etc.

I cant figure out how to display the mfg name once, then loop thru and list all of the products for that mfg, then list the next mfg name, then their products, and so on. Would each manufacturer need to have their own table to do this, or is there a way with the current set up?
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 11-08-03, 05:03 PM
wheezy360's Avatar
wheezy360 wheezy360 is offline
Newbie Coder
 
Join Date: Nov 2003
Location: Toronto, ON
Posts: 64
Thanks: 0
Thanked 0 Times in 0 Posts
I'm not sure about the scope of your project here but it might be more beneficial to store the manufacturer's info in one table, and the products in another table and give the product table a field called mfg_id which is a foreign key to the manufacturer's table.. From then you could have two queries:

PHP Code:

<?

/* include your db connection crap */

$sql "SELECT * FROM manufacturers";
$mfg_result = @mysql_query($sql$connection) or die("Couldn't execute query: " .mysql_error());
while(
$mfg mysql_fetch_array($mfg_result)){
   echo 
$mfg['name'];
   
$sql "SELECT * FROM products WHERE mfg_id=\"" $mfg['id'] . "\"";
   
$product_result = @mysql_query($sql$connection) or die("Couldn't execute query: " mysql_error());
   while(
$product mysql_fetch_array($product_result)){
      echo 
$product['name'];
   }
}
?>
?>
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 11-08-03, 06:07 PM
mdhall's Avatar
mdhall mdhall is offline
Aspiring Coder
 
Join Date: Oct 2003
Posts: 510
Thanks: 1
Thanked 1 Time in 1 Post
OK. thnx, I'll see what she wants to do as far as changing table data and so forth. I knew it needed some type of double query but couldnt quite nail it down.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 11-09-03, 11:10 AM
Stefan's Avatar
Stefan Stefan is offline
Junior Code Guru
 
Join Date: Jun 2003
Location: Utrecht, The Netherlands
Posts: 599
Thanks: 0
Thanked 0 Times in 0 Posts
you might want to look into the use of mysql_data_seek() to make the script slightly more efficient. that way, you can get all your data with one query, and run through the results twice
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 11-09-03, 12:18 PM
mdhall's Avatar
mdhall mdhall is offline
Aspiring Coder
 
Join Date: Oct 2003
Posts: 510
Thanks: 1
Thanked 1 Time in 1 Post
I did some searcing on this, but didn't really find an example of how to set it up for my needs. Someone suggested using this...

select all from mfg
foreach mfg record
print mfg record
select all from items where mfg_mfgid = item_mfgid
foreach item record
print item record
endforeach
endforeach


But I havent found any examples of setting this up either.
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
MOHAA Server Query - ASP Stealth Website Reviews 1 11-27-04 11:38 AM
Emailing from query results Aaronn PHP 1 11-09-03 12:45 AM
Make a query that makes a field a link to the entire row mdhall PHP 8 10-09-03 07:18 PM
query results in multiple columns SnowCrash PHP 2 10-09-03 04:08 AM
Using CDONTS to email Checkbox results AkaMadDiSk ASP 1 07-23-03 06:32 PM


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