Re: multidimensional array problems

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



It's actually quite simple.  You simply add another layer of grouping.  
General case:

$result = mysql_query("SELECT a, b, c, d FROM foo ORDER BY a, b, c");
while ($record = mysql_fetch_object($result)) {
  $roster[$record->a][$record->b][] = $record;
}

ksort($roster);

foreach ($roster as $a => $bfield) {
  print $a;
  ksort($bfield);
  foreach ($bfield as $b => $record) {
    print "$a: $b";
    print_r($record);
  }
}

Add real output syntax to taste.


On Friday 19 January 2007 4:33 pm, nitrox . wrote:
> Ive followed your example on grouping. Im still trying to understand all of
> the code but ive made great progess on this with your example. Now I have
> one last issue and this will be solved. Ill remind here what Im trying to
> achieve
> I have a table for leagues, lookup table and team roster. There can be
> multiple game types for each game i.e. CoD2 - CTF, CoD2 - S&D, CoD2 - TDM.
> If a member is playing CoD2 CTF and CoD2 TDM there should be a table for
> each game and type showing each member playing that game/type. If a member
> is signed up for multiple games/types  he/she should have a name listed
> under each game/type.
>
> Right now my php script is only sorting by game which is putting the same
> person in for each instance of the game instead of sorting through each
> game and then type. So here is my code so far and any help is greatly
> appreciated.
>
> <?php
> include ("db.php");
>
> $memroster = "SELECT inf_league.game, inf_league.type,
> inf_member.user_name, inf_member.rank, " .
>             "inf_member.country, inf_member.email " .
>             "FROM inf_league " .
>             "INNER JOIN inf_memberleague ON inf_league.gid =
> inf_memberleague.l_id " .
>             "INNER JOIN inf_member ON inf_member.user_id =
> inf_memberleague.m_id";
> $roster = array();
> $memrosterresults = mysql_query($memroster)
> or die(mysql_error());
> while ($record = mysql_fetch_object($memrosterresults)) {
> $roster[$record->game][] = $record;
> }
> ksort($roster);
> foreach ($roster as $game => $records) {
>    print "<table>\n";
>    print "<caption>{$game}</caption>\n";
>    print "<th>Name</th> <th>Rank</th> <th>Country</th> <th>Email</th>\n";
>    foreach ($records as $record) {
>      print "<tr>\n";
>      print "<td>{$record->user_name}</td>\n";
>   print "<td>{$record->rank}</td>\n";
>   print "<td>{$record->country}</td>\n";
>   print "<td>{$record->email}</td>\n";
>   print "</tr>\n";
>     }
> print "</table>\n";
> }
> ?>

-- 
Larry Garfield			AIM: LOLG42
larry@xxxxxxxxxxxxxxxx		ICQ: 6817012

"If nature has made any one thing less susceptible than all others of 
exclusive property, it is the action of the thinking power called an idea, 
which an individual may exclusively possess as long as he keeps it to 
himself; but the moment it is divulged, it forces itself into the possession 
of every one, and the receiver cannot dispossess himself of it."  -- Thomas 
Jefferson

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux