marcusfaye87 Posted May 30, 2008 Share Posted May 30, 2008 Ok so I've created a class that handles the database operations. but it gets tricky in the SELECT part of it. I'm just wondering if you pro's could look through my method and give me some pointers on how to improve it, because it seems a little far fetched. thanks in advance EDIT: thought I might add this to help you figure out what I'm trying to pull $search contains nested arrays [1] contains the field and value e.g: $search[1]['username'] = "marcusfaye87"; [2] contains the AND or OR operator also in an array <?php /** SELECT database information */ public function select ($components, $table, $search, $order, $order_mode, $limit) { // Check $table if (!$table) { // Return error } else { // Check $components if (!$components) { $components = '*'; } // Setup the query $query = "SELECT " . implode (', ', $components) . " FROM " . $table; // Check $search if (is_array($search) && is_array($search[1]) && is_array($search[2])) { $query = $query . " WHERE"; if (count ($search[1]) == 1) { foreach ($search[1] as $key => $value) { $query = $query . " `" . $key . "` = '" . $value . "' "; } } elseif (count ($search[1]) == (count ($search[2]) - 1)) { foreach ($search[1] as $key => $value) { $query = $query . " `" . $key . "` = '" . $value . "' "; foreach ($search[2] as $value2) { $query = $query . $value2; } } } else { // Return error } } // Check $order and $order_mode if (!$order) { $order = 'id'; } if (!$order_mode || $order_mode != 'ASC' || $order_mode !='DESC') { $order_mode = "DESC"; } $query = $query . " ORDER BY " . $order . " " . $order_mode; // Check $limit if ($limit && is_int ($limit)) { $query = $query . " LIMIT" . $limit; } } } ?> Link to comment https://forums.phpfreaks.com/topic/107937-db_opersations-class-select-method-need-some-advice/ Share on other sites More sharing options...
marcusfaye87 Posted May 30, 2008 Author Share Posted May 30, 2008 Sorry guys, overlooked something, here is the altered code that actually does work. <?php /** SELECT database information */ public function select ($components, $table, $search, $order, $order_mode, $limit) { // Check $table if (!$table) { // Return error } else { // Check $components if (!$components) { $components = '*'; } // Setup the query $query = "SELECT " . implode (', ', $components) . " FROM " . $table; // Check $search if (is_array($search) && is_array($search[1]) && is_array($search[2])) { $query = $query . " WHERE"; if (count ($search[1]) == 1) { foreach ($search[1] as $key => $value) { $query = $query . " `" . $key . "` = '" . $value . "' "; } } elseif (count ($search[1]) == (count ($search[2]) - 1)) { $loop = 0; $count = count ($search[2]) - 1; foreach ($search[1] as $key => $value) { $query = $query . " `" . $key . "` = '" . $value . "' "; $loop++; if ($loop <= $count) { $query = $query . $search2[$loop]; } } } else { // Return error } } // Check $order and $order_mode if (!$order) { $order = 'id'; } if (!$order_mode || $order_mode != 'ASC' || $order_mode !='DESC') { $order_mode = "DESC"; } $query = $query . " ORDER BY " . $order . " " . $order_mode; // Check $limit if ($limit && is_int ($limit)) { $query = $query . " LIMIT" . $limit; } } } ?> Link to comment https://forums.phpfreaks.com/topic/107937-db_opersations-class-select-method-need-some-advice/#findComment-553284 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.