oddball25 Posted January 21, 2011 Share Posted January 21, 2011 Hi I am having some problems with my mysql database and php queries. I have a simple table ('database_table') that stores order details from my online gift shop. The table is set up as follows: id | order_code | item_code | item_description | name --------------------------------------------------------------------- 1 | 56743 | 56AAY | Fruit Bowl | Joe 2 | 56743 | 518VB | Aftershave | Joe 3 | 88932 | 11TQW | Red Mug | Mary etc... Each item (defined by item_code and item_description) within the order will be added to a separate row. On orders such as '56743' (Joe), there are two entries as they have ordered 2 items within the 1 order. Now my problem is that i want to set up a query that pulls order '56743' from the database and displays all the items within the order. I have the following PHP code for my query: $query="SELECT * FROM database_table WHERE order_code='56743'"; $result=mysql_query($query); mysql_close(); while($row = mysql_fetch_array($result)) { echo " <p>Your Order:</p> <p>Your Name: ".$row['name']."</p> <p>Your Order Code:".$row['order_code']."</p> <p>Your Item Codes:".$row['item_code']."</p> <p>Your Item Descriptions:".$row['item_description']."</p>"; } This outputs the following: Your Order: Your Name: Joe Your Order Code: 56743 Your Item Codes: 56AAY Your Item Descriptions: Fruit Bowl Your Order: Your Name: Joe Your Order Code: 56743 Your Item Codes: 518VB Your Item Descriptions: Aftershave But i only want it to output the 'item_code' and 'item_description' more than once if the order has several items, The 'name' and 'order_code' i only want to be displayed once (so as below:) Your Order: Your Name: Joe Your Order Code: 56743 Your Item Codes: 56AAY Your Item Codes: 518VB Your Item Descriptions: Fruit Bowl Your Item Descriptions: Aftershave I am quite new to mysql so apologies if this isn't explained to well and for the length of it! Thanks in advance for any help John Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted January 21, 2011 Share Posted January 21, 2011 See this link - http://www.phpfreaks.com/forums/mysql-help/need-some-array-help-bigtime!!!/msg1513324/#msg1513324 Obviously, you would need to change any usage of 'date' to 'order_code' to match what you are doing. The code will output the 'heading' information only once, but output each piece of data under that heading. Quote Link to comment Share on other sites More sharing options...
oddball25 Posted January 21, 2011 Author Share Posted January 21, 2011 Thanks for the link, have just used it in my code and it works perfectly thanks! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.