[英]select random from WHILE
How to get a random item when more results are given? 当给出更多结果时如何获得随机物品? IS THIS POSSIBLE instead of osc_count_item_resources(): 这是可能的,而不是osc_count_item_resources():
$max = osc_category_total_items();
$rand = rand(1,$max);
$item = $rand;
if (osc_get_item_resources($item)) {
and then display item details? 然后显示项目详细信息?
Here is the code I use based on functions that I posted below: 这是我根据下面发布的功能使用的代码:
<?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
?>
This is the function that give me all results AFTER the query itself: 此函数可在查询本身后为我提供所有结果:
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;
}
This is the function for the Query: 这是查询的功能:
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());
}
I also have these two functions from which I am not sure which one is better to use. 我也有这两个函数,我不确定从哪个函数最好使用。 Function GET item resources: 功能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') ;
}
Function COUNT item 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') ;
}
Really easy, if you have the items in an array you can use array_rand() . 确实很容易,如果您将项目放在数组中,则可以使用array_rand() 。
After seeing your edit, I'd go with this: 看到您的修改后,我将执行以下操作:
$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)]);
Alternative: 选择:
$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)]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.