Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- require_once "DataObject.class.php";
- class Member extends DataObject
- {
- protected $data = array(
- "id" => "" ,
- "username" => "" ,
- "password" => "" ,
- "firstName" => "" ,
- "lastName" => "" ,
- "joinDate" => "" ,
- "gender" => "" ,
- "favoriteGenre" => "" ,
- "emailAddress" => "" ,
- "otherInterests" => ""
- );
- private $_genre = array(
- "crime" => "Crime" ,
- "horror" => "Horror" ,
- "thriller" => "Thriller" ,
- "romance" => "Romance" ,
- "sciFi" => "Sci-Fi" ,
- "adventure" => "Adventure" ,
- "nonFiction" => "Non-Fiction"
- );
- public static function getMembers( $startRow , $numRows , $order )
- {
- $conn = parent::connect();
- $sql = "select SQL_CALC_FOUND_ROWS * FROM " . TBL_MEMBERS . " order by $order limit :startRow , :numRows";
- try
- {
- $st = $conn->prepare( $sql );
- $st->bindValue( ":startRow" , $startRow , PDO::PARAM_INT );
- $st->bindValue( ":numRows" , $numRows , PDO::PARAM_INT );
- $st->execute();
- $members = array();
- foreach( $st->fetchAll() as $row )
- {
- // var_dump($row);
- $members[] = new Member($row);
- }
- // var_dump($members);
- $st = $conn->query( "select found_rows() as totalRows" );
- $row = $st->fetch();
- parent::disconnect( $conn );
- return array( $members , $row['totalRows'] );
- }
- catch( PDOException $e)
- {
- parent::disconnect();
- die("Query failed " . $e->getMessage() );
- }
- }
- public static function getMember( $id )
- {
- $conn = parent::connect();
- $sql = "select * from " . TBL_MEMBERS . " where id = :id";
- try
- {
- $st = $conn->prepare( $sql );
- $st->bindValue( ":id" , $id , PDO::PARAM_INT );
- $st->execute();
- $row = $st->fetch();
- parent::disconnect( $conn );
- if( $row )
- {
- return new Member($row);
- }
- }
- catch(PDOException $e)
- {
- parent::disconnect();
- die("Query failed: " . $e->getMessage() );
- }
- }
- // start of getByUsername method
- public static function getByUsername( $username )
- {
- $conn = parent::connect();
- $sql = "SELECT * from " . TBL_MEMBERS . " where username = :username";
- try
- {
- $st = $conn->prepare( $sql );
- $st->bindValue(":username" , $username , PDO::PARAM_STR );
- $st->execute();
- $row = $st->fetch();
- parent::disconnect( $conn );
- if( $row ) return new Member($row);
- }
- catch( PDOException $e )
- {
- die( "Connection failed" . $e->getMessage() );
- }
- }
- // end of getByUsername method
- // --------------------------------------------------------------
- // start of getByEmailAddress method
- public static function getByEmailAddress( $emailAddress )
- {
- $conn = parent::connect();
- $sql = "select * from " . TBL_MEMBERS . " where emailAddress = :emailAddress";
- try
- {
- $st = $conn->prepare( $sql );
- $st->bindValue( ":emailAddress" , $emailAddress , PDO::PARAM_STR );
- $st->execute();
- $row = $st->fetch();
- parent::disconnect( $conn );
- if( $row ) return new Member( $row );
- }
- catch( PDOException $e )
- {
- parent::disconnect( $conn );
- die( "Query failed" . $e->getMessage() );
- }
- }
- // end of getByEmailAddress method
- // --------------------------------------------------------------
- public function getGenres()
- {
- return $this->_genre;
- }
- public function getGenderString()
- {
- return ( $this->data['gender'] == "f" ) ? "Female" : "Male";
- }
- public function getFavoriteGenreString()
- {
- return $this->_genre[ $this->data['favoriteGenre'] ];
- }
- // start of insert method
- // --------------------------------------------------------------
- public function insert()
- {
- $conn = parent::connect();
- $sql = "insert into " . TBL_MEMBERS . " (
- username,
- password,
- firstName,
- lastName,
- joinDate,
- gender,
- favoriteGenre,
- emailAddress,
- otherInterests
- ) values (
- :username ,
- password( :password ) ,
- :firstName ,
- :lastName ,
- :joinDate ,
- :gender ,
- :favoriteGenre ,
- :emailAddress ,
- :otherInterests
- )";
- try
- {
- $st = $conn->prepare( $sql );
- $st->bindValue( ":username" , $this->data["username"] , PDO::PARAM_STR );
- $st->bindValue( ":password" , $this->data["password"] , PDO::PARAM_STR );
- $st->bindValue( ":firstName" , $this->data["firstName"] , PDO::PARAM_STR );
- $st->bindValue( ":lastName" , $this->data["lastName"] , PDO::PARAM_STR );
- $st->bindValue( ":joinDate" , $this->data["joinDate"] , PDO::PARAM_STR );
- $st->bindValue( ":gender" , $this->data["gender"] , PDO::PARAM_STR );
- $st->bindValue( ":favoriteGenre" , $this->data["favoriteGenre"] , PDO::PARAM_STR );
- $st->bindValue( ":emailAddress" , $this->data["emailAddress"] , PDO::PARAM_STR );
- $st->bindValue( ":otherInterests" , $this->data["otherInterests"] , PDO::PARAM_STR );
- $st->execute();
- parent::disconnect( $conn );
- }
- catch( PDOException $e )
- {
- parent::disconnect( $conn );
- die( "Query failed" . $e->getMessage() );
- }
- }
- // end of insert method
- // --------------------------------------------------------------
- // start of method authenticate
- // --------------------------------------------------------------
- public function authenticate()
- {
- $conn = parent::connect();
- $sql = "select * from " . TBL_MEMBERS. " where username = :username and password = :password";
- try
- {
- $st = $conn->prepare( $sql );
- $st->bindValue( ":username" , $this->data['username'] , PDO::PARAM_STR ); // see from where it's coming
- $st->bindValue( ":password" , $this->data['password'] , PDO::PARAM_STR ); // see from where it's coming
- $st->execute();
- $row = $st->fetch();
- parent::disconnect();
- if( $row ) return new Member ( $row );
- }
- catch( PDOException $e )
- {
- die( "Query failed" . $e->getMessage() );
- }
- }
- // end of method authenticate
- // --------------------------------------------------------------
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement