Jump to content

Need help grouping results


elis

Recommended Posts

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.

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.