列表中的php循环随机产品 [英] php loop random product from the list

查看:59
本文介绍了列表中的php循环随机产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有产品清单,从女巫那里我需要随机显示四个项目.

i have product list, from witch i need to display four items randomly.

<?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']];
} 
?>

因此,如您所见,我有列表蚂蚁需要只随机显示列表中的四个项目.

so as you see, i have list ant need to display only four items form the list randomly.

提前谢谢!

推荐答案

这可以通过php array_rand 首先要使用数组rand,您需要创建数组

this can be done by php rand() function or you can also use the array_rand to use the array rand first you need to create the array

这篇关于列表中的php循环随机产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆