Advertisement
Achilles

Untitled

Jun 25th, 2013
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.15 KB | None | 0 0
  1. <?php
  2.  
  3. require_once "DataObject.class.php";
  4.  
  5. class Member extends DataObject
  6. {
  7. protected $data = array(
  8. "id" => "" ,
  9. "username" => "" ,
  10. "password" => "" ,
  11. "firstName" => "" ,
  12. "lastName" => "" ,
  13. "joinDate" => "" ,
  14. "gender" => "" ,
  15. "favoriteGenre" => "" ,
  16. "emailAddress" => "" ,
  17. "otherInterests" => ""
  18. );
  19.  
  20. private $_genre = array(
  21. "crime" => "Crime" ,
  22. "horror" => "Horror" ,
  23. "thriller" => "Thriller" ,
  24. "romance" => "Romance" ,
  25. "sciFi" => "Sci-Fi" ,
  26. "adventure" => "Adventure" ,
  27. "nonFiction" => "Non-Fiction"
  28. );
  29.  
  30. public static function getMembers( $startRow , $numRows , $order )
  31. {
  32. $conn = parent::connect();
  33. $sql = "select SQL_CALC_FOUND_ROWS * FROM " . TBL_MEMBERS . " order by $order limit :startRow , :numRows";
  34.  
  35. try
  36. {
  37. $st = $conn->prepare( $sql );
  38. $st->bindValue( ":startRow" , $startRow , PDO::PARAM_INT );
  39. $st->bindValue( ":numRows" , $numRows , PDO::PARAM_INT );
  40. $st->execute();
  41. $members = array();
  42. foreach( $st->fetchAll() as $row )
  43. {
  44. // var_dump($row);
  45. $members[] = new Member($row);
  46. }
  47. // var_dump($members);
  48. $st = $conn->query( "select found_rows() as totalRows" );
  49. $row = $st->fetch();
  50. parent::disconnect( $conn );
  51. return array( $members , $row['totalRows'] );
  52. }
  53.  
  54. catch( PDOException $e)
  55. {
  56. parent::disconnect();
  57. die("Query failed " . $e->getMessage() );
  58. }
  59. }
  60.  
  61. public static function getMember( $id )
  62. {
  63. $conn = parent::connect();
  64. $sql = "select * from " . TBL_MEMBERS . " where id = :id";
  65.  
  66. try
  67. {
  68. $st = $conn->prepare( $sql );
  69. $st->bindValue( ":id" , $id , PDO::PARAM_INT );
  70. $st->execute();
  71. $row = $st->fetch();
  72. parent::disconnect( $conn );
  73. if( $row )
  74. {
  75. return new Member($row);
  76. }
  77. }
  78.  
  79. catch(PDOException $e)
  80. {
  81. parent::disconnect();
  82. die("Query failed: " . $e->getMessage() );
  83. }
  84. }
  85.  
  86. // start of getByUsername method
  87.  
  88. public static function getByUsername( $username )
  89. {
  90. $conn = parent::connect();
  91. $sql = "SELECT * from " . TBL_MEMBERS . " where username = :username";
  92.  
  93. try
  94. {
  95. $st = $conn->prepare( $sql );
  96. $st->bindValue(":username" , $username , PDO::PARAM_STR );
  97. $st->execute();
  98. $row = $st->fetch();
  99. parent::disconnect( $conn );
  100. if( $row ) return new Member($row);
  101. }
  102.  
  103. catch( PDOException $e )
  104. {
  105.  
  106. die( "Connection failed" . $e->getMessage() );
  107. }
  108. }
  109.  
  110. // end of getByUsername method
  111.  
  112. // --------------------------------------------------------------
  113.  
  114. // start of getByEmailAddress method
  115.  
  116. public static function getByEmailAddress( $emailAddress )
  117. {
  118. $conn = parent::connect();
  119. $sql = "select * from " . TBL_MEMBERS . " where emailAddress = :emailAddress";
  120.  
  121. try
  122. {
  123. $st = $conn->prepare( $sql );
  124. $st->bindValue( ":emailAddress" , $emailAddress , PDO::PARAM_STR );
  125. $st->execute();
  126. $row = $st->fetch();
  127. parent::disconnect( $conn );
  128. if( $row ) return new Member( $row );
  129. }
  130.  
  131. catch( PDOException $e )
  132. {
  133. parent::disconnect( $conn );
  134. die( "Query failed" . $e->getMessage() );
  135. }
  136. }
  137.  
  138. // end of getByEmailAddress method
  139.  
  140. // --------------------------------------------------------------
  141.  
  142. public function getGenres()
  143. {
  144. return $this->_genre;
  145. }
  146.  
  147.  
  148. public function getGenderString()
  149. {
  150. return ( $this->data['gender'] == "f" ) ? "Female" : "Male";
  151. }
  152.  
  153. public function getFavoriteGenreString()
  154. {
  155. return $this->_genre[ $this->data['favoriteGenre'] ];
  156. }
  157.  
  158. // start of insert method
  159.  
  160. // --------------------------------------------------------------
  161.  
  162. public function insert()
  163. {
  164. $conn = parent::connect();
  165. $sql = "insert into " . TBL_MEMBERS . " (
  166. username,
  167. password,
  168. firstName,
  169. lastName,
  170. joinDate,
  171. gender,
  172. favoriteGenre,
  173. emailAddress,
  174. otherInterests
  175. ) values (
  176. :username ,
  177. password( :password ) ,
  178. :firstName ,
  179. :lastName ,
  180. :joinDate ,
  181. :gender ,
  182. :favoriteGenre ,
  183. :emailAddress ,
  184. :otherInterests
  185. )";
  186.  
  187. try
  188. {
  189. $st = $conn->prepare( $sql );
  190. $st->bindValue( ":username" , $this->data["username"] , PDO::PARAM_STR );
  191. $st->bindValue( ":password" , $this->data["password"] , PDO::PARAM_STR );
  192. $st->bindValue( ":firstName" , $this->data["firstName"] , PDO::PARAM_STR );
  193. $st->bindValue( ":lastName" , $this->data["lastName"] , PDO::PARAM_STR );
  194. $st->bindValue( ":joinDate" , $this->data["joinDate"] , PDO::PARAM_STR );
  195. $st->bindValue( ":gender" , $this->data["gender"] , PDO::PARAM_STR );
  196. $st->bindValue( ":favoriteGenre" , $this->data["favoriteGenre"] , PDO::PARAM_STR );
  197. $st->bindValue( ":emailAddress" , $this->data["emailAddress"] , PDO::PARAM_STR );
  198. $st->bindValue( ":otherInterests" , $this->data["otherInterests"] , PDO::PARAM_STR );
  199. $st->execute();
  200. parent::disconnect( $conn );
  201. }
  202.  
  203. catch( PDOException $e )
  204. {
  205. parent::disconnect( $conn );
  206. die( "Query failed" . $e->getMessage() );
  207. }
  208.  
  209. }
  210.  
  211. // end of insert method
  212.  
  213. // --------------------------------------------------------------
  214.  
  215.  
  216.  
  217. // start of method authenticate
  218.  
  219. // --------------------------------------------------------------
  220.  
  221. public function authenticate()
  222. {
  223. $conn = parent::connect();
  224. $sql = "select * from " . TBL_MEMBERS. " where username = :username and password = :password";
  225.  
  226. try
  227. {
  228. $st = $conn->prepare( $sql );
  229. $st->bindValue( ":username" , $this->data['username'] , PDO::PARAM_STR ); // see from where it's coming
  230. $st->bindValue( ":password" , $this->data['password'] , PDO::PARAM_STR ); // see from where it's coming
  231. $st->execute();
  232. $row = $st->fetch();
  233. parent::disconnect();
  234. if( $row ) return new Member ( $row );
  235. }
  236.  
  237. catch( PDOException $e )
  238. {
  239. die( "Query failed" . $e->getMessage() );
  240. }
  241. }
  242.  
  243.  
  244. // end of method authenticate
  245.  
  246. // --------------------------------------------------------------
  247.  
  248. }
  249.  
  250. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement