Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class dbMainExec extends dbConn
- {
- private $_table;
- private $_column;
- private $_where;
- private $_join;
- private $_errors = array();
- private $_stmt;
- private $_results;
- private $_countResults;
- public function __construct($table = null, $column = null, $where = null, $join = null)
- {
- parent::__construct();
- //$this->_conn = $conn;
- $this->setTable($table);
- $this->setColumn($column);
- $this->setWhere($where);
- $this->setJoin($join);
- }
- public function setTable($table)
- {
- $table = strtolower($table);
- $this->_table = $table;
- }
- public function getTable()
- {
- return $this->_table;
- }
- public function setColumn($column)
- {
- if (is_array($column)) {
- $this->_column = $column;
- } else {
- $this->_errors[] = "Columen is geen array!";
- }
- }
- public function getColumn()
- {
- return $this->_column;
- }
- public function setWhere($where)
- {
- if (is_array($where) || empty($where)) {
- $this->_where = $where;
- } else {
- $this->_errors[] = "where is geen array!";
- }
- }
- public function getWhere()
- {
- return $this->_where;
- }
- public function setJoin($join)
- {
- if (is_array($join) || empty($join)) {
- $this->_join = $join;
- } else {
- $this->_errors[] = "join is geen array!";
- }
- }
- public function getJoin()
- {
- return $this->_join;
- }
- public function setResults($results)
- {
- if (is_array($results)) {
- $this->_results = $results;
- } else {
- $this->_errors[] = "Fout bij resultaten";
- }
- }
- public function getResults()
- {
- return $this->_results;
- }
- public function setCountResults($countResults)
- {
- if (is_numeric($countResults)) {
- $this->_countResults = $countResults;
- } else {
- $this->_errors[] = "fout bij tellen resultaten";
- }
- }
- public function getCountResults()
- {
- return $this->_countResults;
- }
- public function setStatement($stmt)
- {
- $this->_stmt = $stmt;
- }
- public function getStatement()
- {
- return $this->_stmt;
- }
- public function execute($sql)
- {
- $this->setStatement($this->connect()->prepare($sql));
- /*if (!empty($this->getColumn())) {
- foreach ($this->getColumn() as $singleColumn => $columnValue) {
- $this->getStatement()->bindParam(':' . $singleColumn, $columnValue);
- }
- }*/
- if (!empty($this->getWhere())) {
- foreach ($this->getWhere() as $singleWhere => $whereValue) {
- $this->getStatement()->bindParam(':' . $singleWhere, $whereValue);
- }
- }
- if (!empty($this->getJoin())) {
- foreach ($this->getJoin() as $singleJoin => $joinValue) {
- $this->getStatement()->bindParam(':' . $singleJoin, $joinValue);
- }
- }
- $this->getStatement()->execute();
- $this->disconnect();
- }
- public function selectRow()
- {
- if ($this->_errors) {
- var_dump($this->_errors);
- die();
- }
- $sql = "SELECT " . implode($this->getColumn(), ',') . " FROM " . $this->getTable() . " ";
- if (!empty($this->getJoin())) {
- foreach ($this->getJoin() as $extraTable => $extraValues) {
- $sql .= "JOIN $extraTable";
- foreach ($extraValues as $singleValue => $singleTarget) {
- $sql .= " ON {$singleValue} = {$singleTarget} ";
- }
- }
- }
- if (!empty($this->getWhere())) {
- $sql .= "WHERE ";
- $arrayKeys = array_keys($this->getWhere());
- foreach ($this->getWhere() as $option => $value) {
- if ($option != end($arrayKeys)) {
- $sql .= $option . " = :{$option} AND ";
- } else {
- $sql .= $option . " = :{$option}";
- }
- }
- }
- $this->execute($sql);
- $this->setResults($this->getStatement()->fetch(PDO::FETCH_ASSOC));
- $this->setCountResults($this->getStatement()->rowCount());
- }
- public function selectMultiple()
- {
- if ($this->_errors) {
- var_dump($this->_errors);
- die();
- }
- $sql = "SELECT " . implode($this->getColumn(), ',') . " FROM " . $this->getTable() . " ";
- if (!empty($this->getJoin())) {
- foreach ($this->getJoin() as $extraTable => $extraValues) {
- $sql .= "JOIN $extraTable";
- foreach ($extraValues as $singleValue => $singleTarget) {
- $sql .= " ON {$singleValue} = {$singleTarget} ";
- }
- }
- }
- if (!empty($this->getWhere())) {
- $sql .= "WHERE ";
- $arrayKeys = array_keys($this->getWhere());
- foreach ($this->getWhere() as $option => $value) {
- if ($option != end($arrayKeys)) {
- $sql .= $option . " = :{$option} AND ";
- } else {
- $sql .= $option . " = :{$option}";
- }
- }
- }
- $this->execute($sql);
- $this->setResults($this->getStatement()->fetchAll(PDO::FETCH_ASSOC));
- $this->setCountResults($this->getStatement()->rowCount());
- }
- public function insert()
- {
- if ($this->_errors) {
- var_dump($this->_errors);
- die();
- }
- $sql = "INSERT INTO {$this->getTable()} ( " . implode(', ', array_keys($this->getColumn())) . ") VALUES (";
- $arrayKeys = array_keys($this->getColumn());
- foreach ($this->getColumn() as $singleColum => $singleValue) {
- $sql .= ":" . $singleColum;
- if ($singleColum != end($arrayKeys)) {
- $sql .= ", ";
- }
- }
- $sql .= ')';
- $this->execute($sql);
- }
- public function update()
- {
- if ($this->_errors) {
- var_dump($this->_errors);
- die();
- }
- $sql = "UPDATE {$this->getTable()} SET ";
- $arrayKeys = array_keys($this->getColumn());
- foreach ($this->getColumn() as $singlecolum => $singleValue) {
- $sql .= $singlecolum . " = :" . $singlecolum;
- if ($singlecolum != end($arrayKeys)) {
- $sql .= ", ";
- }
- }
- if (!empty($this->getWhere())) {
- $sql .= " WHERE ";
- $arrayKeys = array_keys($this->getWhere());
- foreach ($this->getWhere() as $option => $value) {
- if ($option != end($arrayKeys)) {
- $sql .= $option . " = :{$option} AND ";
- } else {
- $sql .= $option . " = :{$option}";
- }
- }
- }
- $this->execute($sql);
- }
- public function delete()
- {
- if ($this->_errors) {
- var_dump($this->_errors);
- die();
- }
- $sql = "DELETE FROM {$this->getTable()}";
- if (!empty($this->getWhere())) {
- $sql .= " WHERE ";
- $arrayKeys = array_keys($this->getWhere());
- foreach ($this->getWhere() as $option => $value) {
- if ($option != end($arrayKeys)) {
- $sql .= $option . " = :{$option} AND ";
- } else {
- $sql .= $option . " = :{$option}";
- }
- }
- }
- $this->execute($sql);
- }
- }
Add Comment
Please, Sign In to add comment