Advertisement
Guest User

Untitled

a guest
Jan 27th, 2015
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.93 KB | None | 0 0
  1. <?php
  2. session_start(); ?>
  3. <html>
  4. <?php
  5. //class-test1.php
  6. $data = $_POST;
  7. $sess = $_SESSION;
  8. echo '
  9.  <form action="lab_3.php" method="post">
  10.  Player Name: <input type="text" name="name"><br />
  11.  Player Team: <input type="text" name="team"><br />
  12.  Touchdowns: <input type="text" name="touchdowns"><br />
  13.  <input type="submit" name="go">
  14.  </form>
  15. ';
  16. if(isset($data['go'])){
  17.   if(isset($sess['ball_players'])){
  18.     array_push($sess['ball_players'], new BallPlayer($data['name'], $data['team'], $data['touchdowns']));
  19.  
  20.     if(!in_array($data['team'], $sess['active_teams'], True)){
  21.       array_push($sess['active_teams'], $data['team']);
  22.     }
  23.  
  24.     if(!in_array($data['name'], $sess['player_names'], True)){
  25.       array_push($sess['player_names'], $data['name']);
  26.     }
  27.  
  28.     $sess['total_touchdowns'] += (int)$data['touchdowns'];
  29.     echo 'Active Teams: '.implode(", ", $sess['active_teams']);
  30.     echo 'Current Roster: '.implode(", ", $sess['player_names']);
  31.     echo 'Total Touchdowns: '.(string)$sess['total_touchdowns'];
  32.   }
  33.   else{
  34.     $sess['ball_players'] = array(new BallPlayer($data['name'], $data['team'], $data['touchdowns']));
  35.     $sess['total_touchdowns'] = (int)$data['touchdowns'];
  36.     $sess['active_teams'] = array($data['team']);
  37.     $sess['player_names'] = array($data['name']);
  38.     array_push($_SESSION, $currentRoster, $activeTeams, $totalTouchdowns);
  39.   }
  40. }
  41.  
  42. class BallPlayer
  43. {
  44.   public $name = '';
  45.   public $team = '';
  46.   public $touchdowns = '';
  47.  
  48.   // Magic methods are gross. And I think explicit constructors are better
  49.   function __construct($name, $team, $touchdowns)
  50.   {
  51.     $this->name = $name;
  52.     $this->team = $team;
  53.     $this->touchdowns = $touchdowns;
  54.   }
  55.  
  56.   function __toString()
  57.   {
  58.     $myReturn = '';
  59.     $attributes = get_object_vars($this);
  60.     foreach($attributes as $key => $value)
  61.       $myReturn .= $key.': '.$value;
  62.     return $myReturn;
  63.   }
  64. } ?>
  65. <body>
  66. </body>
  67. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement