I’ve got a few custom pages on my site where I’ve appended a query param on every link that goes to an item, specifically when the user is viewing items by type
or collection
. So if they’re browsing items with the type Still Image
, the items/show links are appended with ?type=6
, etc. The idea is that they’re mainly interested in viewing items that match that criteria, so the next/prev links on items/show should keep within that scope, rather than linking to the next/previous by item id.
The function below outputs the modified next/prev links when the query param is present, though I’m only testing with a very small dataset at this point. I’m wondering specifically if it’s a bad idea to use get_records()
to query when all I really need is each item id. I unset()
the results right after I get the ids, but I have no idea if that’s even helpful.
How many items can I query with get_records()
before it starts dragging the server? The project this is for will likely be returning maybe 2-3K records.
Any thoughts?
function scopedNavigation($item=null, $collectionid=null, $typeid=null){
$link_previous=null;
$link_next=null;
$itemid=$item->id;
$appendParam=null;
if($itemid){
$itemsArray=array();
$params=array();
// limit by collection OR type
if(strlen($collectionid) > 0){
$params['collection']=$collectionid;
$appendParam = '?collection='.$collectionid;
}elseif(strlen($typeid) > 0){
$params['type']=$typeid;
$appendParam = '?type='.$typeid;
}
$itemsQuery = get_records('Item',$params); // @todo: is this performant?
foreach($itemsQuery as $i){
$itemsArray[]=$i->id; // an array of item ids matching the param
}
unset($itemsQuery);
if(count($itemsArray)){
// find the ids on either side of the current item id
$index = array_search($itemid, $itemsArray);
if($index !== false && $index > 0 ){
$prev = $itemsArray[$index-1];
$link_previous = $prev ? '<a href="'.url('items/show/'.$prev.$appendParam).'">'.__('Previous Item').'</a>' : null;
}else{
$link_previous=null;
}
if($index !== false && $index < count($itemsArray)-1){
$next = $itemsArray[$index+1];
$link_next = $next ? '<a href="'.url('items/show/'.$next.$appendParam).'">'.__('Next Item').'</a>' : null;
}else{
$link_next=null;
}
}else{
return null;
}
}
$html .= '<nav>';
$html .= '<ul class="item-pagination-custom">';
$html .= '<li id="previous" class="previous">'.$link_previous ? $link_previous : link_to_previous_item_show().'</li>';
$html .= '<li id="next" class="next">'.$link_next ? $link_next : link_to_next_item_show().'</li>';
$html .= '</ul>';
$html .= '</nav>';
return $html;
}
Usage:
<?php echo scopedNavigation($item, html_escape($_GET['collection']), html_escape($_GET['type']));?>
EDIT: there’s a bug in the code above that causes some waste, but we’ll ignore that for the moment.