I have the following code. The QUERY is custom and give me results based on category.
After some checks I can acces items inside that category. Using the functions posted below.

If there are more items inside that category I want to retrieve a random from those results.
Is it possible or do I need to adjust some code in the functions?

Front page website code:

<?php // osc_query_item("category=".osc_category_id()); < working basic one
 // display category-icon if no items present
 if (osc_category_total_items() == 0) { ?>
 <div class="icon">
 <a href="<?php echo osc_search_category_url();?>">
 <img src="<?php echo osc_current_web_theme_url('images/') . osc_category_name() .'.png' ?>" alt="" title=""/></a>
 </div>
 <?php 
 } else {
 // then if category has items present, do query
 osc_query_item(array('category' => osc_category_id(), 'page' => 0, 'results_per_page' => 3)) ;

 // check WHILE custom-items-present AND images-enabled is TRUE
if ((osc_has_custom_items()) && (osc_images_enabled_at_items())) {   
// if resources present get them       
if (osc_count_item_resources()) {
// show image from item 
?>
<div class="icon">
<a href="<?php echo osc_item_url() ; ?>">
<img src="<?php echo osc_resource_thumbnail_url() ; ?>" title="<?php echo osc_item_title(); ?>" alt="<?php echo osc_item_title() ; ?>" /></a>
</div>                 
<?php 
} else { 
// show icon from category with link from item   
?>
<div class="icon">
<a href="<?php echo osc_item_url() ; ?>">
<img src="<?php echo osc_current_web_theme_url('images/nophoto.png') ; ?>" alt="" title=""/></a>
</div>
<?php 
}}          
// reset query for this category
osc_reset_custom_items () ;
}
// end of random items 
?>

Funtion QUERY items:

function osc_query_item($params = null) {
//      $mSearch = Search::newInstance();
$mSearch = new Search();
if($params==null) {
    $params = array();
} else if(is_string($params)){
    $keyvalue = explode("=", $params);
    $params = array($keyvalue[0] => $keyvalue[1]);
}
foreach($params as $key => $value) {
    switch($key) {
        case 'author':
            $tmp = explode(",", $value);
            foreach($tmp as $t) {
                $mSearch->fromUser($t);
            }
            break;

        case 'category':
        case 'category_name':
            $tmp = explode(",", $value);
            foreach($tmp as $t) {
                $mSearch->addCategory($t);
            }
            break;

        case 'country':
        case 'country_name':
            $tmp = explode(",", $value);
            foreach($tmp as $t) {
                $mSearch->addCountry($t);
            }
            break;

        case 'region':
        case 'region_name':
            $tmp = explode(",", $value);
            foreach($tmp as $t) {
                $mSearch->addRegion($t);
            }
            break;

        case 'city':
        case 'city_name':
            $tmp = explode(",", $value);
            foreach($tmp as $t) {
                $mSearch->addCity($t);
            }
            break;

        case 'city_area':
        case 'city_area_name':
            $tmp = explode(",", $value);
            foreach($tmp as $t) {
                $mSearch->addCityArea($t);
            }

        case 'results_per_page':
            $mSearch->set_rpp($value);
            break;

        case 'page':
            $mSearch->page($value);
            break;

        case 'offset':
            $mSearch->limit($value);
            break;

        default:
            osc_run_hook('custom_query', $key, $value);
            break;
    }
}
View::newInstance()->_exportVariableToView("customItems", $mSearch->doSearch());
}

Funtion HAS custom items:

function osc_has_custom_items() {
if ( View::newInstance()->_exists('resources') ) {
    View::newInstance()->_erase('resources') ;
}
if ( View::newInstance()->_exists('item_category') ) {
    View::newInstance()->_erase('item_category') ;
}
if ( View::newInstance()->_exists('metafields') ) {
    View::newInstance()->_erase('metafields') ;
}
if(View::newInstance()->_get('itemLoop')!='custom') {
    View::newInstance()->_exportVariableToView('oldItem', View::newInstance()->_get('item'));
    View::newInstance()->_exportVariableToView('itemLoop', 'custom');
}
$item = View::newInstance()->_next('customItems') ;
if(!$item) {
    View::newInstance()->_exportVariableToView('item', View::newInstance()->_get('oldItem'));
    View::newInstance()->_exportVariableToView('itemLoop', '');
} else {
    View::newInstance()->_exportVariableToView('item', View::newInstance()->_current('customItems'));
}
return $item;
}

From these last two functions I am not sure which one is better to use

Function GET item resources:

function osc_get_item_resources() {
    if ( !View::newInstance()->_exists('resources') ) {
        View::newInstance()->_exportVariableToView('resources', ItemResource::newInstance()->getAllResourcesFromItem( osc_item_id() ) ) ;
    }
    return View::newInstance()->_get('resources') ;
}

Function COUNT item resources:

function osc_count_item_resources() {
    if ( !View::newInstance()->_exists('resources') ) {
        View::newInstance()->_exportVariableToView('resources', ItemResource::newInstance()->getAllResourcesFromItem( osc_item_id() ) ) ;
    }
    return (int) View::newInstance()->_count('resources') ;
}

At this point I received results from the category and it contains 3 items. I was thinking maybe this is possible when you look at the code at the start. Like setting a pointer to a random item?

$max = osc_category_total_items();
$rand = rand(1,$max);
$item = $rand;

// if resources present get them       
   if (osc_get_item_resources($item)) {
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.