elis Posted June 23, 2009 Share Posted June 23, 2009 I'm trying to group results pulled from a query based on a value: i.e. name | value red | 3 green | 4 blue | 3 yellow | 2 orange | 4 so that when it prints, it would appear as: red | 3 blue | 3 green | 4 orange | 4 yellow | 2 --- I've tried using GROUP BY, but that printed only one result of each. I've tried using sort, but I need to separate the groups - not just have them sorted. I've tried storing the different groups in arrays, but that quickly became overly complicated. Here is my code: <?php $qryA = "SELECT itwsnode.*, itwscontent_type_rankings.* FROM itwsnode, itwscontent_type_rankings WHERE itwsnode.nid = itwscontent_type_rankings.nid AND itwsnode.type = 'rankings' AND itwscontent_type_rankings.field_associated_industry_value != 0"; $resultA = mysql_query($qryA) or die(mysql_error()); $counterA = 1; while($rowA = mysql_fetch_array($resultA)) { ?> <td class="block"> <b><a href="#"><?php echo $rowA['title']; ?></a></b> </td> <?php if($counterA % 2 == 0) { echo '<tr>'; } ?> <?php $counterA++; } ?> I'm trying to group the same values from "field_associated_industry_value" and have them print in the same <td class="block">. And different values in a new <td class="block"> As of now, everything is printing in its own table data cell. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted June 23, 2009 Share Posted June 23, 2009 To get the result set with the same values together, use the following (with your actual column that was represented by value in your example) - ORDER BY value If you want the names to be alphabetical within each value, use the following (with your actual columns that were represented by value and name in your example) - ORDER BY value, name Quote Link to comment Share on other sites More sharing options...
elis Posted June 23, 2009 Author Share Posted June 23, 2009 No, I've tried that already. I'm able to sort everything, I just can't figure out how to group each result based on the value and break the entire query up according to those group. i.e. everything with value 1 in its own table cell, everything with value 2 in its own cell, everything with value 3 in its own cell. I thought GROUP BY would do that, but it was well off too. Quote Link to comment Share on other sites More sharing options...
J.Daniels Posted June 23, 2009 Share Posted June 23, 2009 Are you trying to sort by the name, value, or a combination of both? I don't see a logical sort to your desired output. Quote Link to comment Share on other sites More sharing options...
elis Posted June 24, 2009 Author Share Posted June 24, 2009 I'm trying to sort only by value, but I want to be able to manipulate (I suppose with PHP) how the results are displayed/grouped. I'm trying to group anything that has the same value into its own group and anything with a different value in a different group. I'm able to sort everything, I just can't actually differentiate what should be in which group. That's the part I'm stuck on. Quote Link to comment Share on other sites More sharing options...
elis Posted June 24, 2009 Author Share Posted June 24, 2009 Or, is there way to check whether a field in one row is the same as a field in the following or previous row? If that's possible, I think I could work what I'm trying to do with that. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted June 24, 2009 Share Posted June 24, 2009 is there way to check whether a field in one row is the same as a field in the following or previous row? Of course there is, that is the whole point of programming, writing code to test and manipulate data to produce the desired results. pseudo code - <?php $last_value = ''; // variable to 'remember' the last value being tested (initialize to a value that will never exist as a data value) while($rowA = mysql_fetch_array($resultA)) { // test if there is a change in the column value you are using to trigger your special processing if($rowA['value'] !== $last_value){ // there is a change in the value, do any special processing - output a header, start a new table row... // update the $last_value to be the current value to that the next change in value will be detected $last_value = $rowA['value']; } // do your normal processing of the data here } // end of while loop ?> 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.