Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php / SQL - print many word 4 times between every 4 names with many conditions

Tags:

php

mysql

i have asked this question before here in this topic please read it carefully sql-print-many-word-between-every-columns-with-many-conditions

but the difference between this question and previous one that i want to make a collective players or group of players inside many club names for example :

El Ahly club name have a 12 players i want to split it into 3 teams every team has 4 players what i want to count how many team inside every club names then put them inside immutable numbers (groups) 4 8 16 32 64 128 256 based on the number of teams print **ANY WORD** between them something like that after filter data with select full_name,club_name from players where y_of_birth=2000 the desired output should be like that :

UPDATED :

+----+------+---------------------+
|   full_name  |  club_name       |
+--------------+------------------+
| Ahmed Sayed  |   El Ahly        |
+--------------+------------------+
| Kareem Gaber |   El Ahly        |
+--------------+------------------+
| Maher Zein   |   El Ahly        |
+--------------+------------------+
| Mohab Saeed  |   El Ahly        |
+----+------+--+------------------+
| ahmed shawky |   Petroget       |
+--------------+------------------+
| shaker ali   |   Petroget       |
+--------------+------------------+
| omar Saed    |   Petroget       |
+--------------+------------------+
| moataz Gamal |   Petroget       |
+------+-------+------------------+
| **ANY WORD** |                  |
+--------------+------------------+
| **ANY WORD** |                  |
+--------------+------------------+
| **ANY WORD** |                  |
+--------------+------------------+
| **ANY WORD** |                  |
+--------------+------------------+
| husein mazen |   Cocorico       |
+--------------+------------------+
| hassan maged |   Cocorico       |
+--------------+------------------+
| maged ahmoud |   Cocorico       |
+--------------+------------------+
| zakareya gad |   Cocorico       |
+--------------+------------------+
| Kamal saber  |   El Ahly        |
+--------------+------------------+
| gamel kamel  |   El Ahly        |
+--------------+------------------+
| omar galal   |   El Ahly        |
+--------------+------------------+
| Kamal saber  |   El Ahly        |
+--------------+------------------+
| **ANY WORD** |                  |
+--------------+------------------+
| **ANY WORD** |                  |
+--------------+------------------+
| **ANY WORD** |                  |
+--------------+------------------+
| **ANY WORD** |                  |
+--------------+------------------+
| khaled zein  |   Petroget       |
+--------------+------------------+
| shady sameer |   Petroget       |
+--------------+------------------+
| wahed mahmoud|   Petroget       |
+--------------+------------------+
| samy shahen  |   Petroget       |
+----+------+--+------------------+
| Mohamed gad  |   El Ahly        |
+----+------+--+------------------+
| ehab zeyad   |   El Ahly        |
+----+---------+------------------+
| moaz maged   |   El Ahly        |
+--------------+------------------+
| mazen mahmod |   El Ahly        |
+--------------+------------------+

in this example there are 2 groups of **ANY WORD** because its a 6 group and put them inside 8 immutable group which found in 4 or 8 or 16 or 32 or 64 or 128 or 256 numbers ... and no adjacent between 2 **ANY WORD** or two adjacent between club_name

Examples :

4 players as a one player

if the result after select full_name,club_name from players where y_of_birth=2000 is 12 players it mean 12 players = 3 groups because every group have 4 players next step print **ANY WORD** 4 times (note: **ANY WORD** is a one of these groups) between 3 groups so the output will be like that :

+----+------+---------------------+
|   full_name  |  club_name       |
+--------------+------------------+
| Ahmed Sayed  |   El Ahly        |
+--------------+------------------+
| Kareem Gaber |   El Ahly        |
+--------------+------------------+
| Maher Zein   |   El Ahly        |
+--------------+------------------+
| Mohab Saeed  |   El Ahly        |
+----+------+--+------------------+
| ahmed shawky |   Petroget       |
+--------------+------------------+
| shaker ali   |   Petroget       |
+--------------+------------------+
| omar Saed    |   Petroget       |
+--------------+------------------+
| moataz Gamal |   Petroget       |
+------+-------+------------------+
| **ANY WORD** |                  |
+--------------+------------------+
| **ANY WORD** |                  |
+--------------+------------------+
| **ANY WORD** |                  |
+--------------+------------------+
| **ANY WORD** |                  |
+--------------+------------------+
| Kamal saber  |   El Ahly        |
+--------------+------------------+
| gamel kamel  |   El Ahly        |
+--------------+------------------+
| omar galal   |   El Ahly        |
+--------------+------------------+
| Kamal saber  |   El Ahly        |
+--------------+------------------+

Conclusion :

4 8 16 32 64 128 256 are immutable groups

group 4 should fill of 16 players with **ANY WORD** with no adjacent between them something like that :

+----+------+---------------------+
|   full_name  |  club_name       |
+--------------+------------------+
| Ahmed Sayed  |   El Ahly        |
+--------------+------------------+
| Kareem Gaber |   El Ahly        |
+--------------+------------------+
| Maher Zein   |   El Ahly        |
+--------------+------------------+
| Mohab Saeed  |   El Ahly        |
+----+------+--+------------------+
| ahmed shawky |   Petroget       |
+--------------+------------------+
| shaker ali   |   Petroget       |
+--------------+------------------+
| omar Saed    |   Petroget       |
+--------------+------------------+
| moataz Gamal |   Petroget       |
+------+-------+------------------+
| **ANY WORD** |                  |
+--------------+------------------+
| **ANY WORD** |                  |
+--------------+------------------+
| **ANY WORD** |                  |
+--------------+------------------+
| **ANY WORD** |                  |
+--------------+------------------+
| Kamal saber  |   El Ahly        |
+--------------+------------------+
| gamel kamel  |   El Ahly        |
+--------------+------------------+
| omar galal   |   El Ahly        |
+--------------+------------------+
| Kamal saber  |   El Ahly        |
+--------------+------------------+

group 8 should fill of 32 players with **ANY WORD** with no adjacent between them something like that :

+----+------+---------------------+
|   full_name  |  club_name       |
+--------------+------------------+
| Ahmed Sayed  |   El Ahly        |
+--------------+------------------+
| Kareem Gaber |   El Ahly        |
+--------------+------------------+
| Maher Zein   |   El Ahly        |
+--------------+------------------+
| Mohab Saeed  |   El Ahly        |
+----+------+--+------------------+
| ahmed shawky |   Petroget       |
+--------------+------------------+
| shaker ali   |   Petroget       |
+--------------+------------------+
| omar Saed    |   Petroget       |
+--------------+------------------+
| moataz Gamal |   Petroget       |
+------+-------+------------------+
| **ANY WORD** |                  |
+--------------+------------------+
| **ANY WORD** |                  |
+--------------+------------------+
| **ANY WORD** |                  |
+--------------+------------------+
| **ANY WORD** |                  |
+--------------+------------------+
| husein mazen |   Cocorico       |
+--------------+------------------+
| hassan maged |   Cocorico       |
+--------------+------------------+
| maged ahmoud |   Cocorico       |
+--------------+------------------+
| zakareya gad |   Cocorico       |
+--------------+------------------+
| Kamal saber  |   El Ahly        |
+--------------+------------------+
| gamel kamel  |   El Ahly        |
+--------------+------------------+
| omar galal   |   El Ahly        |
+--------------+------------------+
| Kamal saber  |   El Ahly        |
+--------------+------------------+
| **ANY WORD** |                  |
+--------------+------------------+
| **ANY WORD** |                  |
+--------------+------------------+
| **ANY WORD** |                  |
+--------------+------------------+
| **ANY WORD** |                  |
+--------------+------------------+
| khaled zein  |   Petroget       |
+--------------+------------------+
| shady sameer |   Petroget       |
+--------------+------------------+
| wahed mahmoud|   Petroget       |
+--------------+------------------+
| samy shahen  |   Petroget       |
+----+------+--+------------------+
| Mohamed gad  |   El Ahly        |
+----+------+--+------------------+
| ehab zeyad   |   El Ahly        |
+----+---------+------------------+
| moaz maged   |   El Ahly        |
+--------------+------------------+
| mazen mahmod |   El Ahly        |
+--------------+------------------+

and so on with

group 16 group 32 group 64 group 128 group 256

EDITED:

i have got this query from Gordon Linoff

select p.*
from (select p.*,
             (@rn := if(@c = club_name, @rn + 1,
                        if(@c := club_name, 1, 1)
                       )
             ) as seqnum
      from players p cross join
           (select @c := '', @rn := 0) params
      order by club_name
     ) p
order by floor((seqnum - 1) / 4), club_name

after combined with data filtered :

select p.*
from (select p.full_name,club_name,
             (@rn := if(@c = club_name, @rn + 1,
                        if(@c := club_name, 1, 1)
                       )
             ) as seqnum
      from players p  cross join
           (select @c := '', @rn := 0) params  WHERE `y_of_birth` LIKE N'%2000%'

      order by club_name
     ) p 

order by floor((seqnum - 1) / 4), club_name

output something like that :

+----+------+---------------------+
|   full_name  |  club_name       |
+--------------+------------------+
| Ahmed Sayed  |   El Ahly        |
+--------------+------------------+
| Kareem Gaber |   El Ahly        |
+--------------+------------------+
| Maher Zein   |   El Ahly        |
+--------------+------------------+
| Mohab Saeed  |   El Ahly        |
+----+------+--+------------------+
| kamal mado   |   Cocorico       |
+--------------+------------------+
| hossam magdy |   Cocorico       |
+--------------+------------------+
| sayed ammar  |   Cocorico       |
+--------------+------------------+
| karem gad    |   Cocorico       |
+--------------+------------------+
| Mohamed gad  |   El Ahly        |
+----+------+--+------------------+
| ehab zeyad   |   El Ahly        |
+----+---------+------------------+
| moaz maged   |   El Ahly        |
+--------------+------------------+
| mazen mahmod |   El Ahly        |
+--------------+------------------+
| shaker ali   |   Petroget       |
+--------------+------------------+
| omar Saed    |   Petroget       |
+--------------+------------------+
| moataz Gamal |   Petroget       |
+--------------+------------------+
| gamael zayed |   Petroget       |
+--------------+------------------+
| husein mazen |   Cocorico       |
+--------------+------------------+
| mazen kamal  |   Cocorico       |
+--------------+------------------+
| maged ahmoud |   Cocorico       |
+--------------+------------------+
| mesho kareem |   Cocorico       |
+--------------+------------------+
| mano mazen   |   Petroget       |
+--------------+------------------+
| shady srour  |   Petroget       |
+--------------+------------------+
| mamdouh gamal|   Petroget       |
+--------------+------------------+
| khaled zein  |   Petroget       |
+--------------+------------------+
| husein mazen |   Cocorico       |
+--------------+------------------+
| hassan maged |   Cocorico       |
+--------------+------------------+
| maged ahmoud |   Cocorico       |
+--------------+------------------+
| zakareya gad |   Cocorico       |
+--------------+------------------+
| shady sameer |   Petroget       |
+--------------+------------------+
| wahed mahmoud|   Petroget       |
+--------------+------------------+
| samy shahen  |   Petroget       |
+----+------+--+------------------+
| abdo samy    |   Petroget       |
+----+------+--+------------------+

any help would be appreciated !

like image 391
auth private Avatar asked Jul 22 '15 21:07

auth private


1 Answers

I read your previous post and this one. If I get the idea, the basic thing is you have teams and you have players and you want to display data in certain standardized groupings (so-to-speak).

Well, there is a constant datum that you are always comparing to which is the size of the team.

What I would do is make a subquery that groups the team with a count of the players and use this in some fashion as follows:

select t.*
       , x.total_players
       -- , row_number() over(order by t.team_name) row_id
from team_table t
    join (
          select team_id
                , count(1) total_playerss
          from team_table 
          group by team_id
         ) x on x.team_id = t.team_id
order by t.team_name

When echo'ing out the result set in PHP, you can build some function such as:

<?php

function add_some_word($playerCount) {

if ($playerCount >4 && $playerCount <= 8) {
echo '*** SOME WORD***';
echo '*** SOME WORD***';
echo '*** SOME WORD***';
}
else if ($playerCount >4 && $playerCount <= 8) {
echo '*** SOME WORD***';
echo '*** SOME WORD***';
}
// etc.
}

?>

Using the row_id from the result set, you can see when a team starts and ends.

So when it is spitting out the rows it could be something like:"

<?php

$currentTeamName = "";

while ($row = pg_fetch_array($result,null,PGSQL_ASSOC)) {
// echo data you would normally send out

if ($currentTeamName != $row['team_name']) {
add_some_word($row['total_players']);
}

}

?>

I hope I got your question right. I am dashing this off fast but I thought it would help!

like image 174
Walker Farrow Avatar answered Nov 15 '22 21:11

Walker Farrow