Jump to content

Choosing random strings from a group


giraffemedia

Recommended Posts

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

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 

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.