RE: dynamicdata getitems function

Paul Thomann <[email protected]> Wed, 10 Sep 2003 13:13:17 -0400
Newsgroups gmane.comp.cms.xaraya.knowledge-base
Message-ID <3A352CF455E0D311B50D00105ACEB91E100980@thomannasphalt1.thomannoffice.com>
> >Mikespub wrote:

> Hmmm, you should retrieve all the items you want, build a 
> list of item ids,
> and then pass that list to the getitems() function of 
> hitcount or ratings :
> 
> $items = xarModAPIFunc('dynamicdata', 'user', 'getitems',
>                        array('module' => 'mymodule',
>                              'itemtype' => 0,
>                              'sort' => 'myfield DESC',
>                              'numitems' => 20));
> $idlist = array_keys($items);
> $hits = xarModAPIFunc('hitcount', 'user', 'getitems',
>                        array('modname' => 'mymodule',
>                              'itemtype' => 0,
>                              'itemids' => $idlist));
> foreach ($idlist as $id) {
>     $items[$id]['counter'] = $hits[$id];
> }
> 
> Or in reverse, if you need to start with the top N items, 
> call topitems()
> first, and then retrieve the list of items with those ids from DD :
> 
> $tophits = xarModAPIFunc('hitcount', 'user', 'topitems',
>                          array('modname' => 'mymodule',
>                                'itemtype' => 0,
>                                'numitems' => 20));
> $idlist = array();
> foreach ($tophits as $tophit) {
>     $idlist[] = $tophit['itemid'];
> }
> $items = xarModAPIFunc('dynamicdata', 'user', 'getitems',
>                        array('module' => 'mymodule',
>                              'itemtype' => 0,
>                              'itemids' => $idlist));
> foreach ($tophits as $tophit) {
>     $id = $tophit['itemid'];
>     if (isset($items[$id])) {
>         $items[$id]['counter'] = $tophit['hits'];
>     }
> }
> [or equivalent $object->getItems() calls]
> 
> That way, you only have 2 function/SQL calls instead of 1 + N...
> 
I did it this way. I had to add a few more conditionals in order to get it
to work with hits, ratings and pubdate. 

    if ($vars['toptype'] == 'rating') {
	$dynitems= xarModApiFunc('ratings','user','topitems',
								array(
	
'modname' => 'dyn_example'
									   
	
));
	
            } elseif ($vars['toptype'] == 'dynitems') {
	$dynitems= xarModApiFunc('hitcount','user','topitems',
								array(
	
'modname' => 'dyn_example'
	
));
									   
									   
            } else {
	$dynitems= xarModApiFunc('dynamicdata','user','getitems',
								array(
	
'module' => 'dyn_example'
	
));
	
            }

and then a conditional in the foreach loop to deal with the different
keynames in the associative arrays that are returned. 

> Better yet would be to retrieve everything at once, but that's not
> feasible today (at least with DD).
> 
I look forward to that but really once you have the basic principles down
the rest seems to fall into place and isn't that difficult.


> Thanks :) DD is probably a bit too complex for beginning site 
> designers,
> but it does offer some interesting functionality for more 
> advanced users
> - at least that was the intention :)
I wouldn't call myself advanced. I seem to be getting the hang of it though.

Paul