Hello,
From elsewhere on the forums I've put together this function:
function RandomImages(){
$items = DataObject::get("Image","ParentID=6", false, "")->toArray();
$random_images = new DataObjectSet();
while($random_images->Count() <=1)
{
$random_index = rand(0,sizeof($items)-1);
$random_images->push($items[$random_index]); //<-Problematic line of code
unset($items[$random_index]);
}
return $random_images;
}
and I'm displaying them in my template like so:<% control RandomImages %>
<a class="thickbox" href="$Filename" >
<img src="<% control SetWidth(220) %>$URL<% end_control %>" alt="$Title" />
</a>
<% end_control %>
My pages display properly most of the time, however every once in a while I will get an error '[Notice] Undefined Offset'. I suspect that the problem might be that it only occurs when the same random value is chosen twice in a row and that the 'unset($items[$random_index]);' line is what is causing the error. Once taking this line out, I cannot recreate the error, but instead on occasion I get the same 2 pictures on the page. What would be the proper way to avoid the same random numbers?
Thanks!