Created
August 6, 2012 17:10
-
-
Save azrulharis/3276717 to your computer and use it in GitHub Desktop.
Php Crud Abstraction Layer using array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Crud class | |
* | |
* LICENSE FREE TO USE WITHOUT ANY WARRANTY | |
* How to use: | |
* Create class name extends Crud or change private method to public. | |
* | |
*/ | |
class Crud { | |
private $result = array(); | |
/* | |
* set to your database name, etc | |
* | |
*/ | |
public function __construct() { | |
} | |
/* | |
* return $this->insert('table', array('column1' => $_POST['field'])); | |
* | |
*/ | |
protected function insert($table, $data) { | |
$insert = sprintf('INSERT INTO %s (%s) VALUES ("%s")', $table, | |
implode(', ', array_map('mysql_escape_string', array_keys($data))), | |
implode('", "',array_map('mysql_escape_string', $data))); | |
if(mysql_query($insert)) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
/* | |
* $this->select('table', array('id', 'username',), array('id' => $id)); | |
* return $this->getResult(); | |
* select id, username from table where id=$id | |
*/ | |
protected function select($table, $column = NULL, $data=NULL) { | |
if(!empty($data)) { | |
$query = "SELECT ".implode(', ', $column)." FROM `$table`" . $this->where_list($data); | |
} else { | |
$query = "SELECT * FROM `$table`" . $this->where_list($data); | |
} | |
$query = mysql_query($query); | |
if($query) { | |
$this->numResults = mysql_num_rows($query); | |
for($i = 0; $i < $this->numResults; $i++) { | |
$r = mysql_fetch_array($query); | |
$key = array_keys($r); | |
for($x = 0; $x < count($key); $x++) { | |
// Sanitizes keys so only alphavalues are allowed | |
if(!is_int($key[$x])) { | |
if(mysql_num_rows($query) > 0) | |
$this->result[$i][$key[$x]] = htmlentities($r[$key[$x]]); | |
else if(mysql_num_rows($query) < 1) | |
$this->result = null; | |
else | |
$this->result[$key[$x]] = htmlentities($r[$key[$x]]); | |
} | |
} | |
} | |
return true; | |
} | |
else | |
{ | |
return false; | |
} | |
} | |
/* | |
* Returns the result set | |
*/ | |
protected function getResult() { | |
return $this->result; | |
} | |
protected function doCount($table, $data = array()) { | |
$query = mysql_query("SELECT COUNT(id) FROM `$table`" . $this->where_list($data)); | |
list($row) = mysql_fetch_row($query); | |
return $row; | |
} | |
protected function doUpdate($table, $data = array(), $where = array()) { | |
foreach($data as $key => $value) { | |
$query = "UPDATE $table SET ". $this->column_list($data) . $this->where_list($where); | |
return mysql_query($query); | |
} | |
} | |
private function column_list($conditions = array()) { | |
$output = " SET "; | |
foreach((array) $conditions as $column => $value) { | |
//If the value is an aray it must be an IN clause | |
if(is_array($value)) { | |
$output .= "`".$column."`". "'$value'"; | |
} else { | |
$output .= "`".$column."`". " = " | |
. ($value == '?' ? $value : "'$value'"). ", "; | |
} | |
} | |
return rtrim($output, ", "); | |
} | |
private function where_list($conditions = array()) { | |
$output = " WHERE "; | |
foreach($conditions as $column => $value) { | |
//If the value is an aray it must be an IN clause | |
if(is_array($value)) { | |
$output .= $column. "'$value'"; | |
} else { | |
$output .= $column. " = " | |
. ($value == '?' ? $value : "'$value'"). " AND "; | |
} | |
} | |
return rtrim($output, " AND "); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment