dual_alliance Posted January 16, 2007 Share Posted January 16, 2007 I'm just wondering how do l go about grouping simliar objects that are returned from a MySQL query.Example:Book x 8Chain x 9etc..I've looked on google for grouping things in PHP but l cant find anything.I have also thought about using the COUNT(*) query in MySQL, but does it work in while loops and count every single item in that loop?Thanks for your help. Quote Link to comment https://forums.phpfreaks.com/topic/34396-grouping-items-in-php/ Share on other sites More sharing options...
HuggieBear Posted January 16, 2007 Share Posted January 16, 2007 This all depends on your database structure, and what you're trying to return. This sounds as though you want to do your 'grouping' in MySQL before even getting to php.A few more db details might allow us to help further.RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/34396-grouping-items-in-php/#findComment-161880 Share on other sites More sharing options...
dual_alliance Posted January 16, 2007 Author Share Posted January 16, 2007 Here is a list of all the rows and what they do:i_unique_id <- make sure their is no multiple entriesi_id <- the item id, if you had a wooden stick they would all have an item id of 9. While a baseball bat would all have an id of 15.i_owner <- explains its self.i_cat <- catagory its ini_name <- explains its self.i_picture <- explains its self.i_for <- what type it is, e,g weapon, book, armour.i_c_price <- custom price is some one wants to sell iti_b_price <- price they buy it fori_s_price <- how much they can sell it for if they sell it back to the shopi_multiplyer <- if the item is a weapon how much damage it does. Quote Link to comment https://forums.phpfreaks.com/topic/34396-grouping-items-in-php/#findComment-161889 Share on other sites More sharing options...
HuggieBear Posted January 16, 2007 Share Posted January 16, 2007 OK, and what you want is a list of unique 'for' values e.g. weapon, book, armour and then a count of how many types of that item there are?RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/34396-grouping-items-in-php/#findComment-161893 Share on other sites More sharing options...
dual_alliance Posted January 16, 2007 Author Share Posted January 16, 2007 Yeah basically, l think after reading what you said l know how to do it. I'll post back if it's wrong or l solve it.dual_alliance Quote Link to comment https://forums.phpfreaks.com/topic/34396-grouping-items-in-php/#findComment-161903 Share on other sites More sharing options...
HuggieBear Posted January 16, 2007 Share Posted January 16, 2007 You need this then...SELECT for, count(for) as total FROM table_name GROUP BY forRegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/34396-grouping-items-in-php/#findComment-161908 Share on other sites More sharing options...
dual_alliance Posted January 16, 2007 Author Share Posted January 16, 2007 That is basically the same thing l came up with, thanks for your help HuggieBear.The code l used was:[code]$query = "SELECT `i_unique_id`, `i_name`, `i_s_price`, COUNT(i_id) FROM `mitems` WHERE `i_owner` = '$userid' AND `i_for` = '2' GROUP BY `i_name`";$result = mysql_query($query) or die("Problem with the query: $query<br>" . mysql_error());[/code]And the outcome:[img]http://img147.imageshack.us/img147/2725/here12te3.jpg[/img] Quote Link to comment https://forums.phpfreaks.com/topic/34396-grouping-items-in-php/#findComment-161912 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.