By any means, if any can tell me if this code can be more optimized than it already is, please say so. It's a select statement to my PDO class. When I glare over my code, I'm certain there is a lot of easy improvements.
public function select($table, $where = array(), $Innerjoin = array(), $group = array(), $selects = array()) {
$vowels = array("a", "e", "i", "o", "u", "y", "A", "E", "I", "O", "U","Y");
$select = "";
if(empty($selects)){
$select .= 'SELECT *';
} else {
foreach($selects as $key => $value){
$select == "" ? $select = "SELECT " : $select .= " , ";
$select .= str_replace($vowels, "" , $key) . "." . $value;
}
}
$sql = $select . " FROM `" . $table . "` AS " . str_replace($vowels, "" , $table);
if(!empty($Innerjoin)){
$join = "";
foreach ($Innerjoin as $joinKey => $joinValue) {
foreach ($joinValue as $key => $value) {
$join .= " INNER JOIN `" . $joinKey . "` AS " . str_replace($vowels, "" , $joinKey) . " ON ";
if(is_array($value)){
foreach ($value as $ids => $id) {
$join .= str_replace($vowels, "" , $ids) . "." . $key . " = " . str_replace($vowels, "" , $joinKey) . "." . $id;
}
} else {
$join .= str_replace($vowels, "" , $table) . "." . $key . " = " . str_replace($vowels, "" , $joinKey) . "." . $key;
}
}
}
$sql .= $join;
}
if(!empty($where)){
$qWhere = "";
foreach($where as $key => $value){
foreach($value as $operator => $attribute){
$qWhere == "" ? $qWhere = " WHERE " : $qWhere .= " AND ";
$qWhere .= "" . str_replace($vowels, "" , $table) . "." . $key . " " . $operator . " '" . $attribute . "'";
}
}
$sql .= $qWhere;
}
if(!empty($group)){
$on = "";
foreach($group as $key => $value){
$on == "" ? $on = " GROUP BY (" : $on .= " , ";
$on .= str_replace($vowels, "" , $key) . "." . $value;
}
$on .= ")";
$sql .= $on;
}
return $this->action($sql);
}
public function delete($table, $where) {
$sql = "DELETE FROM {$table} ";
if(!empty($where)){
$qWhere = "";
foreach($where as $key => $value){
foreach($value as $operator => $attribute){
$qWhere == "" ? $qWhere = " WHERE " : $qWhere .= " AND ";
$qWhere .= "" . str_replace($vowels, "" , $table) . "." . $key . " " . $operator . " '" . $attribute . "'";
}
}
$sql .= $qWhere;
}
return $this->action($sql);
}
The way I call this is
public function Show() {
$output = '';
$where = array(
'user_id' => array(
'!=' => '0'
)
);
$join = array(
'users_profile' => array(
'user_id' => 'user_id'
),
'users_information' => array(
'user_id' => 'user_id'
),
'rightsgroup' => array(
'rightsgroup_id' => 'rightsgroup_id'
),
'users_accounting' => array(
'user_id' => 'user_id'
),
'accounting_groups' => array(
'salary_id' => array(
'users_accounting' => 'id'
)
)
);
$DataUsers = $this->_db->select('users', $where, $join, "");
if($DataUsers->count()) {
$resultsUsers = $DataUsers->results();
foreach ($resultsUsers as $userS) {
$output .= '<tr>';
$output .= '<td>' . $userS->firstname .'</td>';
$output .= '<td>' . $userS->lastname .'</td>';
$output .= '<td>' . $userS->username . '</td>';
$output .= '<td>' . $userS->district . '</td>';
$output .= '<td>' . $userS->groupname . '</td>';
$output .= '<td>' . $userS->accountingname . '</td>';
$output .= '<td><a href="?page=medarbejder&view='. $userS->user_id .'">View</td>';
$output .= '</tr>';
}
}
return $output;
}