Reputation: 3324
Let's say I have this loop in my header.php:
<div id="top-20">
<h2 class="top-20-title">TOP 20 TEAMS</h2>
<div class="top-20-content">
<?php
$params = array(
'limit' => -1 // get everything
);
$matches = pods( 'matches', $params );
if ( 0 < $matches->total() ) {
while ( $matches->fetch() ) {
?>
<?php echo $matches->field( 'winner_team' ); ?>
<br>
<?php
} // end of while loop
} // end of if any exists
?>
</div><!-- /top-20-content -->
</div><!-- /top-20 -->
And this is what my simplified table looks like:
id | team_1 | team_2 | winner_team
--------------------------------
1 | 3 | 15 | 3
--------------------------------
2 | 20 | 8 | 8
--------------------------------
3 | 18 | 11 | 18
--------------------------------
4 | 8 | 7 | 8
--------------------------------
...
But I want instead of echoing inside while loop like
<?php echo $matches->field( 'winner_team' ); ?>
create a new array i.e. $matches_array which will contain 2 columns like id_new and winner_team_new and save the value of winner_team in winner_team_new instead of echoing it directly inside while loop.
Then I would like to loop this new created array $matches_array inside another while loop after groupby occurrences of winner_team_new and orderby them DESC.
So, I will get something like this:
8
3
18
instead of what I am currently getting when using echo in while loop:
3
8
18
8
Why I can not just do it inside sql? I need to be done using another array not using conditions in $params or sql array due to this bug: https://github.com/pods-framework/pods/issues/595 and the fact that pods framework is too complicated for my to make my own code adjustements into core so I would like to bypass this by working with an array that will be created from while loop.
So, what I need is to create an array and rearange the results based on occurrences of winner_team from the most to the less.
I have tried using
$matches_array = array_count_values(array_map($matches,array_keys($new_row)));
And then loop it in while loop but it is not working at all.
I need help with the count array/group by part.
I don't know practically anything about it.
I just guess that I need after grouping based on the same winner_team field create a new field e.g. wins where the number of occurrences for every team(based on winner_team) value will be stored. And then order that new array DESC.
Btw. all columns in the table are INTEGERS
EDIT 2: I have figured out how to store values from the while loop inside a new array called $matches_array and the code looks like this:
<div id="top-20">
<h2 class="top-20-title">TOP 20 TEAMS</h2>
<div class="top-20-content">
<?php
$params = array(
'limit' => -1 // get everything
);
$matches = pods( 'matches', $params );
if ( 0 < $matches->total() ) {
while ( $matches->fetch() ) {
?>
<?php // echo $matches->field( 'winner_team' ); ?>
<?php $row_array = $matches->field( 'winner_team' ); ?>
<?php $matches_array[]; ?>
<br>
<?php
} // end of while loop
} // end of if any exists
?>
</div><!-- /top-20-content -->
</div><!-- /top-20 -->
Now, I need probably do a foreach loop after a groupby(based on occureces of the numbers in winner_team field) and order the result before the output DESC.
Any advice how to do that?
Upvotes: 0
Views: 1116
Reputation: 61
The issue that initially kept you from sorting by an aliased column should be resolved. I've tested it with the initial scenario and it's doing exactly what it should. I think you can happily discard this attempted work-around.
Upvotes: 2