Jump to:

23461 Posts in 18908 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » displaying random images on page

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 1196 Views
  • theoldlr
    Avatar
    Community Member
    103 Posts

    displaying random images on page Link to this post

    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!

  • schellmax
    Avatar
    Community Member
    126 Posts

    Re: displaying random images on page Link to this post

    in case someone's still interested:
    using 'unset' won't rearrange the indexes in the array. use array_splice instead.
    see http://www.webmasterworld.com/php/3632117.htm

  • Devlin
    Avatar
    Community Member
    215 Posts

    Re: displaying random images on page Link to this post

    function RandomImages(){
    $limit = 50;
    $items = DataObject::get("Image", "ParentID=6", "RAND()", null, $limit);
    return $items;
    }

  • schellmax
    Avatar
    Community Member
    126 Posts

    Re: displaying random images on page Link to this post

    i guess this will give you duplicates, no?

  • Dr0gaz
    Avatar
    Community Member
    37 Posts

    Re: displaying random images on page Link to this post

    "in case someone's still interested:
    using 'unset' won't rearrange the indexes in the array. use array_splice instead.
    see http://www.webmasterworld.com/php/3632117.html"

    the link is no best... i think...

    Not understand how relolve problem with array_splice MY CASE:

    function RandomPerguntasItems()
       {   
          $list_dim=5;
          $items = $this->Problemas()->toArray();
          $random_perguntas = new DataObjectSet();

          while($random_perguntas->Count() <= ($list_dim-1) )
          {   
             $random_index = rand(0,sizeof($items)-1);
             $random_perguntas->push($items[$random_index]);
             array_splice($items , count($items), 5 );
             //unset($items[$random_index]);
             
             //if($items[$random_index]< $random_perguntas->Count()){$random_perguntas->Count() + $items[$random_index];}
          }
          
          return $random_perguntas;

    1196 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.