Jump to content


Photo

multi-dimension arrays and classes


  • Please log in to reply
6 replies to this topic

#1 poweredbyaces

poweredbyaces
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 11 August 2006 - 04:58 AM

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


#2 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 11 August 2006 - 05:17 AM

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:

      $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;


#3 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 11 August 2006 - 05:19 AM

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


Why dont you show us that function then?

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

$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;


#4 poweredbyaces

poweredbyaces
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 11 August 2006 - 06:26 PM

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



}
}










?>


#5 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 11 August 2006 - 08:54 PM

... 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.

#6 poweredbyaces

poweredbyaces
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 11 August 2006 - 09:10 PM

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...


#7 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 11 August 2006 - 10:57 PM

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:
$this->$teamid = $teamid;

should probably be:

$this->teamid = $teamid;

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

      $this->teamname=$row{'name'};
      $this->mascot=$row{'mascot'};

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

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:

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

should be:

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

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:

      $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;

should be:

      $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;

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?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users