Jump to content


making a specific variable in a loop

  • Please log in to reply
3 replies to this topic

#1 jdubwelch

  • Members
  • PipPipPip
  • Advanced Member
  • 85 posts

Posted 28 February 2006 - 06:41 PM

I have a text file:

team [tab] rank[enter]

There are 8 teams in the text file. What I'm trying to do is loop through each team and assign each team in the list to a value so I can put it into my code later on in the script. Here's my code for getting the stuff out of the text file:

$filename = '../../https/hoopFix06/oakland.txt';
$fp = fopen ($filename, "r") or die ("error");
$data = fread ($fp, filesize($filename));
fclose ($fp);
$line = explode ("\n", $data);
$i = count ($line);
for ($n=0; $n<$i-1; $n++) {
    $teamData = explode ("\t", $line[$n]);
    $teamName = $teamData[0];
    $teamRank = $teamData[1];

i want the first team to be labeled $a11a, the second team $a11b, the third team $a12a, the fourth team $a12b, etc. I don't know how to get those into the loop though.

#2 kenrbnsn

  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 28 February 2006 - 08:19 PM

Is there a reason why you want names like that? Why not store the names and ranks in an array, where the team name would be in the index, and the rank is the value:
$teams = array();
$filename = '../../https/hoopFix06/oakland.txt';
$data = file($filename); // reads the file into an array
foreach($data as $line) {
    list($tn, $tr) = explode ("\t", trim($line));
    $team[$tn] = $tr; }


#3 radius

  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 28 February 2006 - 08:20 PM

hum i'm not sure to understand what you want, by the way if you want to create variables on the fly you can do that

$teamname = "a11a";
$$teamname = "yo";
${$teamname . $teamname} = "yoyo";

then you have $a11a = "yo" and $a11aa11a = "yoyo"

#4 jdubwelch

  • Members
  • PipPipPip
  • Advanced Member
  • 85 posts

Posted 01 March 2006 - 03:21 AM

well i'm making a thing for the march madness basketball tournament coming up. i don't know what teams are going to be in the bracket yet, but I want to start making it. I was thinking if I just read the teams in from a text file, then it doesn't matter that there are no teams set in the bracket yet, because all i have to do is change 1 file.

does that make sense? Is there a better way to do that?

it's a 63 game tournament, with 64 teams participating in it. Right now I'm just trying to do a "mini" version to get it working... then I'll expand it. And if it's just reading from a text file, it'll be easy to expand. Or at least i think it does.

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users