- Moderated by:
- Support
-
- rank:
-
Freshman
- registered:
- January 2003
- Status:
- offline
- last visit:
- 26.08.04
- Posts:
- 26
I am using this code to sort some of my arrays and I was wondering if someone could help me add it to my pnuserapi.php. Also how would I call it from pnuser.php? It works great puting in side the function but I would like to keep everything compliant if possible. Thanks in advance.
Code
//Array Sort Function from http://www.zend.com/codex.php?id=501&single=1
//metanurb
function aasort(&$array, $args) {
foreach($args as $arg) {
$order_field = substr($arg, 1, strlen($arg));
foreach($array as $array_row) {
$sort_array[$order_field][] = $array_row[$order_field];
}
$sort_rule .= '$sort_array['.$order_field.'], '.($arg[0] == "+" ? SORT_ASC : SORT_DESC).',';
}
eval ("array_multisort($sort_rule".' &$array);');
}
/*
Syntax: aasort($assoc_array, array("+first_key", "-second_key", etc..));
Example: aasort($db_array, array("+ID", "-AGE", "+NAME"));
Where the "+" in front of the keys stands for "ASC" and "-" for "DESC".
*/ -
- rank:
-
Moderator
- registered:
- March 2002
- Status:
- offline
- last visit:
- 26.08.08
- Posts:
- 7720
Functions is the userapi need to be named <module>_userapi_function. The function then takes an arguments array.
So the function looks like
Code
function <yourmodulename_userapi_yourfunctioname($args)
{
// Get arguments from argument array - all arguments to this function
// should be obtained from the $args array, getting them from other
// places such as the environment is not allowed, as that makes
// assumptions that will not hold in future versions of PostNuke
extract($args);
......
}
The function is then called using a call to pnModAPIFunc('modulename', 'type', 'func', argsarray) having first loaded the user API
loading and calling a function is
Code
......
// Load API. All of the actual work for the creation of the new item is
// done within the API, so we need to load that in before we can do
// anything. If the API fails to load an appropriate error message is
// posted and the function returns
if (!pnModAPILoad('module', type')) {
pnSessionSetVar('errormsg', _LOADFAILED);
return $output->GetOutput();
}
// call the apu function
$mid = pnModAPIFunc('module',
'type',
'function',
array('parameter1' => $paramter1,
'parameter2' => $paramter2,
etc..... ));
.......
-Mark</module> -
- rank:
-
Freshman
- registered:
- January 2003
- Status:
- offline
- last visit:
- 26.08.04
- Posts:
- 26
Hey mark, been a while.. I understand the format it should be in. I just can get the original function converted over and call it properly.. Probably figure it out like always right after I send this post off.. Thanks again. -
- rank:
-
Moderator
- registered:
- March 2002
- Status:
- offline
- last visit:
- 26.08.08
- Posts:
- 7720
In which case it would be something like
Code
function <module>_userapi_aasort($args)
{
$returnarray = array();
foreach($args as $arg) {
$order_field = substr($arg, 1, strlen($arg));
foreach($array as $array_row) {
$sort_array[$order_field][] = $array_row[$order_field];
}
$sort_rule .= '$sort_array['.$order_field.'], '.($arg[0] == "+" ? SORT_ASC : SORT_DESC).',';
}
eval ("array_multisort($sort_rule".' $returnarray);');
return $returnarray;
}
-Mark -
- rank:
-
Freshman
- registered:
- January 2003
- Status:
- offline
- last visit:
- 26.08.04
- Posts:
- 26
Thanks once again but I'm having a heck of a time getting the arguments over to the API. The function is looking for $array and $args. I have tried
Code
$returnarray = pnModAPIFunc('Wdpool',
'user',
'aasort',
array('array' => $fortiebreaking,
'args' => $sortbys));
I have also tried changing the function to look for $sortby instead of $args and I still get Invalid argument supplied for foreach()
That is for the frist foreach that looks for the $args or $sortby array. -
- rank:
-
Freshman
- registered:
- January 2003
- Status:
- offline
- last visit:
- 26.08.04
- Posts:
- 26
I got it working. Thanks again for your help Mark.. Here is what I got incase anyone else was following this thread. I wish I would have found this function months ago, would have saved me a lot of time and trouble with sorting these arrays..
Code
Code
function modname_userapi_aasort($args)
{
extract($args);
$returnarray = $array;
foreach($args as $arg) {
$order_field = substr($arg, 1, strlen($arg));
foreach($array as $array_row) {
$sort_array[$order_field][] = $array_row[$order_field];
}
$sort_rule .= '$sort_array['.$order_field.'], '.($arg[0] == "+" ? SORT_ASC : SORT_DESC).',';
}
// eval ("array_multisort($sort_rule".' $returnarray);');
eval ("array_multisort($sort_rule".' &$returnarray);');
return $returnarray;
}
