从WHILE中随机选择

问题描述:

如何获得更多结果时的随机项目? 这是可能的,而不是osc_count_item_resources():从WHILE中随机选择

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

我也有这两个功能,我不知道哪一个更好用。 函数来获取项目资源:

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

为什么你不能只是通过随机()'和'限制1'加上'秩序在SQL查询中? – varnie 2013-03-09 13:45:54

+0

我编辑了我的答案。顾名思义,$ item是单个项目吗?如果是这样检查我的答案是否有效。 – 2013-03-09 14:22:25

真的很容易,如果你有一个数组中的项目,您可以使用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)]); 
+0

谢谢,这很有用。但是,我可以排除0结果吗? array_rand有时会产生0. – 2013-03-09 14:42:39

+0

对此答案的更新:它也不会给我自己的项目标识。 – 2013-03-09 15:11:52

+0

0也是一个有效的键......我不明白你为什么要排除它(总是不包括该值),但是,你可以。这将返回密钥。你可以做var_dump($ item);并显示它返回的内容?你通常如何检索物品ID? – 2013-03-09 15:41:01