Jump to content

Custom Function Variables?


Dtonlinegames

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/172405-custom-function-variables/
Share on other sites

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)

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;
}

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/>";

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.