0

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?

Derfder
  • 3,204
  • 11
  • 50
  • 85
  • I have tried this $params = array( 'select' => 'winner_team, count(id) as wins', 'orderby' => 'wins DESC', 'groupby' => 'winner_team', 'limit' => 20 ); But there is an issue with Pods so it is not working. Therefore I would like to bypass it using arrays and then while loop the grouped and oredered array (if it is possible with arrays) itself again – Derfder Oct 11 '12 at 08:09
  • Not quite sure what you want to put into the array you create. Is it the contents of *$matches->field( 'team_1.name' )* and *$matches->field( 'team_2.name' )* or what? – inquam Oct 11 '12 at 08:10
  • @Derfder: That doesn't have anything to do with the actual question. What have you tried to solve the problem yourself? – Jon Oct 11 '12 at 08:11
  • all the 4 values (id, team_1, team_2, winner_team). I will edit my question and add this info with a simplified table schema – Derfder Oct 11 '12 at 08:12
  • @Jon wait a sec, I will add things to my question, thanks. – Derfder Oct 11 '12 at 08:13
  • I have edited my question. If something is still not clear, let my know I will try to add it there. Thanks in advance for any help. – Derfder Oct 11 '12 at 08:29
  • Edited once more to make it even more clearer ;). If it is still not enough, just tell I will try by best to add as much info as possible. – Derfder Oct 11 '12 at 08:42
  • So, do I need to use after while loop an foreach loop? – Derfder Oct 11 '12 at 09:10
  • OK, I have stored the values in an array called $matches_array[]. Now I need to groupby and oredr the values DESC. Any advice? – Derfder Oct 11 '12 at 09:17

1 Answers1

2

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.

Phil Lewis
  • 61
  • 4