Jump to content

Archived

This topic is now archived and is closed to further replies.

poweredbyaces

multi-dimension arrays and classes

Recommended Posts

Hi-

Im working on a baseball simulation where i store all player attributes in a multi dimension array.

In the class i have an init function that pulls the data from the db and stores it as such:


$this->ratings[$mypos]=$plid;
$this->ratings[$mypos][name]=$name;
$this->ratings[$mypos][phit]=$phit;
$this->ratings[$mypos][chit]=$chit;
$this->ratings[$mypos][plhit]=$plhit;
$this->ratings[$mypos][spd]=$spd;
$this->ratings[$mypos][glv]=$glv;
$this->ratings[$mypos][arm]=$arm;
$this->ratings[$mypos][awr]=$awr;
$this->ratings[$mypos][cnf]=$cnf;
$this->ratings[$mypos][clch]=$clch;
$this->ratings[$mypos][tlnt]=$tlnt;


But then when i print this array with print_r, i only get values for the first column:

Array ( [catcher] => 98757 [batorder] => 18769 [] => 78769 [first] => 88759 [second] => 88760 [short] => 78761 [third] => 88762 [left] => 68765 [center] => 88766 [right] => 38767 [reliefpitch] => 78773 [closepitch] => 78774 )

Thoughts?
Rob

Share this post


Link to post
Share on other sites
it's because you're initializing it as a scalar, then attempting to add values to it as an array.  choose which one you want.  try:

[code]
      $this->ratings[$mypos][plid]=$plid;
      $this->ratings[$mypos][name]=$name;
      $this->ratings[$mypos][phit]=$phit;
      $this->ratings[$mypos][chit]=$chit;
      $this->ratings[$mypos][plhit]=$plhit;
      $this->ratings[$mypos][spd]=$spd;
      $this->ratings[$mypos][glv]=$glv;
      $this->ratings[$mypos][arm]=$arm;
      $this->ratings[$mypos][awr]=$awr;
      $this->ratings[$mypos][cnf]=$cnf;
      $this->ratings[$mypos][clch]=$clch;
      $this->ratings[$mypos][tlnt]=$tlnt;
[/code]

Share this post


Link to post
Share on other sites
[quote]In the class i have an init function that pulls the data from the db and stores it as such:[/quote]

Why dont you show us that function then?

And while were at it, we may aswell make the code you posted legal.

[code=php:0]
$mypos=$plid;
$this->ratings[$mypos]['name']=$name;
$this->ratings[$mypos]['phit']=$phit;
$this->ratings[$mypos]['chit']=$chit;
$this->ratings[$mypos]['plhit']=$plhit;
$this->ratings[$mypos]['spd']=$spd;
$this->ratings[$mypos]['glv']=$glv;
$this->ratings[$mypos]['arm']=$arm;
$this->ratings[$mypos]['awr']=$awr;
$this->ratings[$mypos]['cnf']=$cnf;
$this->ratings[$mypos]['clch']=$clch;
$this->ratings[$mypos]['tlnt']=$tlnt;
[/code]

Share this post


Link to post
Share on other sites
ive tried [], ive tried [''] ive tried [""]  no difference.  Heres the code as is:


<?

class Team {

var $teamid;
var $teamname;
var $mascot;

var $ratings=array();


function PlayerName ( $which , $spot ){

if($which=="position"){
echo $this->ratings[$spot][name];
} else {
echo $this->ratings[batorder][$spot][name];
}
}



function GetInfo ( $teamid ){

$this->$teamid = $teamid;


$sql = "SELECT * FROM teams WHERE `index`='$teamid'";
$result = @mysql_query($sql) or die("couldn't execute query $sql.".mysql_error().mysql_error());
while($row = mysql_fetch_array($result)) {
$this->teamname=$row{'name'};
$this->mascot=$row{'mascot'};
}


$c=0;
$if=0;
$of=0;
$order=0;
$sql = "SELECT * FROM players WHERE `team`='$teamid' && `pos`!='P' order by `order`";
$result = @mysql_query($sql) or die("couldn't execute query $sql.".mysql_error().mysql_error());
while($row = mysql_fetch_array($result)) {
$plid=$row{'index'};
$name=$row{'firstname'}. " " . $row{'lastname'};
$age=$row{'age'};
$pos=$row{'pos'};
$mypos="";

if($pos=="C"){
$count=0;
$sqlb = "SELECT * FROM players WHERE `team`='$teamid' && `pos`='C' order by `fielding`";
$resultb = @mysql_query($sqlb) or die("couldn't execute query $sql.".mysql_error().mysql_error());
while($rowb = mysql_fetch_array($resultb)) {
$count++;
if($rowb{'index'}==$plid){ $c=$count; }
}
if($c==1){
$mypos="catcher";
$order++;
}
}


if($pos=="IF"){
$count=0;
$sqlb = "SELECT * FROM players WHERE `team`='$teamid' && `pos`='IF' order by `fielding`";
$resultb = @mysql_query($sqlb) or die("couldn't execute query $sql.".mysql_error().mysql_error());
while($rowb = mysql_fetch_array($resultb)) {
$count++;
if($rowb{'index'}==$plid){ $if=$count; }
}
if($if==1){
$mypos="first";
$order++;
}
if($if==2){
$mypos="second";
$order++;
}
if($if==3){
$mypos="short";
$order++;
}
if($if==4){
$mypos="third";
$order++;
}
}
if($pos=="OF"){
$count=0;
$sqlb = "SELECT * FROM players WHERE `team`='$teamid' && `pos`='OF' order by `fielding`";
$resultb = @mysql_query($sqlb) or die("couldn't execute query $sql.".mysql_error().mysql_error());
while($rowb = mysql_fetch_array($resultb)) {
$count++;
if($rowb{'index'}==$plid){ $of=$count; }
}
if($of==1){
$mypos="left";
$order++;
}
if($of==2){
$mypos="center";
$order++;
}
if($of==3){
$mypos="right";
$order++;
}
}

$sqlb = "SELECT * FROM pattributes_baseball WHERE `index`='$plid'";
$resultb = @mysql_query($sqlb) or die("couldn't execute query $sql.".mysql_error().mysql_error());
while($rowb = mysql_fetch_array($resultb)) {
$phit=$rowb{'phit'};
$chit=$rowb{'chit'};
$plhit=$rowb{'plhit'};
$spd=$rowb{'spd'};
$glv=$rowb{'glv'};
$awr=$rowb{'awr'};
$arm=$rowb{'arm'};
$cnf=$rowb{'cnf'};
$clch=$rowb{'clch'};
$tlnt=$rowb{'tlnt'};
}


$this->ratings[$mypos]=$plid;
$this->ratings[$mypos][name]=$name;
$this->ratings[$mypos][phit]=$phit;
$this->ratings[$mypos][chit]=$chit;
$this->ratings[$mypos][plhit]=$plhit;
$this->ratings[$mypos][spd]=$spd;
$this->ratings[$mypos][glv]=$glv;
$this->ratings[$mypos][arm]=$arm;
$this->ratings[$mypos][awr]=$awr;
$this->ratings[$mypos][cnf]=$cnf;
$this->ratings[$mypos][clch]=$clch;
$this->ratings[$mypos][tlnt]=$tlnt;

$this->ratings[batorder]=$plid;
$this->ratings[batorder][$order][position]=$mypos;
$this->ratings[batorder][$order][name]=$name;
$this->ratings[batorder][$order][phit]=$phit;
$this->ratings[batorder][$order][chit]=$chit;
$this->ratings[batorder][$order][plhit]=$plhit;
$this->ratings[batorder][$order][spd]=$spd;
$this->ratings[batorder][$order][glv]=$glv;
$this->ratings[batorder][$order][arm]=$arm;
$this->ratings[batorder][$order][awr]=$awr;
$this->ratings[batorder][$order][cnf]=$cnf;
$this->ratings[batorder][$order][clch]=$clch;
$this->ratings[batorder][$order][tlnt]=$tlnt;

}


$which=0;
$sql = "SELECT * FROM players WHERE `team`='$teamid' && `pos`='P' order by `order`";
$result = @mysql_query($sql) or die("couldn't execute query $sql.".mysql_error().mysql_error());
while($row = mysql_fetch_array($result)) {
$plid=$row{'index'};
$name=$row{'firstname'}. " " . $row{'lastname'};
$age=$row{'age'};
$pos=$row{'pos'};
$which++;
if($gametime==$which){
$this->ratings[startpitch]=$plid;
$this->ratings[startpitch][name]=$name;
$this->ratings[startpitch][phit]=$phit;
$this->ratings[startpitch][chit]=$chit;
$this->ratings[startpitch][plhit]=$plhit;
$this->ratings[startpitch][spd]=$spd;
$this->ratings[startpitch][glv]=$glv;
$this->ratings[startpitch][arm]=$arm;
$this->ratings[startpitch][awr]=$awr;
$this->ratings[startpitch][cnf]=$cnf;
$this->ratings[startpitch][clch]=$clch;
$this->ratings[startpitch][tlnt]=$tlnt;

$this->ratings[batorder]=$plid;
$this->ratings[batorder][9][name]=$name;
$this->ratings[batorder][9][phit]=$phit;
$this->ratings[batorder][9][chit]=$chit;
$this->ratings[batorder][9][plhit]=$plhit;
$this->ratings[batorder][9][spd]=$spd;
$this->ratings[batorder][9][glv]=$glv;
$this->ratings[batorder][9][arm]=$arm;
$this->ratings[batorder][9][awr]=$awr;
$this->ratings[batorder][9][cnf]=$cnf;
$this->ratings[batorder][9][clch]=$clch;
$this->ratings[batorder][9][tlnt]=$tlnt;
}
if($which==4){
$this->ratings[reliefpitch]=$plid;
$this->ratings[reliefpitch][name]=$name;
$this->ratings[reliefpitch][phit]=$phit;
$this->ratings[reliefpitch][chit]=$chit;
$this->ratings[reliefpitch][plhit]=$plhit;
$this->ratings[reliefpitch][spd]=$spd;
$this->ratings[reliefpitch][glv]=$glv;
$this->ratings[reliefpitch][arm]=$arm;
$this->ratings[reliefpitch][awr]=$awr;
$this->ratings[reliefpitch][cnf]=$cnf;
$this->ratings[reliefpitch][clch]=$clch;
$this->ratings[reliefpitch][tlnt]=$tlnt;
}
if($which==5){
$this->ratings[closepitch]=$plid;
$this->ratings[closepitch][name]=$name;
$this->ratings[closepitch][phit]=$phit;
$this->ratings[closepitch][chit]=$chit;
$this->ratings[closepitch][plhit]=$plhit;
$this->ratings[closepitch][spd]=$spd;
$this->ratings[closepitch][glv]=$glv;
$this->ratings[closepitch][arm]=$arm;
$this->ratings[closepitch][awr]=$awr;
$this->ratings[closepitch][cnf]=$cnf;
$this->ratings[closepitch][clch]=$clch;
$this->ratings[closepitch][tlnt]=$tlnt;
}
}



}
}










?>

Share this post


Link to post
Share on other sites
... did you even READ our replies?  we suggested the changes to make BEFORE asking for the script, since technically, we don't even need your script to tell you why this isn't working.

look at my original reply.

Share this post


Link to post
Share on other sites
Ive tried both versions of what you guys posted and neither has worked.  Which is why i posted the entire version of the code to see if something else was interfering...

Share this post


Link to post
Share on other sites
my apologies for jumping the gun, it just doesn't look like you've tried either of our replies (unless the code you've posted is the original).  here is a breakdown of things i found wrong with your code:

ONE:
[code]$this->$teamid = $teamid;[/code]

should probably be:

[code]$this->teamid = $teamid;[/code]

TWO:
i'm no classes guru, but i don't think array handling changes within classes.

[code]      $this->teamname=$row{'name'};
      $this->mascot=$row{'mascot'};[/code]

should be:
[code]      $this->teamname=$row['name'];
      $this->mascot=$row['mascot'];[/code]

this applies to all instances where you are using the $row array to assign local variables.

THREE:
as far as i know, the "is not equal to" operator in MySQL is not !=, it is <>.  check the manual to make sure on this one, as i maybe wrong.  if so, then:

[code]$sql = "SELECT * FROM players WHERE `team`='$teamid' && `pos`!='P' order by `order`";[/code]

should be:

[code]$sql = "SELECT * FROM players WHERE `team`='$teamid' && `pos`<>'P' order by `order`";[/code]

FOUR:
again, your assignment of the ratings array is wrong.  you cannot initiate it as a scalar, then assign array values to that scalar.  this:

[code]      $this->ratings[$mypos]=$plid;
      $this->ratings[$mypos][name]=$name;
      $this->ratings[$mypos][phit]=$phit;
      $this->ratings[$mypos][chit]=$chit;
      $this->ratings[$mypos][plhit]=$plhit;
      $this->ratings[$mypos][spd]=$spd;
      $this->ratings[$mypos][glv]=$glv;
      $this->ratings[$mypos][arm]=$arm;
      $this->ratings[$mypos][awr]=$awr;
      $this->ratings[$mypos][cnf]=$cnf;
      $this->ratings[$mypos][clch]=$clch;
      $this->ratings[$mypos][tlnt]=$tlnt;[/code]

should be:

[code]      $this->ratings[$mypos][plid]=$plid;
      $this->ratings[$mypos][name]=$name;
      $this->ratings[$mypos][phit]=$phit;
      $this->ratings[$mypos][chit]=$chit;
      $this->ratings[$mypos][plhit]=$plhit;
      $this->ratings[$mypos][spd]=$spd;
      $this->ratings[$mypos][glv]=$glv;
      $this->ratings[$mypos][arm]=$arm;
      $this->ratings[$mypos][awr]=$awr;
      $this->ratings[$mypos][cnf]=$cnf;
      $this->ratings[$mypos][clch]=$clch;
      $this->ratings[$mypos][tlnt]=$tlnt;[/code]

fix all that up, run it again, and let us know if it's still not working.  if it still isn't working, what are you getting in the class's variables, and what is the UPDATED code you are using?

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.