I've written a simple class that puts together a MySQL SELECT query and
allows you to extend the query, but I'm unsure as to when to use $this - >
var_name and when I can just use $varname, and when it's necessary to use
'var $varname' and when this can be left out.
Here's the class - could it be made simpler?
Michael
class select_query_bu ilder {
// class to put together and add to a select query
var $query;
var $select;
var $from;
var $where;
var $orderby;
var $distinct;
var $t_select;
var $t_from;
var $t_where;
var $t_orderby;
function select_query_bu ilder($select, $from, $where='', $orderby ='') {
// each variable is an array
$this -> select = $select;
$this -> from = $from;
$this -> where = $where;
$this -> orderby = $orderby;
$this -> t_select = $select;
$this -> t_from = $from;
$this -> t_where = $where;
$this -> t_orderby = $orderby;
}
// add additional elements to the query
function extend_query($w hat, $newinfo)
{
$this -> {'t_'.$what} = array_merge($th is -> {'t_'.$what}, $newinfo);
}
// return the complete query;
function return_query() {
$this -> query = 'SELECT '$this -> distinct.' '.implode(', ', $this ->
t_select).' FROM '.implode(', ', $this -> t_from);
if (!empty($this -> t_where)) {
$this -> query .= ' WHERE '.implode(' AND ', $this -> t_where);
}
if (!empty($this -> orderby)) {
$this -> query .= ' ORDER BY '.implode(' ASC, ', $this -> t_orderby).'
ASC';
}
return $this -> query;
}
function reset_to_before _extend() {
$this -> t_select = $this -> select;
$this -> t_from = $this -> from;
$this -> t_where = $this -> where;
$this -> t_orderby = $this -> orderby;
}
}
allows you to extend the query, but I'm unsure as to when to use $this - >
var_name and when I can just use $varname, and when it's necessary to use
'var $varname' and when this can be left out.
Here's the class - could it be made simpler?
Michael
class select_query_bu ilder {
// class to put together and add to a select query
var $query;
var $select;
var $from;
var $where;
var $orderby;
var $distinct;
var $t_select;
var $t_from;
var $t_where;
var $t_orderby;
function select_query_bu ilder($select, $from, $where='', $orderby ='') {
// each variable is an array
$this -> select = $select;
$this -> from = $from;
$this -> where = $where;
$this -> orderby = $orderby;
$this -> t_select = $select;
$this -> t_from = $from;
$this -> t_where = $where;
$this -> t_orderby = $orderby;
}
// add additional elements to the query
function extend_query($w hat, $newinfo)
{
$this -> {'t_'.$what} = array_merge($th is -> {'t_'.$what}, $newinfo);
}
// return the complete query;
function return_query() {
$this -> query = 'SELECT '$this -> distinct.' '.implode(', ', $this ->
t_select).' FROM '.implode(', ', $this -> t_from);
if (!empty($this -> t_where)) {
$this -> query .= ' WHERE '.implode(' AND ', $this -> t_where);
}
if (!empty($this -> orderby)) {
$this -> query .= ' ORDER BY '.implode(' ASC, ', $this -> t_orderby).'
ASC';
}
return $this -> query;
}
function reset_to_before _extend() {
$this -> t_select = $this -> select;
$this -> t_from = $this -> from;
$this -> t_where = $this -> where;
$this -> t_orderby = $this -> orderby;
}
}
Comment