Randomize and Limitation

I have a slight problem with my code. What it is supposed to do is randomize the products to be displayed (see http://wilsonelectronics.com/Misc.php?Page=WhatsNew to see what I’m talking about) and only display a set amount at a time.

From my limited understanding of the php coding, this is the section that tells it what is to be placed on the page, how many it is supposed to display and that it is supposed to randomize which ones to display.

[code]case ‘WhatsNew’:
$New[] = NewProduct(812201, ‘Now Available’);
$New[] = NewProduct(801245, ‘Now Available’);
$New[] = NewProduct(301124, ‘Now Available’);
$New[] = NewProduct(301133, ‘Now Available’);
$New[] = NewProduct(301129, ‘Now Available’);
$New[] = NewProduct(301130, ‘Now Available’);
$New[] = NewProduct(301123, ‘Now Available’);
$New[] = NewProduct(801306, ‘Now Available’);
$New[] = NewProduct(811210, ‘Now Available’);
$New[] = NewProduct(811211, ‘Now Available’);
$New[] = NewProduct(801247, ‘Summer 2007’);
//Randomly Remove 3
$Rand = rand(0, (sizeof($New) - 3));
unset($New[$Rand]);

		$Tpl->MergeBlock('New', $New) ;
	break;[/code]

So far, all it does is display 10 of the 11 products and doesn’t randomize them. How can I make this work?

[php]
shuffle($New);
[/php]

for randomize items.

You can get part of array using array_slice.

[php]
shuffle($New);
$New = array_slice($New, 0, 2);
[/php]
Now you have random 3 items.

What if I wanted it to display, say, 6 products at a time only, and want them to be randomized from the list (array) that is set up.

I didn’t create the code, so I am not sure where to put the randomization and limitation of display in there. I inherited the job, not start it.

[php]
case ‘WhatsNew’:
$New[] = NewProduct(812201, ‘Now Available’);
$New[] = NewProduct(801245, ‘Now Available’);
$New[] = NewProduct(301124, ‘Now Available’);
$New[] = NewProduct(301133, ‘Now Available’);
$New[] = NewProduct(301129, ‘Now Available’);
$New[] = NewProduct(301130, ‘Now Available’);
$New[] = NewProduct(301123, ‘Now Available’);
$New[] = NewProduct(801306, ‘Now Available’);
$New[] = NewProduct(811210, ‘Now Available’);
$New[] = NewProduct(811211, ‘Now Available’);
$New[] = NewProduct(801247, ‘Summer 2007’);
shuffle($New);
$New = array_slice($New, 0, 5);

     $Tpl->MergeBlock('New', $New) ;
  break;[/php]

Thank you! That worked wonders :D. I used 6 as the second number instead of 5, because for some reason it didn’t display six.

Sponsor our Newsletter | Privacy Policy | Terms of Service