What is the best way to create Update Method using PHP OOP -
i have thinking of past hour, unable understand best way add update
, insert
feature using class in php.
i have employee
table has around 5 columns, have created different properties in employees
class.
now unable understand set these values, in __costructor()
, in add_employees()
method or other way..
if set them in __constructor()
user must remember , follow same pattern in writing arguments followed in writing parameters.
i unable understand right way or should use different approach this.
i have searched on net, , found complex examples, people taking argument in form of array, separating it, add loop in , put database.
class employees(){ public $db; public $conn; public $emp_id; public $first_name; public $last_name; public $gender; public $added_by; public function __construct(){ $this->db= new databaseconnection('localhost', 'root', '', 'employeesmanagement'); $this->conn= $this->db->connectdb(); } public function get_employees(){ } public function add_employees(){ }
i've written class before, things related database things. here class
/** * class: database * * description: class deals database connection * across website. if class needs use database * has extends one. * * @author: andre ferraz * @copyright: ^ * @version: 2.0 */ class database { /** * holds class object instance * * @var object * @access: private * @static */ private static $_instace; /** * holds pdo object * * @var pdo * @access: private */ private $_pdo; /** * used keep track of how many columns * has been found! * * @var int * @access: private */ private $_count; /** * holds data database * * @var array * @access: private */ private $_results = array(); /** * description: instantiates pdo object * * @access: protected */ protected function __construct() { $host = config::get("database:host"); $user = config::get("database:username"); $pass = config::get("database:password"); $dbna = config::get("database:dbname"); try { $this->_pdo = new pdo("mysql:dbname=".$dbna.";host=".$host.";", $user, $pass); } catch(pdoexception $e) { redirect::to(500); } } /** * description: gets data database * * @access: protected * @return boolean */ protected function get($table, $columns, $condition = null) { if($condition != null) { $query = $this->_pdo->prepare("select $columns $table $condition"); if($query->execute()) { $this->_count = $query->rowcount(); if($this->_count > 0) { $this->_results = $query->fetchall(); return true; } return false; } } return false; //@todo condition == null } /** * description: similar function, * instead checks if data exists without storing * data. * * @access: protected * @return boolean */ protected function query($table, $columns, $condition = null) { if($condition != null) { $query = $this->_pdo->prepare("select $columns $table $condition"); if($query->execute()) { $this->_count = $query->rowcount(); return(($this->_count > 0)? true : false); } } return false; //@todo condition == null } /** * description: updates information on database * * @access: protected */ protected function update($table, $cv = array(), $condition) { if($cv !=null) { $columns = ''; $x = 1; foreach($cv $key => $value) { $columns .= "$key='$value'"; if($x < count($cv)) { $columns .= ","; } $x++; } $query = $this->_pdo->prepare("update $table set $columns $condition"); if($query->execute()) return true; else return false; } return false; } /** * description: inserts data database * * @access: protected */ protected function insert($table, $cv = array()) { if($cv !=null) { // join array elements string $columns = implode(", ", array_keys($cv)); $values = ''; $x = 1; // put array key values variables foreach($cv $value) { $values .= "'".$value."'"; if($x < count($cv)) { $values .= ', '; } $x++; } $query = $this->_pdo->prepare("insert $table ($columns) values({$values})"); // check execution successful if($query->execute()) return true; else return false; } return false; } /** * description: deletes data database * * @access: protected */ protected function delete($table, $condition = null) { if($condition != null) { $query = $this->_pdo->prepare("delete $table $condition"); if($query->execute()) return true; else return false; } else { $query = $this->_pdo->prepare("delete $table"); if($query->execute()) return true; else return false; } } protected function getresults() { return $this->_results; } /** * description: singleton pattern, prevents multiple * instantiations of same class. * * note: not needed. "show of" * * @access: public * @static * @return object */ public static function instance() { if(isset(self::$_instace)) return self::$_instace; else self::$_instace = new self; } }
which other classes user class extend , use necessary function database data related user. have @ project. there bugs in few classes (which can't bothered fix @ point), database class working fine. don't mind if reference it.
visit github full project. github
Comments
Post a Comment