Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- session_start(); ?>
- <html>
- <?php
- //class-test1.php
- $data = $_POST;
- $sess = $_SESSION;
- echo '
- <form action="lab_3.php" method="post">
- Player Name: <input type="text" name="name"><br />
- Player Team: <input type="text" name="team"><br />
- Touchdowns: <input type="text" name="touchdowns"><br />
- <input type="submit" name="go">
- </form>
- ';
- if(isset($data['go'])){
- if(isset($sess['ball_players'])){
- array_push($sess['ball_players'], new BallPlayer($data['name'], $data['team'], $data['touchdowns']));
- if(!in_array($data['team'], $sess['active_teams'], True)){
- array_push($sess['active_teams'], $data['team']);
- }
- if(!in_array($data['name'], $sess['player_names'], True)){
- array_push($sess['player_names'], $data['name']);
- }
- $sess['total_touchdowns'] += (int)$data['touchdowns'];
- echo 'Active Teams: '.implode(", ", $sess['active_teams']);
- echo 'Current Roster: '.implode(", ", $sess['player_names']);
- echo 'Total Touchdowns: '.(string)$sess['total_touchdowns'];
- }
- else{
- $sess['ball_players'] = array(new BallPlayer($data['name'], $data['team'], $data['touchdowns']));
- $sess['total_touchdowns'] = (int)$data['touchdowns'];
- $sess['active_teams'] = array($data['team']);
- $sess['player_names'] = array($data['name']);
- array_push($_SESSION, $currentRoster, $activeTeams, $totalTouchdowns);
- }
- }
- class BallPlayer
- {
- public $name = '';
- public $team = '';
- public $touchdowns = '';
- // Magic methods are gross. And I think explicit constructors are better
- function __construct($name, $team, $touchdowns)
- {
- $this->name = $name;
- $this->team = $team;
- $this->touchdowns = $touchdowns;
- }
- function __toString()
- {
- $myReturn = '';
- $attributes = get_object_vars($this);
- foreach($attributes as $key => $value)
- $myReturn .= $key.': '.$value;
- return $myReturn;
- }
- } ?>
- <body>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement