giraffemedia Posted May 17, 2010 Share Posted May 17, 2010 Hi, i've tried searching for this but had no luck. I want to be able to select 3-4 random strings of text from a group of about 10, but i'm unable to get the right result. The strings will contain html tags that have a h4, p and img tags to display a random selection of products. I've tried the following but had no luck. Does anyone have an idea of what I should be doing? $product_1 = '<h2>Test Product 1</h2><p>skdug sdkhg ks dkghjs kjsd jhfs dfklj</p><img src="images/test/test1.gif" align="left" height="50" width="50" border="0" />'; $product_2 = '<h2>Test Product 2</h2><p>skdug sdkhg ks dkghjs kjsd jhfs dfklj</p><img src="images/test/test2.gif" align="left" height="50" width="50" border="0" />'; $product_3 = '<h2>Test Product 3</h2><p>skdug sdkhg ks dkghjs kjsd jhfs dfklj</p><img src="images/test/test3.gif" align="left" height="50" width="50" border="0" />'; $products = array($product_1, $product_2, $product_3); echo rand($products); Thanks, James Quote Link to comment Share on other sites More sharing options...
scvinodkumar Posted May 17, 2010 Share Posted May 17, 2010 the rand() which is used to display random numbers only, for that you need to use like this $pid = array_rand($products); echo $products[$pid]; Quote Link to comment Share on other sites More sharing options...
taquitosensei Posted May 17, 2010 Share Posted May 17, 2010 array_rand(); $product_1 = '<h2>Test Product 1</h2><p>skdug sdkhg ks dkghjs kjsd jhfs dfklj</p><img src="images/test/test1.gif" align="left" height="50" width="50" border="0" />'; $product_2 = '<h2>Test Product 2</h2><p>skdug sdkhg ks dkghjs kjsd jhfs dfklj</p><img src="images/test/test2.gif" align="left" height="50" width="50" border="0" />'; $product_3 = '<h2>Test Product 3</h2><p>skdug sdkhg ks dkghjs kjsd jhfs dfklj</p><img src="images/test/test3.gif" align="left" height="50" width="50" border="0" />'; $products = array($product_1, $product_2, $product_3); $rand_products=array_rand($products,3); // this gives you an array with your 3 random elements Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.