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 Link to comment https://forums.phpfreaks.com/topic/202031-choosing-random-strings-from-a-group/ 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]; Link to comment https://forums.phpfreaks.com/topic/202031-choosing-random-strings-from-a-group/#findComment-1059440 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 Link to comment https://forums.phpfreaks.com/topic/202031-choosing-random-strings-from-a-group/#findComment-1059443 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.