Dtonlinegames Posted August 29, 2009 Share Posted August 29, 2009 Hi guys, I was wondering about setting function variables. I built an SQL class and obviously I have variables to pass to it like table names and stuff. I was wondering for instance I have a function like this static function select($table,$column='',$where='',$limit='',$limitto='',$groupby='',$orderby=''){ could I try and set the group variable without setting the column,where,limit,limit to etc variables first? Like this? Database::select('table',$group='group by `row`); Or is there a specific way of doing this? Quote Link to comment Share on other sites More sharing options...
Mardoxx Posted August 29, 2009 Share Posted August 29, 2009 how about <?php static function select($table,$group='',$column='',$where='',$limit='',$limitto='',$groupby='',$orderby=''){ if(!isnull($group)){ //process $group } else { //process rest of the stuff } } but for EVERY Database::select I think you'd have to do select(table,'',column, where,.....etc) Quote Link to comment Share on other sites More sharing options...
Dtonlinegames Posted August 29, 2009 Author Share Posted August 29, 2009 Ah that sucks, I'll just have to reorder the variables so there in order of most used then. Thanks for the quick response! If I come up with any other resolution or idea's I'll be sure to post them up here! Quote Link to comment Share on other sites More sharing options...
attock Posted August 29, 2009 Share Posted August 29, 2009 actually you can, try this $blah = select($table, 'my grouping'); similar you can also try: $blah = select($table, $group = '', '', 'where this'); $blah = select($table, '', $column = '', 'where this'); Tops are the same. Where is what I use, you may find it handy: /*********************************************** * mysqlSearch(&$handle, $table, $arrSelect, $arrWhere, $order_field = '' , $order = '', $group = '', $limit = 100000, $arrJoins, $daKey) * * Generic table mysql fetch * $arrSelect = array ('id', 'carrier') or array('*' for all) \ OR LITERAL STRING e.g. array('literal: `id` as `theID`', `blah`) * $arrWhere = array ('id' => $id) * or Literal array('literal' => ' `id` = AND ( 99 OR `id` = 999)') * would result in AND ( `id` = AND ( 99 OR `id` = 999)' ) * $order_field = ORDER BY `the_field` * $order = ORDER BY `the_field` $order * $group = GROUP BY `carrier` * * @return array function mysqlSearch(&$handle, $table, $arrSelect, $arrWhere, $order_field = '' , $order = '', $group = '', $limit = 100000, $arrJoins = NULL, $daKey = NULL) { global $objResponse; # what needs to be selected? $strSelect = prepare_select($arrSelect); # what needs to be selected? $strWhere = prepare_where($arrWhere); # is there is no where, have a dummy one if (!$strWhere) $strWhere = ' 1 '; # any joins? $strJoins = prepare_joins($arrJoins); # run mysql now $query = ' SELECT ' . $strSelect .' FROM '.$table.' '. $strJoins .' WHERE '. $strWhere .' '.( $group ? ' GROUP BY ' . $group : ' ' ) .' '.( $order_field ? ' ORDER BY ' . $order_field . ' ' . $order : ' ' ) .' LIMIT '. $limit ; $result = mysqlRun($query, $handle , 'mySQL Fetch from ' . $table); if ($result) { while ($row = $result->fetch_assoc()) { if ($daKey) $arr[$row[$daKey]] = $row; else $arr[] = $row; } } return $arr; } /*************************************** * prepare_select($arrSlt) * * Returns a string of select criteria * */ function prepare_select($arrSelect) { if (is_array($arrSelect)) { foreach ($arrSelect as $k) { if ($strSelect) $strSelect .= ' , '; # literal string? if (preg_match('/literal:/',$k)) { $k = trim(str_replace('literal:','',$k)); $strSelect .= $k ; } else if ($k == '*') $strSelect .= $k; else $strSelect .= ' `'.$k.'` '; } } return $strSelect; } /*************************************** * prepare_where($arrWhere) * * Returns a string of where criteria * */ function prepare_where($arrWhere) { if (is_array($arrWhere)) { foreach ($arrWhere as $k => $v) { if ($strWhere) $strWhere .= ' AND '; # literal string? if (preg_match('/literal/',$k)) { $strWhere .= $v ; } else { $strWhere .= ' `'.$k.'` = "'.$v.'" '; } } } return $strWhere; } /*************************************** * prepare_joins($arrJoins) * * Returns a string of joins * */ function prepare_joins($arrJoins) { if (is_array($arrJoins)) { foreach ($arrJoins as $k => $v) { $strJoins .= $v ; } } return $strJoins; } Quote Link to comment Share on other sites More sharing options...
Mardoxx Posted August 29, 2009 Share Posted August 29, 2009 I had an idea: <?php function select($table,$a='', $b='',$c='') { if(func_num_args()==2){ $group = func_get_arg(1); echo "doing something with \$group: $group\n<br />\n"; } else { echo "doing something else with the other args:\n<br \>\n"; echo "\$a : $a, \$b : $b, \$c : $c"; } } echo "select(\"table\", \"group by 'row'\");\n<br />\n"; select("table", "group by 'row'"); echo "\n<br/><br/>"; echo "select(\"table\", \"apple\", \"boat\", \"cow\");\n<br/>\n"; select("table", "apple", "boat", "cow"); echo "\n<br/><br/>"; ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.