2012-09-17 42 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使用数组兰特首先你需要创建数组