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
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)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.