1

给出更多结果时如何获得随机项目?这有可能代替 osc_count_item_resources():

$max = osc_category_total_items(); 
$rand = rand(1,$max);
$item = $rand; 
if (osc_get_item_resources($item)) {

然后显示项目详细信息?

这是我根据我在下面发布的函数使用的代码:

 <?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 
?>

这是在查询本身之后给我所有结果的函数:

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;
}

这是查询的功能:

    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());
}

我也有这两个功能,我不确定哪个更好用。函数GET项目资源:

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') ;
}

函数 COUNT 项资源:

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') ;
}
4

1 回答 1

0

真的很容易,如果你有一个数组中的项目,你可以使用array_rand()

看到您的编辑后,我会这样做:

$ItemsFromQuery = array();
// While the function returns values, assign them.
while ($item = osc_has_custom_items())
  {
  // Introduce each item in the array
  $ItemsFromQuery[] = $item;
  }
// Echo only one random item id from the random key in the array of items
echo osc_item_id($ItemsFromQuery[array_rand($ItemsFromQuery)]);

选择:

$ItemsFromQuery = array();
// While the function returns values, assign them.
while ($item = osc_has_custom_items())
  {
  // Introduce each id in the array
  $IDsFromQuery[] = osc_item_id($item);
  }
// Echo only one random item id from the random key in the array of items
echo $IDsFromQuery[array_rand($IDsFromQuery)]);
于 2013-03-09T13:55:25.027 回答