Logon to UM City or Apply for a UM City passport
 - UM City - Sport statistical logging/displaying    Home > DOWNTUM > Visual Stimulus > Sport statistical logging/displaying
x
Sport statistical logging/displaying
by Universal Sea
x
x
x Unread post xUniversal Sea   - at 5:26 pm on Tuesday February 12 2002 x
x x
x Universal Sea This was my response to a high school student in Texas who required help with having coaches update the scores of games onto a website, and then have the information viewed by the public.

Hi,

I understand what you're trying to do now. Its not terribly difficult, it will probably take half a day to get right. I don't have time this week to do the entire script myself, but I will provide as much help as I can, and explain the more difficult functions.

The functions you will need to use are:

fwrite();
fopen();
fclose();
fread();
filesize();

First, you want to create a separate directory for each sport:

/basketball
/baseball
/football
...etc

You must assign proper write permissions to these directories (like CHMOD 777 if need be), meaning that the coaches will be able to write to the folders without "permission denied" errors.

Once you have made these folders, create the individual text files. They can be a blank notepad document. For Basketball, create the following empty text files, name them as follows, and upload them to the basketball folder.

teamtype.txt
otherteam.txt
scoremarlins.txt
scoreopp.txt
leadersmarlins.txt
leadersopp.txt
gamenotes.txt

You will need to create three custom functions, one for organising and submitting the information, one for writing the new game data to the website, and the other to display the saved information. If you wanted to get more complex, you could have an "edit" function as well, based on a combination of the two I will describe.

In the form, you don't even need the final score, because we will tally that up automatically anyways. You also need to have different form field names for the periods, for each the Marlins and the other team (I will refer to them as "opp" for "opposition"). I have changed the form field "remarks" to "game notes".

The first custom function will simply upload the new game information to the server.

--

function uploadgameinfo($sport,$filename,$filedata){

// open the file in the directory for the given sport

$file=fopen("$sport/$filename", "a+");

// write to the page on the server

fwrite($fp, "$filedatan");

// close the file connection

fclose($file);
}

--


The second custom function will put the information in the database. I will call this "addgamestats" for now.

--

// declare the function
function addgamestats($sport){

// we will use basketball as the example here, you can change the form fields around for the other sports
if($sport=='basketball'){

// declare all of the form variables used on the form

global $teamtype,$period1marlins,$period2marlins,$period3marlins,$period4marlins,$scorermarlins,
$reboundermarlins,$otherteam,$period1opp,$period2opp,$period3opp,$period4opp,$scoreropp,
$rebounderopp,$gamenotes;

// piece together the score line for both teams
$scoremarlins = "$period1marlins-$period2marlins-$period3marlins-$period4marlins";
$scoreopp = "$period1opp-$period2opp-$period3opp-$period4opp";

// piece together the leading scorers and rebounders for each team (first the scorers, then the rebounders)

$leadermarlins = "$scorermarlins-$reboundermarlins";
$leaderopp = "$scoreropp-$rebounderopp";

// now start writing to each of the text files using the first function we made

// type of game and game info
uploadgameinfo("basketball","teamtype.txt",$teamtype);
uploadgameinfo("basketball","otherteam.txt",$otherteam);

// Marlins' and opponents scores
uploadgameinfo("basketball","scoremarlins.txt",$scoremarlins);
uploadgameinfo("basketball","scoreopp.txt",$scoreopp);

// game leaders (you could have the coaches put their points in brackets after their names, if you like)
uploadgameinfo("basketball","leadersmarlins.txt",$leadersmarlins);
uploadgameinfo("basketball","leadersopp.txt",$leadersopp);

// finally, the game notes
uploadgameinfo("basketball","gamenotes.txt",$gamenotes);

}
// end basketball

}
// end function

--

So, the first part is complete, the game information has been added to the server. You can add other fields like the date, who is home and away, attendance how hot the cheerleaders are, etc.

Basically the way this is all set up is that each game receives its own line in each of the text files, and the next part is to retrieve it and break it up for display.

The first stage is to show a list of the games stored on the server. You will need to create a PHP page called "games.php" (just like you have "game1.html", "game2.html", etc). This way, you just need one page to handle everything.

The second stage, when a game has been selected, is to show the game information.

--

// retrieve the list of games played, with links to more detailed information on each of those games

// no specific game has been selected
if(!$gameid){

// open the list of games in the "otherteam.txt" file (you could alternately use the date of the game as the comparitive file)

$file = fopen("basketball/otherteam.txt","r");

// read the contents of the file
$filecontents = fread($file,filesize($file));

// break apart the games to count them
$gamepieces = explode("/n",$filecontents);

// for each game, display a row with a link to that game info

for($i=0;$i
// we want to start the game number at one, not zero (PHP starts counting at 0)

$gameid=$i+1;

// the row with the game link (this assumes you are in the basketball folder)

$gameblock .="Game $gameid
";
}

}else{
// a game has been set, retrieve the game information (the function for this is described underneath)

// this part is a bit bulky, as you will need to open all of the games and sort through them (the last function will take care of this)

// the type of game and the teams playing
$gametype=readgameinfo("basketball","gametype.txt");
$otherteam=readgameinfo("basketball","otherteam.txt");

// open and break apart the different scores, by period
$periodsmarlins=readgameinfo("basketball","scoremarlins");

$periodpieces=explode("-",$periodsmarlins);

for($i=0;$i<4;$i++){
// add one again
$periodnumber = $i+1;

// prepare variable for variable variable
$period = "marlinsperiod$periodnumber";

// create a new variable called "marlinsperiod1", "marlinsperiod2", etc)
$$period = $periodpieces[$i];

// tally the total score
$opptotalscore = $opptotalscore + $periodpieces[$i];

}

// clear rogue variables
unset($periodpieces);

// do the exact same thing for the other team
$periodsopp=readgameinfo("basketball","scoreopp");

$periodpieces=explode("-",$periodsopp);

for($i=0;$i<4;$i++){
// add one again
$periodnumber = $i+1;

// prepare variable for variable variable
$period = "oppperiod$periodnumber";

// create a new variable called "oppsperiod1", "oppperiod2", etc)
$$period = $periodpieces[$i];

// tally the total score
$opptotalscore = $opptotalscore + $periodpieces[$i];

}

// open the scoring/rebounding leaders
$leadersmarlins = readgameinfo("basketball","leadersmarlins.txt");

// separate the leading scorer from the leading rebounder
$leaders = explode("-",$leadersmarlins);

$scorermarlins = $leaders[0];
$reboundermarlins = $leaders[1];

// same for the other theam
$leadersopp = readgameinfo("basketball","leadersopp.txt");

// separate the leading scorer from the leading rebounder
$leaders = explode("-",$leadersopp);

$scoreropp = $leaders[0];
$rebounderopp = $leaders[1];


// open your game notes
$gamenotes = readgameinfo("basketball","gamenotes.txt");


}



--

The last function, the one that opens the game information for display, looks like this:

--

function readgameinfo($sport,$filename,$gameid){

// open and read the specified file
$file = fopen("$sport/$filename.txt","r");

// read the contents of the file
$filecontents = fread($file,filesize($file));

// break apart the games to count them
$gamepieces = explode("/n",$filecontents);

// remember that we had to add 1 to the game number in the last function, now we have to subtract it back, as PHP starts counting at 0.

$gameid=$gameid-1;

// extract the info from the given file for the game you want

for($i=0;$i
// there will only be one row of game stats that you're looking for, then you can kill the function

$gameinfo = $gamepieces[$gameid];
exit();
}

}

// return the information you retrieved
return($gameinfo);
}

--

Your "games.php" page could look simply like this:

// do all of the PHP stuff above (the reading/menu part of the games) here
?>



Basketball Games



// no game shown, display menu of games
if(!$gameid){
print $gameblock

}else{
// specific game requested, display to your liking
?>

Game





Marlins -v-

Marlins:






First Period: Second Period: Third Period: Fourth Period: Final Score:





:






First Period: Second Period: Third Period: Fourth Period: Final Score:


Leading scorers:

, Marlins, ,

Leading rebounders:

, Marlins, ,

Coaches' remarks:









Put all your functions in a page called "functions.php", and for each sport you want to do, you need to put the following line at the top of the page:

require("functions.php");

This can get a little confusing because the folders may all be in different places.

There also may be a couple of minor mistakes that I didn't proof read.

It would have been easier and faster to do this all with a MySQL database, and there are better, cleaner ways to do this, but as far as what you need, this should be fine. Plus, I don't know how much PHP you know, so I didn't want to appear condescending.

If you need more help, let me know. At least now, if you go on a message board (like Devshed, PHPBuilder, etc), you have something to start with.

Good luck,

Sea
x

x
x x x
x x View the profile for Universal Sea x
x
x Unread post xUniversal Sea   - at 11:22 pm on Tuesday February 12 2002 x
x x
x Universal Sea i guess this help wasn't good enough.

for some reason i have responded to him a couple of times and am now in the middle of doing the whole thing 100% for him. He knows little about HTML and almost nothing about PHP.

a rule here, if there are any:

we will not do your work for you. if your work is too much for you and you think you can save your ass on message boards, maybe you should find a new line of work.

do what you love.
x
x
x x x
x x View the profile for Universal Sea x
x
x Unread post xxUniversal Sea   - at 12:25 am on Wednesday February 13 2002 x
x x
x Universal Sea i pulled a chris cosentino (the guy from who's book I started learning PHP) on him.

everything works up to the final function, which holds the key to everything, but he must figure it out himself.

he will appreciate it.
x
x
x x x
x x View the profile for Universal Sea x
x
^ ^ Back to top
x
< < return to Visual Stimulus, DOWNTUM
Logon to UM City or Apply for a UM City passport
x
x
City home | Login/Signup