2012-09-17 49 views
-4

我有產品清單,從女巫我需要隨機顯示四個項目。php循環隨機產品從名單

<?php 

// my product list 
function productlistX { 
    $array = array( 

     $product {$name = 'product name', $link = 'product link', $img = "product image"}; 
     $product {$name = 'product name', $link = 'product link', $img = "product image"}; 
     $product {$name = 'product name', $link = 'product link', $img = "product image"}; 
     $product {$name = 'product name', $link = 'product link', $img = "product image"}; 
     $product {$name = 'product name', $link = 'product link', $img = "product image"}; 
     $product {$name = 'product name', $link = 'product link', $img = "product image"}; 
     $product {$name = 'product name', $link = 'product link', $img = "product image"}; 
     $product {$name = 'product name', $link = 'product link', $img = "product image"}; 
     $product {$name = 'product name', $link = 'product link', $img = "product image"}; 
     $product {$name = 'product name', $link = 'product link', $img = "product image"}; 
     $product {$name = 'product name', $link = 'product link', $img = "product image"}; 
     $product {$name = 'product name', $link = 'product link', $img = "product image"}; 
     $product {$name = 'product name', $link = 'product link', $img = "product image"}; 
     $product {$name = 'product name', $link = 'product link', $img = "product image"}; 
     $product {$name = 'product name', $link = 'product link', $img = "product image"}; 

    ) 

    // need to display 4 random products from the array above 
    foreach { 
     echo "<li>".$name . $link . $img . "</li>" 
    } 
} 

// display these random products in my ajax tab (products.php?order=mytab) 
$data['mytab'] = $productlistX; 

if (isset($data[$_GET['order']])) { 
    echo $data[$_GET['order']]; 
} 
?> 

所以你看,我有表螞蟻需要顯示只有四個項目隨機形成的列表。

在此先感謝!

+6

這看起來像功課。您可能想要添加更多的細節。包括你已經嘗試過的代碼。 – ZnArK

+0

更新了我的問題,等待幫助 – ctSx

+0

您的代碼無效PHP,您有沒有閱讀手冊?您似乎缺乏編程語言基本語法規則的知識。 –

回答

0

不知道我是否知道什麼是text-php文件類型,但生成四個數字,並去閱讀,直到你得到所有四個顯示。或者閱讀整個事情並從那裏選擇。

+0

抱歉,我是新的PHP。我已更新我的問題更多細節 – ctSx

2

這可以通過PHP rand()函數來完成,或者您也可以使用array_rand使用數組蘭特首先你需要創建數組