derferman / movie-addict

Track the amount of IMDB Top 250 movies you have seen. For Facebook

This URL has Read+Write access

movie-addict / addict_database.php
100644 334 lines (272 sloc) 10.759 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
<?php
/*
Movie Addict - IMDB Top 250 List - Kyle Conroy
*/
 
/*
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
 
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
 
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses
/*******************************************************************************
* MySQL Database Class
*******************************************************************************
* Author: Micah Carrick
* Email: email@micahcarrick.com
* Website: http://www.micahcarrick.com
*
* File: db.class.php
* Version: 1.0.4
* Copyright: (c) 2005 - Micah Carrick
* You are free to use, distribute, and modify this software
* under the terms of the GNU General Public License. See the
* included license.txt file.
*
*******************************************************************************
* VERION HISTORY:
*
* v1.0.4 [07.06.2005] - Added ability to add NULL values in update_array
* and insert_array functions.
* v1.0.3 [05.10.2005] - Fixed bug in update_array funciton
* v1.0.2 [05.09.2005] - Fixed select_one function for queries returning 0
* v1.0.1 [04.28.2005] - Fixed bug in select_one function (returned null!)
* v1.0.0 [04.17.2005] - Initial Version
*
*******************************************************************************
* DESCRIPTION:
*
* This class aids in MySQL database connectivity. It was written with
* my specific needs in mind. It simplifies the database tasks I most
* often need to do thus reducing redundant code. It is also written
* with the mindset to provide easy means of debugging erronous sql and
* data during the development phase of a web application.
*
* The future may call for adding a slew of other features, however, at
* this point in time it just has what I often need. I'm not trying to
* re-write phpMyAdmin or anything. :) Hope you find it useful.
*
* A screenshot and sample script can be found on my website.
*
*******************************************************************************
*/
require_once('config.php');
 
class AddictDatabase {
 
   var $host; // mySQL host to connect to
   var $user; // mySQL user name
   var $pw; // mySQL password
   var $db; // mySQL database to select
 
   var $db_link; // current/last database link identifier
   var $auto_slashes; // the class will add/strip slashes when it can
   
   function __construct($db = null) {
   
      // class constructor. Initializations here.
      // Setup your own default values for connecting to the database here. You
      // can also set these values in the connect() function and using
      // the select_database() function.
      global $hostname;
      global $username;
      global $password;
      global $database;
      
      $this->host = $hostname;
      $this->user = $username;
      $this->pw = $password;
      if(!$db)
        $this->db = $database;
      else
        $this->db = $db;
      $this->connect();
      $this->select_db();
   }
 
   function connect($host='', $user='', $pw='', $db='', $persistant=true) {
 
      // Opens a connection to MySQL and selects the database. If any of the
      // function's parameter's are set, we want to update the class variables.
      // If they are NOT set, then we're giong to use the currently existing
      // class variables.
      // Returns true if successful, false if there is failure.
      
      if (!empty($host)) $this->host = $host;
      if (!empty($user)) $this->user = $user;
      if (!empty($pw)) $this->pw = $pw;
 
 
      // Establish the connection.
      if ($persistant)
         $this->db_link = mysql_pconnect($this->host, $this->user, $this->pw);
      else
         $this->db_link = mysql_connect($this->host, $this->user, $this->pw);
 
      // Check for an error establishing a connection
      if (!$this->db_link) {
         $this->last_error = mysql_error();
         return false;
      }
  
      // Select the database
      if (!$this->select_db($db)) return false;
 
      return $this->db_link; // success
   }
 
   function select_db($db='') {
 
      // Selects the database for use. If the function's $db parameter is
      // passed to the function then the class variable will be updated.
 
      if (!empty($db)) $this->db = $db;
      
      if (!mysql_select_db($this->db)) {
         $this->last_error = mysql_error();
         return false;
      }
 
      return true;
   }
   
   function query($sql) {
      
      // Performs an SQL query and returns the result pointer or false
      // if there is an error.
      
      $r = mysql_query($sql);
      if (!$r) {
print("MySQL Error: " . mysql_error() . "<br>");
}
      return $r;
   }
   
    /* User Functions */
function getUsers(){
$row = $this->query("SELECT * FROM users");
$users = array();
while($result = mysql_fetch_array($row)){
$user = array();
$user['id'] = $result['user_id'];
$user['count'] = $result['seen'];
$users[$result['user_id']] = $user;
}
return $users;
}
 
function addUser($user_id){
return $this->query("INSERT INTO users (user_id) VALUES($user_id)");
}
 
function deleteUser($user_id){
return $this->query("DELETE FROM users WHERE user_id = $user_id");
}
 
function getUser($user_id){
$user_id = (int) $user_id;
$row = $this->query("SELECT * FROM users WHERE user_id = $user_id");
$result = mysql_fetch_array($row);
if($result){
$user = array();
$user['id'] = $result['user_id'];
$user['count'] = $result['seen'];
$user['percent'] = $result['percent'];
} else
$user = false;
return $user;
}
   
/* User Movie Functions */
function getUserMovies($user_id){
$row = $this->query("SELECT * FROM users_movies WHERE user_id = $user_id");
$movies = array();
while($result = mysql_fetch_array($row)){
$movies[] = $result['movie_id'];
}
return $movies;
}
 
function getUserMoviesRanked($user_id){
$row = $this->query("SELECT users_movies.movie_id FROM users_movies JOIN movies ON users_movies.movie_id = movies.movie_id WHERE user_id = $user_id AND (imdb_pos IS NOT NULL OR afi_pos IS NOT NULL)");
$movies = array();
while($result = mysql_fetch_array($row)){
$movies[] = $result['movie_id'];
}
return $movies;
}
 
function getMovieUsers($movie_id){
$row = $this->query("SELECT * FROM users_movies WHERE movie_id = $movie_id");
return $movies;
}
 
function updateUserMovieCount($user_id){
$movies = self::getUserMoviesRanked($user_id);
$total_count = count(self::getRankedMovies());
$user_count = count($movies);
$percent = round($user_count / $total_count * 100, 2);
return $this->query("UPDATE users SET seen = $user_count, percent = $percent WHERE user_id = $user_id");
}
 
function addUserMovie($user_id, $movie_id){
return $this->query("INSERT INTO users_movies (user_id, movie_id) VALUES($user_id,$movie_id) ON DUPLICATE KEY UPDATE user_id=user_id");
}
 
function deleteUserMovie($user_id, $movie_id){
return $this->query("DELETE FROM users_movies WHERE movie_id = $movie_id AND user_id = $user_id");
}
 
/* Movie Functions */
function addMovie($movie_id, $title, $imdb_rank=NULL, $afi_rank=NULL){
if(!$imdb_rank)
$imdb_rank = "NULL";
if(!$afi_rank)
$afi_rank = "NULL";
return $this->query("INSERT INTO movies (movie_id,title,imdb_pos,afi_pos) VALUES($movie_id, '$title', $imdb_rank, $afi_rank)");
}
 
function updateMovieTitle($movie_id, $title){
return $this->query("UPDATE movies SET title = \"$title\" WHERE movie_id = $movie_id");
}
 
function resetIMDBRank(){
return $this->query("UPDATE movies SET imdb_pos = NULL");
}
 
function resetAFIRank(){
return $this->query("UPDATE movies SET afi_pos = NULL");
}
 
function updateIMDBRank($movie_id, $rank){
return $this->query("UPDATE movies SET imdb_pos = $rank WHERE movie_id = $movie_id");
}
 
function updateAFIRank($movie_id, $rank){
return $this->query("UPDATE movies SET afi_pos = $rank WHERE movie_id = $movie_id");
}
 
function updateNetflixInstant($movie_id, $bool){
if($bool)
$value = 1;
else
$value = 0;
       return $this->query("UPDATE movies SET instant = $value WHERE movie_id = $movie_id");
}
 
    function updateNetflixId($movie_id, $netflix_id){
return $this->query("UPDATE movies SET netflix_id = $netflix_id WHERE movie_id = $movie_id");
}
 
    function resetNetflixInstant(){
return $this->query("UPDATE movies SET instant = 0");
}
 
function deleteMovie($movie_id){
return $this->query("DELETE FROM movies WHERE movie_id = $movie_id");
}
 
function getMovie($movie_id){
$row = $this->query("SELECT * FROM movies WHERE movie_id = $movie_id");
$result = mysql_fetch_array($row);
if($result){
$movie = array();
$movie['id'] = $result['movie_id'];
$movie['netflix_id'] = $result['netflix_id'];
$movie['title'] = $result['title'];
$movie['imdb_rank'] = $result['imdb_pos'];
$movie['afi_rank'] = $result['afi_pos'];
$movie['instant'] = $result['instant'];
} else
$movie = false;
return $movie;
}
 
function getMovies(){
return self::movieQuery("SELECT * FROM movies");
}
 
function getRankedMovies(){
return self::movieQuery("SELECT * FROM movies WHERE imdb_pos IS NOT NULL OR afi_pos IS NOT NULL");
}
 
function getIMDBMovies(){
return self::movieQuery("SELECT * FROM movies WHERE imdb_pos IS NOT NULL ORDER BY imdb_pos");
}
 
function getAFIMovies(){
return self::movieQuery("SELECT * FROM movies WHERE afi_pos IS NOT NULL ORDER BY afi_pos");
}
 
private function movieQuery($sql){
$row = $this->query($sql);
$movies = array();
while($result = mysql_fetch_array($row)){
$movie = array('id' => $result['movie_id'],
'title' => $result['title'],
'imdb_rank' => $result['imdb_pos'],
'afi_rank' => $result['afi_pos'],
'instant' => $result['instant'],
'netflix_id' => $result['netflix_id']);
$movies[$result['movie_id']] = $movie;
}
return $movies;
}
   
}
 
?>