[PHP] multidimensional array problems

[PHP] multidimensional array problems

Post by larr » Mon, 15 Jan 2007 12:51:08


t's better to just leave the record as an array and read it that way.

while ($row = mysql_fetch_assoc($result)) {
print "<tr><td>{$row['game']}</td> <td>{$row['type']}</td></tr>\n";
}

And so on. You're not actually dealing with a multi-dimensional array yet;
$result is an object from which you are extracting data records as
one-dimensional associative arrays.

On Saturday 13 January 2007 7:40 pm, nitrox doe wrote:

--
Larry Garfield AIM: LOLG42
XXXX@XXXXX.COM 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] multidimensional array problems

Post by larr » Mon, 15 Jan 2007 14:02:36

opying back to the list...

Actually, I'd suggest doing a PHP-side grouping. See this article for
details:

http://www.garfieldtech.com/blog/php-group-by

On Saturday 13 January 2007 10:50 pm, nitrox . wrote:

--
Larry Garfield AIM: LOLG42
XXXX@XXXXX.COM 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] multidimensional array problems

Post by list » Mon, 15 Jan 2007 15:00:06

ive this a go

<?php
//begin member league table
$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";
$memrosterresults = mysql_query($memroster) or die(mysql_error());

$currentType = FALSE;
echo "<p>\n";
while ( $row = mysql_fetch_assoc($memrosterresults) ) {
if ( $row['type'] != $currentType ) {
if ( $currentType !== FALSE ) {
echo "</p>\n";
echo "<p>\n";
}
echo " <h3>{$row['type']}</h3>\n";
$currentType = $row['type'];
}
echo "{$row['user_name']} - {$row['rank']} - {$row['country']} -
{$row['email']}<br />\n";
}
echo "</p>\n";
//end member league table
?>

This is untested, but it should give you the results you are looking for.

Jim Lucas

nitrox doe wrote:
 
 
 

[PHP] multidimensional array problems

Post by c3.nitro » Sun, 21 Jan 2007 07:33:32

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";
}
?>

_________________________________________________________________
Valentine Day -- Shop for gifts that spell L-O-V-E at MSN Shopping
http://www.yqcomputer.com/ ,ptnrid=37,ptnrdata=24095&tcode=wlmtagline
 
 
 

[PHP] multidimensional array problems

Post by larr » Sun, 21 Jan 2007 08:28:47

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.




--
Larry Garfield AIM: LOLG42
XXXX@XXXXX.COM 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