Menu

[886e52]: / myCookbook.php  Maximize  Restore  History

Download this file

237 lines (184 with data), 6.3 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
<?php
require_once('myCookbook/Recipe.php');
require_once('config.php');
/**
* Gets a list of category types stored in the database.
*/
function mycookbook_get_categories( ) {
$query = "show fields from recipes";
$result = mycookbook_internal_run_query($query);
$row = mysql_fetch_array($result);
while ($row['Field'] != "category") {
$row = mysql_fetch_array($result);
}
// Extract a list of types from the enumerated "Types" field.
$enum = str_replace('enum(', '', $row['Type']);
$enum = ereg_replace('\\)$', '', $enum);
$enum = explode('\',\'', substr($enum, 1, -1));
sort($enum);
reset($enum);
return $enum;
}
function mycookbook_add_category($newCategory) {
if ($newCategory == "") {
return;
}
$categories = mycookbook_get_categories( );
$query = 'ALTER TABLE `recipes` CHANGE `category` `category` ENUM(';
foreach ($categories as $category) {
$query .= '\''.$category.'\',';
}
$query .= '\''.$newCategory.'\') NULL DEFAULT NULL';
mycookbook_internal_run_query($query);
}
/**
* Gets a list of cuisine types stored in the database.
*/
function mycookbook_get_cuisines( ) {
$query = "show fields from recipes";
$result = mycookbook_internal_run_query($query);
$row = mysql_fetch_array($result);
while ($row['Field'] != "cuisine") {
$row = mysql_fetch_array($result);
}
// Extract a list of types from the enumerated "Types" field.
$enum = str_replace('enum(', '', $row['Type']);
$enum = ereg_replace('\\)$', '', $enum);
$enum = explode('\',\'', substr($enum, 1, -1));
sort($enum);
reset($enum);
return $enum;
}
/**
* Gets the recipe of a particular name
*/
function mycookbook_get_recipe( $recipe_uid ) {
$query = 'select * from recipes where uid = '.$recipe_uid;
$result = mycookbook_internal_run_query($query);
$row = mysql_fetch_array($result);
return new Recipe($row);
}
/**
* Gets a random recipe
*/
function mycookbook_get_featured_recipe() {
$query = 'select * from recipes order by rand() limit 1';
$result = mycookbook_internal_run_query($query);
$row = mysql_fetch_array($result);
return new Recipe( $row );
}
function mycookbook_rate_recipe($uid, $rating) {
$query = 'UPDATE `recipes` SET `rating` = \''.$rating.'\' WHERE `uid` = '.$uid.' LIMIT 1';
mycookbook_internal_run_query($query);
}
/**
* Return an array of results from a search
*/
function mycookbook_do_search( $recipe_category, $recipe_title, $recipe_source, $recipe_contains, $recipe_serves ) {
$query = "select * from recipes";
if ( $recipe_category != "" )
$query .= " where category = '".$recipe_category."' and";
if ( $recipe_title != "" )
$query .= " where title like '%".$recipe_title."%' and";
if ( $recipe_source != "" )
$query .= " where source like '%".$recipe_source."%' and";
if ( $recipe_contains != "" )
$query .= " where ingredients like '%".$recipe_contains."%' and";
if ( $recipe_serves != "" )
$query .= " where servings = '".$recipe_serves."'";
if ( substr($query, -4) == " and" )
$query = substr($query, 0, -4);
$query .= " order by category";
$result = mycookbook_internal_run_query($query);
$num_rows = mysql_num_rows($result);
for ( $i = 0; $i < $num_rows; $i++ ) {
$row = mysql_fetch_array($result);
$search_results[$i] = $row;
}
return $search_results;
}
function mycookbook_add_recipe( $title, $servings, $category, $preptime, $cooktime, $cuisine, $rating, $source, $ingredients, $instructions ) {
$query = 'INSERT INTO `recipes` (`uid`, `title`, `servings`, `category`, `preptime`, `cooktime`, `cuisine`, `rating`, `source`, `ingredients`, `instructions`, `image`) VALUES (\'\', ';
$query .= '\''.$title.'\', ';
if ($servings != "") {
$query .= '\''.$servings.'\', ';
} else {
$query .= 'NULL, ';
}
if ($category != "") {
$query .= '\''.$category.'\', ';
} else {
$query .= 'NULL, ';
}
if ($preptime != "") {
$query .= '\''.$preptime.'\', ';
} else {
$query .= 'NULL, ';
}
if ($cooktime != "") {
$query .= '\''.$cooktime.'\', ';
} else {
$query .= 'NULL, ';
}
if ($cuisine != "") {
$query .= '\''.$cuisine.'\', ';
} else {
$query .= 'NULL, ';
}
if ($rating != "") {
$query .= '\''.$rating.'\', ';
} else {
$query .= 'NULL, ';
}
if ($source != "") {
$query .= '\''.$source.'\', ';
} else {
$query .= 'NULL, ';
}
$query .= '\''.$ingredients.'\', ';
$query .= '\''.$instructions.'\', ';
$query .= 'NULL'; // Image not implemented
$query .= ')';
mycookbook_internal_run_query($query);
}
/**
* Setup function that will install the database to a MySQL server
*/
function mycookbook_install_database() {
require_once('config.php');
if ($config_done == "no") {
return 'fail';
}
$query = 'CREATE TABLE IF NOT EXISTS `recipes` (';
$query .= '`uid` int(11) NOT NULL auto_increment,';
$query .= '`title` varchar(50) NOT NULL,';
$query .= '`servings` smallint(6) default NULL,';
$query .= '`category` enum(\'Entree\',\'Salad\',\'Dessert\',\'Soup\',\'Breakfast\') default NULL,';
$query .= '`preptime` varchar(20) default NULL,';
$query .= '`cooktime` varchar(20) default NULL,';
$query .= '`cuisine` enum(\'Asian\',\'Mexican\',\'American\',\'Italian\',\'Greek\',\'German\',\'French\',\'British\') default NULL,';
$query .= '`rating` tinyint(4) default NULL,';
$query .= '`source` varchar(50) default NULL,';
$query .= '`ingredients` text NOT NULL,';
$query .= '`instructions` text NOT NULL,';
$query .= '`image` binary(1) default NULL,';
$query .= 'PRIMARY KEY (`uid`)';
$query .= ') AUTO_INCREMENT=4 ;';
mycookbook_internal_run_query($query);
return 'ok';
}
/**
* INTERNAL method for the purpose of centralizing code
*/
function mycookbook_internal_run_query($query) {
require('config.php');
// Run the query to add a recipe
$connection = mysql_connect($sql_server, $sql_user, $sql_pass)
or die("Could not connect to server");
$db = mysql_select_db($sql_database, $connection)
or die("Could not select database");
$result = mysql_query($query, $connection)
or die("Query Failed: ".$query);
return $result;
}
?>
MongoDB Logo MongoDB