2013-01-23 22 views
0

我知道如何編寫一個旋轉編碼,但我怎麼能讓一個跳過傳遞空的$ stuff_link變量?如何旋轉變量,除非它們是空的

我有4個鏈接變量如下所示,但有時它們是空白的。所以我需要做的是使用旋轉器在4個變量之間旋轉,但如果說$ stuff_link是空白跳過傳遞它。

$stuff_link 
$stuff_link2 
$stuff_link3 
$stuff_link4 

下面的代碼是我將它放在裏面的代碼。

if(percentChance(35) && $stuff_status == 1) 
{ 

    rotator goes here  
} 

下面這是percentChance

function percentChance($chance){ 
// Notice we go from 0-99 - therefore a 100% $chance is always larger 
$randPercent = mt_rand(0,99); 
return $chance > $randPercent; 
} 
+0

在存儲你的「東西」數組會走很長的路。 – meagar

回答

0

你讓使用4個變量有一點有趣,最後有一個沒有數字。如果必須使用這些作爲是像這樣的做的工作:

$stuffs = array('', '2', '3', '4'); // Array of possible variable endings 
$random = array_rand($stuffs); // Pick one 
$selected = $stuffs[$random]; // Get the ending 

// Check if the variable is empty, if not pick another 
while (empty(${'stuff_link'.$selected})) { 
    $random = array_rand($stuffs); 
    $selected = $stuffs[$random]; 
} 

// Output 
echo ${'stuff_link'.$selected}; 

如果你可以移動變量到一個數組,那麼生活變得更加容易:

// Example array 
$stuff_link = array(); 
$stuff_link[] = 'stuff 1'; 
$stuff_link[] = ''; 
$stuff_link[] = 'stuff 3'; 
$stuff_link[] = 'stuff 4'; 

shuffle($stuff_link); // mix them up 

// Keep shuffling until the first value is not empty 
while (empty($stuff_link[0])) { 
    shuffle($stuff_link); 
} 

// Output 
echo $stuff_link[0]; 
+0

以及$ stuff_link中的每一個都從我的數據庫中填充信息。他們還需要顯示偶數的時間。 – user2002220

+0

這很好,但我需要它顯示均勻 – user2002220

+0

如同在隨機順序顯示在同一頁上的每個非空白鏈接? –

0

功能從我可以從這個問題明白了,你需要一個類似的功能:

function isEmpty($link) { 
    return ($link == NULL || $link == "");    
} 

if (!isEmpty($stuff_link)) 
{ 
    // Only enters if not empty 
} 
+0

謝謝,我不知道isEmpty也是我想要的是在4個變量之間旋轉,但是如果1是空的,跳過它 – user2002220

相關問題