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
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 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.