2013-03-26 42 views
0

如何顯示一組隨機的,沒有任何重複的問題(從WordPress的拉)?隨機選擇一組行沒有任何重複

這是我已經試過:

<?php 
$amount = get_field('select_number_of_questions'); 
$rand_max = count(get_field('step_by_step_test')) -1; 
$rand = rand($amount,$rand_max); 
$i = 0; 

while(has_sub_field('step_by_step_test')): 
    if($rand == $i): 
     echo the_sub_field('question'); 
    endif; 
    $i++; 
endwhile; 
?> 

目前它只是顯示1隨機抽題。

它的所有動態的,所以例如可以有總共10個,20個,31個問題等的問題,以選擇的總量由get_field('select_number_of_questions');

count(get_field('step_by_step_test')) -1;定義越來越的問題的總量從選擇。

因此總的來說,我希望它從總共count(get_field('step_by_step_test')) -1;中選擇由get_field('select_number_of_questions');定義的問題的數量,而不會有任何重複。

回答

3

我真的不知道如何在WordPress的具體實現這一點,但是這是你的工作流程應該如何看起來像:

  1. 採取所有的問題到一個數組。
  2. 選擇隨機(使用array_rand())一個問題。
  3. 從數組中刪除該問題(使用unset($array[$question_you_selected]))。
  4. 再次選擇。
  5. 沖洗並重復,直到你有你想要的問題的數量。你不會有重複。
+0

好,我解決了你的理論(感謝)的第一部分。只是解決問題的第二部分。 – Rob 2013-03-26 12:15:47

+0

第二部分?第二部分是什麼? – 2013-03-26 13:50:59

+0

對不起,我現在補充說,作爲一個單獨的問題 - http://stackoverflow.com/questions/15638373/include-required-questions-in-a-random-selection – Rob 2013-03-26 13:54:13

相關問題