Go Back   Forum Care Forums > Development Reference Area > Php Development

Reply
 
LinkBack Thread Tools Display Modes
multidimensional array problems
Old
  (#1)
nitrox doe
Guest
 
Posts: n/a
Default multidimensional array problems - 05-14-2007, 03:34 AM

hi all,
im very new to php but i think i jumped on the
toughest thing to learn. Im trying to create a
team roster that will show game, game type
and league and then show each member based
on the game type. Ive worked out alot of code
but just cant figure where im going wrong. so
here is my code. Any pointers would be greatly
appreciated.

this is an example of what im trying to do
http://www.chalkthree.com/exampleroster.html

php code
<?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());
while ($row = mysql_fetch_array($memrosterresults)) {

foreach ($row as $game => $type) {
echo "<p>";
echo "$type";
foreach ($row as $type => $user_name) {
echo "$user_name" . " - " . "$rank" . " - " . "$country" . " - " . "$email";
}
print '</p>';
}
}
//end member league table
?>








mysql


CREATE TABLE `inf_league` ( `gid` int(11) NOT NULL auto_increment, `game`
varchar(255) NOT NULL, `type` varchar(255) NOT NULL, `league` varchar(255)
NOT NULL, `season` varchar(255) NOT NULL, PRIMARY KEY (`gid`))
TYPE=MyISAM AUTO_INCREMENT=4 ;-- -- Dumping data for table `inf_league`--
INSERT INTO `inf_league` (`gid`, `game`, `type`, `league`, `season`) VALUES
(1, 'DF:BHD', 'TKOTH', 'TWL', '2006 1st Quarter');INSERT INTO `inf_league`
(`gid`, `game`, `type`, `league`, `season`) VALUES (2, 'CoD2', 'CTF', 'TWL',
'2006 2nd QTR');INSERT INTO `inf_league` (`gid`, `game`, `type`, `league`,
`season`) VALUES (3, 'CoD2', 'Search & Destroy', 'CAL', '2006 4th QTR');--
---------------------------------------------------------- -- Table
structure for table
`inf_member`-- CREATE TABLE `inf_member` ( `user_id` int(11) NOT NULL
auto_increment, `user_level` int(2) NOT NULL default '0', `list_order`
int(3) NOT NULL default '0', `user_name` varchar(100) NOT NULL default '',
`p***word` varchar(25) NOT NULL default '', `email` varchar(100) NOT NULL
default '', `country` text NOT NULL, `game` text, `rank` varchar(40)
default NULL, `qoute` longtext, `config` int(1) default '0', `map`
varchar(100) default '', `gun` varchar(100) default '', `brand`
varchar(100) default '', `cpu` varchar(20) default '', `ram` varchar(20)
default '', `video` varchar(100) default '', `sound` varchar(100) default
'', `monitor` varchar(100) default '', `mouse` varchar(100) default '',
PRIMARY KEY (`user_id`)) TYPE=MyISAM AUTO_INCREMENT=3 ;--
-- Dumping data for table `inf_member`-- INSERT INTO `inf_member`
(`user_id`, `user_level`, `list_order`, `user_name`, `p***word`, `email`,
`country`, `game`, `rank`, `qoute`, `config`, `map`, `gun`, `brand`, `cpu`,
`ram`, `video`, `sound`, `monitor`, `mouse`) VALUES (1, 1, 0, 'nitrox',
'test', 'EMAIL REMOVED', 'United States', 'CoD2', 'Founder', NULL, 0, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);INSERT INTO `inf_member`
(`user_id`, `user_level`, `list_order`, `user_name`, `p***word`, `email`,
`country`, `game`, `rank`, `qoute`, `config`, `map`, `gun`, `brand`, `cpu`,
`ram`, `video`, `sound`, `monitor`, `mouse`) VALUES (2, 1, 1, 'raze',
'itsme', 'EMAIL REMOVED', 'United States', NULL, 'Leader', NULL, 0, '',
'', '', '', '', '', '', '', '');--
---------------------------------------------------------- -- Table
structure for table
`inf_memberleague`-- CREATE TABLE `inf_memberleague` ( `l_id` int(4) NOT
NULL, `m_id` int(4) NOT NULL) TYPE=MyISAM;-- -- Dumping data for table
`inf_memberleague`-- INSERT INTO `inf_memberleague` (`l_id`, `m_id`) VALUES
(1, 2);INSERT INTO `inf_memberleague` (`l_id`, `m_id`) VALUES (1, 1);INSERT
INTO `inf_memberleague` (`l_id`, `m_id`) VALUES (2, 1);INSERT INTO
`inf_memberleague` (`l_id`, `m_id`) VALUES (2, 2);

__________________________________________________ _______________
Get live scores and news about your team: Add the Live.com Football Page
http://www.live.com/?addtemplate=foo...T001MSN30A0701
   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Re: [PHP] multidimensional array problems
Old
  (#2)
Jim Lucas
Guest
 
Posts: n/a
Default Re: [PHP] multidimensional array problems - 05-14-2007, 03:34 AM

Give 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_***oc($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:
> hi all,
> im very new to php but i think i jumped on the
> toughest thing to learn. Im trying to create a
> team roster that will show game, game type
> and league and then show each member based
> on the game type. Ive worked out alot of code
> but just cant figure where im going wrong. so
> here is my code. Any pointers would be greatly
> appreciated.
>
> this is an example of what im trying to do
> http://www.chalkthree.com/exampleroster.html
>
> php code
> <?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());
> while ($row = mysql_fetch_array($memrosterresults)) {
>
> foreach ($row as $game => $type) {
> echo "<p>";
> echo "$type";
> foreach ($row as $type => $user_name) {
> echo "$user_name" . " - " . "$rank" . " - " . "$country" . " - " .
> "$email"; }
> print '</p>';
> }
> }
> //end member league table
> ?>
>
>
>
>
>
>
>
>
> mysql
>
>
> CREATE TABLE `inf_league` ( `gid` int(11) NOT NULL auto_increment,
> `game` varchar(255) NOT NULL, `type` varchar(255) NOT NULL, `league`
> varchar(255) NOT NULL, `season` varchar(255) NOT NULL, PRIMARY KEY
> (`gid`)) TYPE=MyISAM AUTO_INCREMENT=4 ;-- -- Dumping data for table
> `inf_league`-- INSERT INTO `inf_league` (`gid`, `game`, `type`,
> `league`, `season`) VALUES (1, 'DF:BHD', 'TKOTH', 'TWL', '2006 1st
> Quarter');INSERT INTO `inf_league` (`gid`, `game`, `type`, `league`,
> `season`) VALUES (2, 'CoD2', 'CTF', 'TWL', '2006 2nd QTR');INSERT INTO
> `inf_league` (`gid`, `game`, `type`, `league`, `season`) VALUES (3,
> 'CoD2', 'Search & Destroy', 'CAL', '2006 4th QTR');--
> ---------------------------------------------------------- -- Table
> structure for table
> `inf_member`-- CREATE TABLE `inf_member` ( `user_id` int(11) NOT NULL
> auto_increment, `user_level` int(2) NOT NULL default '0',
> `list_order` int(3) NOT NULL default '0', `user_name` varchar(100)
> NOT NULL default '', `p***word` varchar(25) NOT NULL default '',
> `email` varchar(100) NOT NULL default '', `country` text NOT NULL,
> `game` text, `rank` varchar(40) default NULL, `qoute` longtext,
> `config` int(1) default '0', `map` varchar(100) default '', `gun`
> varchar(100) default '', `brand` varchar(100) default '', `cpu`
> varchar(20) default '', `ram` varchar(20) default '', `video`
> varchar(100) default '', `sound` varchar(100) default '', `monitor`
> varchar(100) default '', `mouse` varchar(100) default '', PRIMARY
> KEY (`user_id`)) TYPE=MyISAM AUTO_INCREMENT=3 ;--
> -- Dumping data for table `inf_member`-- INSERT INTO `inf_member`
> (`user_id`, `user_level`, `list_order`, `user_name`, `p***word`,
> `email`, `country`, `game`, `rank`, `qoute`, `config`, `map`, `gun`,
> `brand`, `cpu`, `ram`, `video`, `sound`, `monitor`, `mouse`) VALUES
> (1, 1, 0, 'nitrox', 'test', 'EMAIL REMOVED', 'United States', 'CoD2',
> 'Founder', NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
> NULL);INSERT INTO `inf_member` (`user_id`, `user_level`, `list_order`,
> `user_name`, `p***word`, `email`, `country`, `game`, `rank`, `qoute`,
> `config`, `map`, `gun`, `brand`, `cpu`, `ram`, `video`, `sound`,
> `monitor`, `mouse`) VALUES (2, 1, 1, 'raze', 'itsme',
> 'EMAIL REMOVED', 'United States', NULL, 'Leader', NULL, 0, '', '',
> '', '', '', '', '', '', '');--
> ---------------------------------------------------------- -- Table
> structure for table
> `inf_memberleague`-- CREATE TABLE `inf_memberleague` ( `l_id` int(4)
> NOT NULL, `m_id` int(4) NOT NULL) TYPE=MyISAM;-- -- Dumping data for
> table `inf_memberleague`-- INSERT INTO `inf_memberleague` (`l_id`,
> `m_id`) VALUES (1, 2);INSERT INTO `inf_memberleague` (`l_id`, `m_id`)
> VALUES (1, 1);INSERT INTO `inf_memberleague` (`l_id`, `m_id`) VALUES
> (2, 1);INSERT INTO `inf_memberleague` (`l_id`, `m_id`) VALUES (2, 2);
>
> __________________________________________________ _______________
> Get live scores and news about your team: Add the Live.com Football
> Page http://www.live.com/?addtemplate=foo...T001MSN30A0701
>

   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: [PHP] multidimensional array problems
Old
  (#3)
nitrox .
Guest
 
Posts: n/a
Default Re: [PHP] multidimensional array problems - 05-14-2007, 03:35 AM

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’s Day -- Shop for gifts that spell L-O-V-E at MSN Shopping
http://shopping.msn.com/content/shp/...ode=wlmtagline
   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: [PHP] multidimensional array problems
Old
  (#4)
Larry Garfield
Guest
 
Posts: n/a
Default Re: [PHP] multidimensional array problems - 05-14-2007, 03:35 AM

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
EMAIL REMOVED 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
   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





Contact Us - Forum Care Forums - Archive - Top