0

I want to create a function where the input are two array's. One for the fields and one for the tables.

public function s($fields = array(), $tables = array()) { }

but I have no idea how to one from here. I had the idea to loop through the two array's and save each value to a string like this (but this to me like not the best way to do this):

$length = count($fields);
$fieldsString = "";

for ($i = 0; $i < $length; $i++) {
    $fieldsString += $fields[$i];
}

then do the same thing to $tables and output the $fieldsString and $tablesString to a SQL query.

My question: can this be done in a more effective way?

EDIT I know how do to this with for-loop and get the output I want. But I'm looking for a more "professional" way to deal with this problem, to learn from this.

2 Answers 2

2

Instead of looping through the whole Array, you could also use implode() to create a string containing all the elements of the array:

$ieldsString = implode(',' , $fields);

http://php.net/manual/de/function.implode.php

Sign up to request clarification or add additional context in comments.

Comments

1

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!

5 Comments

the output of your code is what I'm looking for. I would've done it the same way with my knowledge (for-loop stuff). I just wanted to see if there's a more simple way of doing this.
You mean a simple way to create the query?
more like a pro way to do this, I'll edit my question
Oh Professional way.. that is just the quick and simple Procedural programming. The professional way is in the OOP of PHP. I'll write a class to show you the advantage
And yeah use Implode is much more faster

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.