The OOP Approach:
First I'll just say that the OOP is much more than just one class in a code.
It's a concept, that allows us to think in bigger scale, and creates much more functionality in our code.
I created a DBHandler Class which will handle all of my DB Requests what so ever, so it's better to keep that in a separate file, even in a higher hierarchy directory.
This is how the class looks like:
<?php
class DBHandler{
public $dbCon;
function __construct(mysqli $dbCon){
$this->dbCon = $dbCon;
}
private function createQuery($parameters, $tables){
$params = implode(',', $parameters);
$tParam = implode(',',$tables);
return "SELECT $params FROM $tParam";
}
public function query($dbCon, $parameters, $tables){
$query = createQuery($parameters, $tables);
$result =$this->dbCon->query($query);
return $result;
}
}
?>
And this is how your main should look like:
<?php
$myownDB = new DBHandler($dbCon);
$myownDB->query($parameters, $tables);
?>
This way is way more maintainable. and I really suggest using it.
It's easier to separate your database handler from your actual code, so it wont get messy.
Please note, that this call $myownDB->query($parameters, $tables); will run the query instantly, and won't echo it.
The Procedural Programming:
<?php
function s($parameters){
$i = 0;
$addtoQuery ='';
for($i =0; $i<count($parameters); $i++){
$addtoQuery .= $parameters[$i].',';
}
return substr($addtoQuery, 0, -1);
}
function t($tables){
$i = 0;
$addtoQuery ='';
for($i =0; $i<count($tables); $i++){
$addtoQuery .= $tables[$i].',';
}
return substr($addtoQuery, 0, -1);
}
$parameters = array("range", "distance", "which");
$tables = array("north", "south");
$s = s($parameters);
$t = t($tables);
function createQuery($s, $t){
$query = "SELECT $s FROM $t";
return $query;
}
echo createQuery($s,$t);
?>
Result of echo => SELECT range,distance,which FROM north,south
Which you can simply add to Mysqli - Query
If it's not what you were looking for, I'm sorry.
Good luck!