You have several issues in your code:
- In your
addvar method, you are not accessing any instance variables. You are assigning the alues to a local variable.
- Your
initiate method cannot access the variable $postvar.
- In the
if clause you are accessing a local variable $q instead of the instance variable $this->q.
- You want to pass an array of arrays to
http_build_query but is has to be a "normal" array.
You are mixing up a lot!
A more complete example of your class would be helpful, but I think it should look more like this:
class QueryBuilder {
private $params = array();
public function addParameter($key, $value) {
$this->params[$key] = $value;
}
public function send() {
$query = http_build_query($this->params);
// whatever else has to be done to send.
// for the sake of this example, it just returns the query string:
return $query;
}
}
Example:
$obj = new QueryBuilder();
$obj->addParameter('username', 'abc');
$obj->addParameter('password', 'foobar');
echo $obj->send(); // echos 'username=abc&password=foobar'
In general, if you already have the query that was built by html_build_query you can just append to that string:
$query = http_build_query(array('foo' => 'bar', 'faa' => 'baz'));
$query .= '&key=value';
echo $query; // echos 'foo=bar&faa=baz&key=value'