Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Iteration question when reading CSV

Status
Not open for further replies.

ppat60

Programmer
Oct 20, 2007
13
0
0
US
My last issue at last :) . I have uncovered a poor logical error on my part. I hope the CSV file and being reading it in(that was the easy part).

I only want to read in the first 4 turns/players and store their data(okay that wasn't to bad either). I am using a for loop for that one. Then I use a while loop to continue reading the CSV file. And that is where my issue is. When the While loop starts to read, it starts at the top of the file(CSV). So my question is how can I read the first four lines/players info then transition into reading from line 5 to the end with out re-reading the first four lines? Here is how I currently have it set up below.
Code:
//Open file to read
 $handle = fopen("YellowWins.csv", "r");
   for($i = 0; $i < 4; $i++)
   {
          $data = fgetcsv($handle, 1024, ",");
        // read in first four turns before while loop.
          if($i == 0){
              echo $i. " Begin <br>";
              $Player_Array[0][0] = $data[0];
              $Player_Array[0][1] = $data[1];
              $Player_Array[0][2] = "Route One";
              $Player_Array[0][3] = $RouteOne[0];
              $Player_Array[0][5] = $RouteOne;
              $Player_Array[0][6] = $Player1;
              $whichPlayerInstance = $Player1;
              Enact_Turn($data, $Player_Array,$whichPlayerInstance,$RouteTwo,$RouteThree);
          }
          elseif($i == 1){
              echo $i. "<br>";
              $Player_Array[1][0] = $data[0];
              $Player_Array[1][1] = $data[1];
              $Player_Array[1][2] = "Route One";
              $Player_Array[1][3] = $RouteOne[0];
              $Player_Array[1][5] = $RouteOne;
              $whichPlayerInstance = $Player2;
              $Player_Array[1][6] = $Player2;
              Enact_Turn($data, $Player_Array,$whichPlayerInstance,$RouteTwo,$RouteThree);
          }
          elseif($i == 2){
              echo $i. "<br>";
              $Player_Array[2][0] = $data[0];
              $Player_Array[2][1] = $data[1];
              $Player_Array[2][2] = "Route One";
              $Player_Array[2][3] = $RouteOne[0];
              $Player_Array[2][5] = $RouteOne;
              $whichPlayerInstance = $Player3;
              $Player_Array[2][6] = $Player3;
              Enact_Turn($data, $Player_Array,$whichPlayerInstance,$RouteTwo,$RouteThree);
          }
          elseif($i == 3){
              echo $i. "<br>";
              $Player_Array[3][0] = $data[0];
              $Player_Array[3][1] = $data[1];
              $Player_Array[3][2] = "Route One";
              $Player_Array[3][3] = $RouteOne[0];
              $Player_Array[3][5] = $RouteOne;
              $whichPlayerInstance = $Player4;
              $Player_Array[3][6] = $Player4;
              Enact_Turn($data, $Player_Array,$whichPlayerInstance,$RouteTwo,$RouteThree);
          }
          else{
            echo("Sorry there was a system var i problem");
          }
       echo "<br>" .$i. " end of for loop <br>";
   }
   //Open class to read csv into data
   while (($data = fgetcsv($handle, 1024, ",")) !== FALSE)
   {
     Enact_Turn($data, $Player_Array, $whichPlayerInstance,$RouteTwo,$RouteThree);
   }
   echo"<br> -- END OF FILE -- ";
 //close file
 fclose($handle);

Thank you for your time!
 
Code:
$handle = fopen("YellowWins.csv", "r");
$c = 0;
while (while (($data = fgetcsv($handle, 1024, ",")) !== FALSE) {
 if ($c <= 3){
  echo $i. " Begin <br>";
  $Player_Array[$c][0] = $data[0];
  $Player_Array[$c][1] = $data[1];
  $Player_Array[$c][2] = "Route One";
  $Player_Array[$c][3] = $RouteOne[0];
  $Player_Array[$c][5] = $RouteOne;
  $Player_Array[$c][6] = $Player{$c+1};
  $whichPlayerInstance = $Player{$c+1};
  $c++;
 }
 Enact_Turn($data, $Player_Array,$whichPlayerInstance,$RouteTwo,$RouteThree);  
}
 
jpadie,

Thanks for your time and help. I tested what you wrote, but I had some issues. I am using php -v 5.1.2 my first isshe was an Unexpected while in the while(while(... when I dropped one of the while statement then I had object issues, because I need $Player_Array[0-3][6] to call a member function and it is failing so it causes a fatal error... I will look at it more when I am in a bit... do you have any other ideas for a work around for this? I thought this was really slick move to eliminate some code redundancy.

Thank you...
pat
 
the double while was cut and paste gone wrong.

i don't understand your second point. i cannot see any objects in your posted code.
 
I will post the entire code. Warning... this is very ugly and was planned for a reason to be like this to some degree.. Here we go.
Code:
<?php
 $RouteOne = array ("Start","Black","Black","Black","Yellow","Black","Green","Black","Blue","Black",
                    "Red","Black","Yellow","Green","Red","Blue","Red","Black","Red","Black",
                    "Yellow","Black","Green","Black","Blue","Black","Red");
 $RouteTwo = array ("Start","Black","Black","Red","Black","Green","Black","Yellow","Black","Blue",
                    "Black","Red","Black","Green","Black","Yellow","Black","Blue","Black");
 $RouteThree = array ("Start","Purple","Purple","Purple","Purple","Purple","Green1","Purple","Purple",
                      "Purple","Purple","Purple","Green2","Purple","Purple","Purple","Purple","Finish");
 $Special = array("Start","Go Back","Finish");

class Player
{
  var $Player_Color;
  var $Current_Route;
  var $Current_Square;
  var $Current_DieRoll;
  var $New_Square_Color;

  public function Pcolor($data)
  {
    echo($this->Player_Color = $data[0] .  " car is current color playing.<br>");
  }

  public function PRoute()
  {
    echo $this->Current_Route. " is your current route.";
  }

   public function PSquare()
  {
    echo "you were on a " .$this->Current_Square. " square color.";
  }
  function MoveArray($data,$myRteArray,$whichPlayerInstance,$Player_Array,$RouteTwo,$RouteThree)
  {
      echo"(MoveArray) your Player Obj Id is: " .$whichPlayerInstance. "<br>";
      $Array = $myRteArray;
      echo"(MoveArray)your array start position before the move is: " .$Array[0]. "<br>";
        for($i = 1; $i <= $data[1]; $i++)
        {
           echo("(MoveArray) landed on : ");
           echo( $saveNewPos = $Array[$i]. "<br>");
        }
      echo("(MoveArray) saveNewPos= " .$saveNewPos. "<br>");
      $whichPlayerInstance->PnewSquare($saveNewPos,$data,$Player_Array,$RouteTwo,$RouteThree);
  }

   public function PDieRoll($data)
   {
     echo "your current die roll is: " .$this->Current_DieRoll = $data[1]. "<br>";
   }

  public function PnewSquare($saveNewPos,$data,$Player_Array,$RouteTwo,$RouteThree)
  {
  /* This member function is to save the "New Square" for each
       player(1-4) of game in to a 2D array and to check if
       Player of color is on their own color to advance to
       other arrays to complete the game
  */
    echo("(PnewSquare) saveNewPos = " .$saveNewPos. "<br>");

    if('Red' == $data[0] ){
      //save position for player 1
      $Player_Array[0][4] = $saveNewPos;
      echo "(PnewSquare P1) your new square is: " .$saveNewPos. "<br>";
    }

elseif('Yellow' == $data[0]){
      //save position for player 2
      $Player_Array[1][4] = $saveNewPos;
      echo "(PnewSquare P2) your new square is: " .$saveNewPos. "<br>";
      //if Route Array == Route One do this call to see if we advance to Array H2o 1 to 2
      if("Route One" == $Player_Array[1][2]){
       echo "inside EVAL call array 2 for Route ONE <br>";
        $Player_Array[1][6]->ArrayWater($data,$saveNewPos,$Player_Array,$RouteTwo,$RouteThree);
      }
      else
      {
        if("Route Two" == $Player_Array[1][2]){
        echo"EVAL for Route TWO <br>";
        //if Route Array == Route Two do make call to Array Air to see if we move form 2 to 3
          $Player_Array[1][6]->ArrayAir($data,$saveNewPos,$Player_Array,$RouteTwo,$RouteThree);
        }
        else
        { // G1 This sends user back to   Array
          if("Route Three" == $Player_Array[1][2] && "G" == $saveNewPos[0] && "1" == $saveNewPos[5]){
          //Call Function to return user to appropriate route.
          echo "Sorry you landed on a wheel, you are returning to Array number 1 ";
          }
          else
          {
            if("Route Three" == $Player_Array[1][2] && "G" == $saveNewPos[0] && "2" == $saveNewPos[5]){
            //Call Function to retrun user to appropriate route
            echo "Sorry you landed a the second wheel, your going back to Array 2";
            }
            else
            {
              echo("Error or bad logic line 101ish");
            }
          }
        }
      } 
}//end yellow elseif
    elseif('Green' == $data[0]){
      //save positon for player 3
      $Player_Array[2][4] = $saveNewPos;
      echo "(PnewSquare P3) your new square is: " .$saveNewPos. "<br>";
      //$Player = 4;
    }
    elseif('Blue' == $data[0]){
      //save position for player 4
     $Player_Array[3][4] = $saveNewPos;
      echo "(PnewSquare P3) your new square is: " .$saveNewPos. "<br>";
      //$Player = "";
    }
    else
      { echo("Sorry, there was an error in the saving the New Square Process at line 99 <br>"); }
  }

  public function ArrayWater($data,$saveNewPos,$Player_Array,$RouteTwo,$RouteThree) {
  /*
      This function to verify that users Car Color and square color match to advance
      to Second array.
  */
      echo"(ArrayWater)your in the save new Array function <br>";

   if('Red' == $data[0] && 'R' == $saveNewPos[0])
   {
     echo"(WATERARRAY P1) you are now using Route 2";
     $Player_Array[0][2] = "Route Two";
     $Player_Array[0][3] = $RouteTwo[0];
     $Player_Array[0][5] = $RouteTwo;
   }
   else
   {
     if('Yellow' == $data[0] && 'Y' == $saveNewPos[0])
     {     
echo "(ARRAYWATER P2)you in array two because data and saveNewPos matched <br>";
       echo $Player_Array[1][2] = "Route Two";
       echo"<br> Your new starting position is: " . $Player_Array[1][3] = $RouteTwo[0]. "in " .$Player_Array[1][2]. "<br>"
       echo $Player_Array[1][5] = $RouteTwo; echo "<br>";
       echo "(ARRAYWATER P2)SAVED NEW DATA to  at this point or at least should have.........";
     }
     else
     {
       if('Green' == $data[0] && 'G' == $saveNewPos[0])
       {
         $Player_Array[2][2] = "Route Two";
         $Player_Array[2][3] = $RouteTwo[0];
         $Player_Array[2][5] = $RouteTwo;
       }
       else
       {
         if('Blue' == $data[0] && 'B' == $saveNewPos[0] && 'l' == $saveNewPos[1] && 'u' == $saveNewPos[2])
         {
           $Player_Array[3][2] = "Route Two";
           $Player_Array[3][3] = $RouteTwo[0];
           $Player_Array[3][5] = $RouteTwo;
         }
         else
         {
           return;
           //echo("Error: error in Water Array Function <br>" );
         }
       }
     }
   }

  }// end Array Water
public function ArrayAir($data,$saveNewPos,$Player_Array,$RouteTwo,$RouteThree) {
  /*
      This function to verify that users Car Color and square color match to advance
      to Third array aka ArrayAir.
  */
      echo"(ArrayAir)your in the save new Array function <br>";

   if('Red' == $data[0] && 'R' == $saveNewPos[0])
   {
     echo"(AIR ARRAY P1) you are now using Route 2";
     $Player_Array[0][2] = "Route Three";
     $Player_Array[0][3] = $RouteThree[0];
     $Player_Array[0][5] = $RouteThree;
   }
   else
   {
     if('Yellow' == $data[0] && 'Y' == $saveNewPos[0])
     { $startPos = "";
       echo "(AIR ARRAY P2) YOUR IN ARRAY 3 :p <br>";
       $Player_Array[1][2] = "Route Three";
       echo"<br> Your new starting position in route 2 will be: " . $Player_Array[1][3] = $RouteThree[0]. "<br>";
       $Player_Array[1][5] = $RouteThree;
     }
     else
     {
       if('Green' == $data[0] && 'G' == $saveNewPos[0])
       {
         $Player_Array[2][2] = "Route Three";
         $Player_Array[2][3] = $RouteThree[0];
         $Player_Array[2][5] = $RouteThree;
       }
       else
       {
         if('Blue' == $data[0] && 'B' == $saveNewPos[0] && 'l' == $saveNewPos[1] && 'u' == $saveNewPos[2])
         {     
$Player_Array[3][2] = "Route Three";
           $Player_Array[3][3] = $RouteThree[0];
           $Player_Array[3][5] = $RouteThree;
         }
         else
         {
           return;
           //echo("Error: error in AIR Array Function <br>" );
         }
       }
     }
   }

  }//end Array Air
####################################################
} // end of Player Class

function Enact_Turn($data, $Player_Array, $whichPlayerInstance,$RouteTwo, $RouteThree)
 {
   $whichPlayer = $data[0];
   switch($whichPlayer)
   {
        case "Red":
        // do your work here
          // Make call to look up player Color
          if($Player_Array[0][0] == $data[0]){
            $Player_Array[0][6]->Pcolor($data);
            $whichPlayerInstance = $Player_Array[0][6];
            // Make call to look up which Route Number
            echo("You are on " .$Player_Array[0][2]. "<br>");
            // look up last Square_Color[]
            echo("Your current square position before die roll is " .$myCurrSquare = $Player_Array[0][3]. "<br>");
            // Make Die Roll or retrieve the die roll
            echo("Your die roll was " .$data[1]. "<br>");
            // Make Call to MOVE Array[index] to Match Die Roll
              $myRteArray = $Player_Array[0][5];
              $Player_Array[0][6]->MoveArray($data,$myRteArray,$whichPlayerInstance,$Player_Array,$RouteTwo,$RouteThree);
            // Make call to save Current Route Number
            // Make call to save Current Square_Color[]
            echo("This is the end of Red: <br><br>");
          }
          else{
            echo("Error: Player Color does not match this turn for Player 1 <br>");
          }
          break;
        case "Yellow":
          // Make call to look up player Color
          if($Player_Array[1][0] ==  $Player2->Player_Color = $data[0]){
              $Player_Array[1][6]->Pcolor($data);
              $whichPlayerInstance = $Player_Array[1][6];
            // Make call to look up which Route Number
            echo "here I am in Yellow case stmt...";     
  echo("You are on " .$myRoute = $Player_Array[1][2]. "<br>");
            // Make call to look up last Square_Color[]
              echo("Your current square position before die roll is " .$myCurrSquare = $Player_Array[1][3]. "<br>");
            // Make Die Roll or retrieve the die roll
              echo("Your die roll was " .$data[1]. "<br>");
            // Make Call to MOVE Array[index] to Match Die Roll
              $myRteArray = $Player_Array[1][5];
              $Player_Array[1][6]->MoveArray($data,$myRteArray,$whichPlayerInstance,$Player_Array,$RouteTwo,$RouteThree);
            // Make call to save Current Route Number
              if($data[0])
                //echo("Your route number is <br>");
            echo("This is the end of Yellow: <br><br>");
          }
          else{
            echo("Error:  Player Color does not match this turn for Player 2 <br>");
          }
        break;
        case "Green":
          // do your work here
          // Make call to look up player Color
          if($Player_Array[2][0] ==  $Player3->Player_Color = $data[0]){
            $Player_Array[2][6]->Pcolor($data);
            $whichPlayerInstance = $Player_Array[2][6];
            // Make call to look up which Route Number
            echo("You are on " .$myRoute = $Player_Array[2][2]. "<br>");
            // Make call to look up last Square_Color[]
            echo("Your current square position before die roll is " .$myCurrSquare = $Player_Array[2][3]. "<br>");
            // Make Die Roll or retrieve the die roll
            echo("Your die roll was " .$data[1]. "<br>");
            // Make Call to MOVE Array[index] to Match Die Roll
            $myRteArray = $Player_Array[2][5];
            $Player_Array[2][6]->MoveArray($data,$myRteArray,$whichPlayerInstance,$Player_Array,$RouteTwo,$RouteThree);
            // Make call to save Current Route Number
            echo("<br>");
        }                      
  else{
          echo("Error:  Player Color does not match this turn for Player 3 <br>");
        }
        break;
                case "Blue":
                // do your work here
                // Make call to look up player Color
                if($Player_Array[3][0] ==  $Player4->Player_Color = $data[0]){
                  $Player_Array[3][6]->Pcolor($data);
                  $whichPlayerInstance = $Player_Array[3][6];
                  // Make call to look up which Route Number
                  echo("You are on " .$myRoute = $Player_Array[3][2]. "<br>");
                  // Make call to look up last Square_Color[]
                  echo("Your current square position before die roll is " .$myCurrSquare = $Player_Array[3][3]. "<br>");
                  // Make Die Roll or retrieve the die roll
                  echo("Your die roll was " .$data[1]. "<br>");
                  // Make Call to MOVE Array[index] to Match Die Roll
                  $myRteArray = $Player_Array[3][5];
                  $Player_Array[3][6]->MoveArray($data,$myRteArray,$whichPlayerInstance,$Player_Array,$RouteTwo,$RouteThre
                  // Make call to save Current Route Number
                  //echo("Your route number is <br>");
                  // Make call to save Current Square_Color[]
                  //echo("Your new square color after your die roll is <br>");
                  echo("<br>");
                }
                else{
                  echo("Error:  Player Color does not match this turn for Player 4 <br>");
                }
                break;
   }
 }

 // Main() ### This is not a function ###
 echo "Welcome to the Three Routes Game<br><br>";
//  $Player = new Player;
  $Player1 = new Player;
  $Player2 = new Player;
  $Player3 = new Player;
  $Player4 = new Player;

 $Player_Array = array("Player1"=> array("Color","DieRoll","CurrRoute","CurrSquareColor","NewSquareColor","RteArray","P1",
                       "Player2"=> array("Color","DieRoll","CurrRoute","CurrSquareColor","NewSquareColor","RteArray","P2",
                       "Player3"=> array("Color","DieRoll","CurrRoute","CurrSquareColor","NewSquareColor","RteArray","P3",
                       "Player4"=> array("Color","DieRoll","CurrRoute","CurrSquareColor","NewSquareColor","RteArray","P4",
                       );

 //Open file to read
 $handle = fopen("YellowWins.csv", "r");
 $i = 0;
 while(($data = fgetcsv($handle, 1024, ",")) !== FALSE) {
   if($i < 4)
   {
        // read in first four turns before while loop.
          if($i == 0){
              $Player_Array[0][0] = $data[0];
              $Player_Array[0][1] = $data[1];
              $Player_Array[0][2] = "Route One";
              $Player_Array[0][3] = $RouteOne[0];
              $Player_Array[0][5] = $RouteOne;
              $Player_Array[0][6] = $Player1;
              $whichPlayerInstance = $Player1;
              $i++;
          }
          elseif($i == 1){
              $Player_Array[1][0] = $data[0];
              $Player_Array[1][1] = $data[1];
              $Player_Array[1][2] = "Route One";
              $Player_Array[1][3] = $RouteOne[0];
              $Player_Array[1][5] = $RouteOne;    
                        $whichPlayerInstance = $Player2;
              $Player_Array[1][6] = $Player2;
              $i++;
          }
          elseif($i == 2){
              $Player_Array[2][0] = $data[0];
              $Player_Array[2][1] = $data[1];
              $Player_Array[2][2] = "Route One";
              $Player_Array[2][3] = $RouteOne[0];
              $Player_Array[2][5] = $RouteOne;
              $whichPlayerInstance = $Player3;
              $Player_Array[2][6] = $Player3;
              $i++;
          }
          elseif($i == 3){
              $Player_Array[3][0] = $data[0];
              $Player_Array[3][1] = $data[1];
              $Player_Array[3][2] = "Route One";
              $Player_Array[3][3] = $RouteOne[0];
              $Player_Array[3][5] = $RouteOne;
              $whichPlayerInstance = $Player4;
              $Player_Array[3][6] = $Player4;
              $i++;
          }
          else{ }
           // echo("Sorry there was a system var i problem");
          //}
   }
 Enact_Turn($data, $Player_Array,$whichPlayerInstance,$RouteTwo,$RouteThree);
}
   echo" -- END OF FILE -- ";
 //close file
 fclose($handle);
?>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top