php - Modify how Function takes and queries DB? -
the following here current function:
public function get($table, $where) { if (count($where) === 3) { $operators = array( '=', '>', '<', '>=', '<=' ); $field = $where[0]; $operator = $where[1]; $value = $where[2]; if (in_array($operator, $operators)) { $sql = "select * {$table} {$field} {$operator} ?"; if (!$this->query($sql, array( $value ))->error()) { return $this; } } } return false; }
i modify function allow more variables in sql eg:
select * table id=2 , field2 = 'test';
currently takes query this:
get('table', array('field', '=', 'a'));
i want modify take query this:
get('table', array('field1', '=', 'a' , 'field2', '=', 'b'));
does make sense?
i still learning if there issue in code please let me know :)
thanks
<?php function _get($table, $wheres, $operatorbtwnwheres = "and") { $conditions = array(); $operators = array('=', '>', '<', '>=', '<=' ); foreach($wheres $where) { if (count($where) === 3) { $field = $where[0]; $operator = $where[1]; $value = $where[2]; if (in_array($operator, $operators)) $conditions[] = " $field $operator $value "; } } $w = implode($operatorbtwnwheres, $conditions); return "select * $table $w;"; } echo _get('yourtable', array( array('field1', '=', "'a'"), array('field2', '=', "'b'") )); // output: select * yourtable field1 = 'a' , field2 = 'b' ; echo _get( 'yourtable2', array( array('field3', '<', "10"), array('field4', '>', "8"), array('field5', '<=', "0") ), "or"); // output: select * yourtable2 field3 < 10 or field4 > 8 or field5 <= 0 ; ?>
Comments
Post a Comment