Jamroom Logo Jamroom 5 Core
is now Open Source!
User Support Forum Archive (Read Only)
User Site Design, Integration and Customization:
Order of Subscription Options on Template Page
speedbunny



Joined: 15 Nov 2007
Posts: 363

Posted: 01/17/08 11:31 
Hi -

I am trying to order my three subscription options on my subscription template by their quota_id.


Code

{foreach from=$_SUBS|@sortby:"quota_id" item="_sub"}


This displays quota_id 6 first, quota_id 4 second, and quota_id 7 last.

If I take off the sortby code I get quota_id 7 first, quota_id 6 second and quota_id 4 last.

- How is it sorting them by default?
(I've checked and it isn't time created, nor alphabetical)

- How can I sort it in ascending quota_id?

- How can I sort it by price of subscription?

Thanks for your help!!

Sarah

Back to top
smith.kyle
CodeSmith


Joined: 27 Apr 2006
Posts: 22009
Location: Southern California

Posted: 01/17/08 15:18 
I thought it did do them by quota_id ascending? Maybe not though...You might try this:

{foreach from=$_SUBS|@sortby:"QUOTA_ID" item="_sub"}

(note it's capitalized). Where'd you find the |@sortby bit? That's pretty nifty, I've never seen it!


_________________
kyle[at]jamroom.net

Yes...that's a soda machine...

I get bored when no one's posting...
Back to top
Brian
Jamroom Team


Joined: 09 Jul 2003
Posts: 37583
Location: Seattle, WA

Posted: 01/17/08 17:55 
It looks like the @sortby is an undocumented "feature" - nice find Wink

- Brian


_________________
Make sure and check out:
* The Jamroom FAQ
* The Jamroom Documentation
Back to top
speedbunny



Joined: 15 Nov 2007
Posts: 363

Posted: 01/17/08 18:30 
Well, I'm not convinced sortby is doing what it is meant to do - it doesn't matter what I put in the sortby string (whether sortby="sjfsd" or sortby="QUOTA_ID") - it just sorts the given array the same way.

It is possible I've not installed it correctly, never really used smarty modifiers and functions before...

So, back to the original question, how are subscription options currently sorted in Jamroom?

And is there any way I can get it to sort by quota_id instead of the default?

I mean if someone wanted to baby step me through installing the sortby modifier so that it worked, that would be great - otherwise I'll try and do it without adding something else in.

Thanks for trying so far guys...

Back to top
smith.kyle
CodeSmith


Joined: 27 Apr 2006
Posts: 22009
Location: Southern California

Posted: 01/17/08 18:54 
Are you using one of the sortby modifiers on this page:

http://www.phpinsider.com/smarty-forum/viewtopic.php?t=1079&highlight=sortby

?


_________________
kyle[at]jamroom.net

Yes...that's a soda machine...

I get bored when no one's posting...
Back to top
speedbunny



Joined: 15 Nov 2007
Posts: 363

Posted: 01/17/08 19:04 
Found a slightly more up to date one - this is what I'm using. I also have functions uploaded but not sure if I need them or just this modifier file below...


Code

<?PHP

#
# sorts an array of named arrays by the supplied fields
# code by dholmes at jccc d0t net (taken from http://au.php.net/function.uasort)
# modified by cablehead, messju and pscs and trine

function array_sort_by_fields(&$data, $sortby){
static $sort_funcs = array();

if (empty($sort_funcs[$sortby]))
{
$code = "\$c=0;";
foreach (split(',', $sortby) as $key)
{
$d = '1';
if (substr($key, 0, 1) == '-')
{
$d = '-1';
$key = substr($key, 1);
}
if (substr($key, 0, 1) == '#')
{
$key = substr($key, 1);
$code .= "if ( \$a['$key'] > \$b['$key']) return $d * 1;\n";
$code .= "if ( \$a['$key'] < \$b['$key']) return $d * -1;\n";
}
else
{
$code .= "if ( (\$c = strcasecmp(\$a['$key'],\$b['$key'])) != 0 ) return $d * \$c;\n";
}
}
$code .= 'return $c;';
$sort_func = $sort_funcs[$sortby] = create_function('$a, $b', $code);
}
else
{
$sort_func = $sort_funcs[$sortby];
}
uasort($data, $sort_func);
}
#
# Modifier: sortby - allows arrays of named arrays to be sorted by a given field
#
function smarty_modifier_sortby($arrData,$sortfields) {
array_sort_by_fields($arrData,$sortfields);
return $arrData;
}

$smarty->register_modifier( "sortby", "smarty_modifier_sortby" );

?>


Back to top
SteveX
Ultrabubble


Joined: 30 Aug 2005
Posts: 8792
Location: Ultrabubble

Posted: 01/17/08 19:15 
Got a link to the sortby page by any chance?

Don't worry if you don't, I couldn't see it on my initial google but I'm sure it is findable.


_________________
Kulshi Mezian!

"Stranger from another planet, welcome to our hole. Just strap on your guitar and we'll play some rock and roll"

Ultrabubble create things.
Back to top
speedbunny



Joined: 15 Nov 2007
Posts: 363

Posted: 01/17/08 19:20 
Hi Steve, I just got the mod from the forum smith.kyle linked to above - (he of the official smith.kyle Shoutcast nontheless).

I did find another site after some searching (I added the modifier code a while back), but I've not been able to find it again today. Actually, my code above is just directly from the forum with the modification on the last post added....

Back to top
smith.kyle
CodeSmith


Joined: 27 Apr 2006
Posts: 22009
Location: Southern California

Posted: 01/17/08 19:51 
Try doing this instead, create a file called modifier.sortby.php and put this in it:


Code
<?php

#
# sorts an array of named arrays by the supplied fields
# code by dholmes at jccc d0t net (taken from http://au.php.net/function.uasort)
# modified by cablehead, messju and pscs and trine

function smarty_modifier_sortby($array,$sortby)
{
    function array_sort_by_fields($array, $sortby)
    {
    $sort_funcs = array();

    if (empty($sort_funcs[$sortby])) {
        $code = "\$c=0;";
        foreach (split(',', $sortby) as $key) {
            $d = '1';
            if (substr($key, 0, 1) == '-') {
                $d = '-1';
                $key = substr($key, 1);
            }
            if (substr($key, 0, 1) == '#') {
                $key = substr($key, 1);
                $code .= "if ( \$a['$key'] > \$b['$key']) return $d * 1;\n";
                $code .= "if ( \$a['$key'] < \$b['$key']) return $d * -1;\n";
            }
            else {
                $code .= "if ( (\$c = strcasecmp(\$a['$key'],\$b['$key'])) != 0 ) return $d * \$c;\n";
            }
        }
        $code .= 'return $c;';
        $sort_func = $sort_funcs[$sortby] = create_function('$a, $b', $code);
    }
    else {
        $sort_func = $sort_funcs[$sortby];
    }
    uasort($array, $sort_func);
    }
    array_sort_by_fields($array,$sortfields);
    return $array;
}
?>


And then put that file in your jamroom/include/plugins/smarty/ directory. I'm not sure if it'll work but it's worth a try.


_________________
kyle[at]jamroom.net

Yes...that's a soda machine...

I get bored when no one's posting...

Last edited by smith.kyle on 01/17/08 20:05; edited 1 time in total
Back to top
speedbunny



Joined: 15 Nov 2007
Posts: 363

Posted: 01/17/08 19:55 
With that version I get ze error:

"PHP Fatal error: Call to undefined function array_sort_by_fields() in /home3/rockscom/public_html/include/smarty/libs/plugins/modifier.sortby.php on line 36"

Smile Cheers for all your help, I'm sure we'll crack it.

Back to top
smith.kyle
CodeSmith


Joined: 27 Apr 2006
Posts: 22009
Location: Southern California

Posted: 01/17/08 20:07 
Duh, I screwed something up. I edited the code above - recopy it and paste it to your file. Try putting the file in the jamroom/include/plugins/smarty/directory (not the one you have it in).


_________________
kyle[at]jamroom.net

Yes...that's a soda machine...

I get bored when no one's posting...
Back to top
speedbunny



Joined: 15 Nov 2007
Posts: 363

Posted: 01/17/08 20:16 
Cheers Kyle...

You know - that didn't seem to do anything... however the earlier version of the code, before people started adding fancy stuff to it, seems to work just fine.

I know it may be a bit memory intensive (what am I saying... I don't know &*^%... ) but this is what I'm using and what's working....


modifier.sortby.php - in the includes/smarty/libs/plugin directory


Code

<?php
#
# sorts an array of named arrays by the supplied fields
#   code by dholmes at jccc d0t net
#   taken from http://au.php.net/function.uasort
#
function array_sort_by_fields(&$data, $sortby){
    if(is_array($sortby)) {
        $sortby = join(',', $sortby);
    }
    uasort( $data,
         create_function( '$a, $b', '
            $skeys = split(\',\',\''.$sortby.'\');
            foreach($skeys as $key){
               if( ($c = strcasecmp($a[$key],$b[$key])) != 0 ){
                            return($c);
               }
            }
           return($c);  '));
}

#
# Modifier: sortby - allows arrays of named arrays to be sorted by a given field
#
 
function smarty_modifier_sortby($arrData, $sortfields) {
   array_sort_by_fields($arrData, $sortfields);
   return $arrData;
}

$smarty->register_modifier( "sortby", "smarty_modifier_sortby" ); 


Sortby function in the sub template:

Code

{foreach from=$_SUBS|@sortby:"QUOTA_ID" item="_sub"}


what says you? Smile anything I can do to make it fancier?

Back to top
smith.kyle
CodeSmith


Joined: 27 Apr 2006
Posts: 22009
Location: Southern California

Posted: 01/17/08 20:20 
Well hell, I didn't realize what you had was working--I thought it wasn't. If it's workin, then DON't use mine! lol. If it ain't broke, don't fix it!


_________________
kyle[at]jamroom.net

Yes...that's a soda machine...

I get bored when no one's posting...
Back to top
speedbunny



Joined: 15 Nov 2007
Posts: 363

Posted: 01/17/08 20:26 
Hi Kyle - nooo, it wasn't working Smile

I was just working on making changes the same time as you... and hit upon this version between my last post and your last post.

Thanks for all your help, the effort wasn't wasted, that's for sure!!

Back to top
jubsi



Joined: 12 Jun 2012
Posts: 11

Posted: 08/07/12 18:49 
Thanks Speedbunny, it is 2012 now, but your solution still works like a charm.

Back to top
Display posts from previous:   
User Support Forum Archive (Read Only)
User Site Design, Integration and Customization

12Next >
 
Solutions
• Social Media Platform
• Social Networking Software
• Musician Website Manager
• Community Builder
Products
• Jamroom Core
• Jamroom Addons
• Jamroom Modules
• Jamroom Marketplace
Support
• Support Forum
• Documentation
• Support Center
• Contact Support
Community
• Community Forum
• Member Sites
• Developers
Company
• About Us
• Contact Us
• Privacy Policy
©2003 - 2010 Talldude Networks, LLC.