I don't want rand to be employed on the sql. The concept is to cache
some records (so as not to hit to database all the time) and then get
a random record.

Just for the record :
in the above code I had srand(floor(time() / (60*60*24)));
debug(rand()); which always returned the same value.
When I change the seed for srand to srand(floor(time() +1000 /
(60*60*24))); rand() started returning random numbers! Now it does
work.

On May 18, 6:22 pm, piousbox <pious...@gmail.com> wrote:
> First, if you're developing this application, I strongly advice not
> messing with caching until you're ready to deploy. In particular, you
> do $banners_tmp = Cache::read('banners'); even if it returns empty?
> Maybe you're looking at only your cached version all the time, not
> what's in your database?
>
> Second, MySQL has random functions so you can put that inside your SQL
> code. You can have conditions like, "... SORT BY RAND() LIMIT 1..."
>
> Third, array_rand() doesn't look to be a cakePHP function either. Try
> writing a function that takes some data and returns a random piece of
> it. Then input your Position data into that function, basically break
> the task at hand into two: the cakePHP part of retrieving data, and
> the non-cakePHP part of taking a random element from an array. that
> is, if you don't want to use the advise above and incorporate RAND()
> into your SQL query.
>
> _V
>
> On May 17, 8:50 am, HK <hkosm...@gmail.com> wrote:
>
>
>
> > Hello,
>
> > I have a strange problem with one of my applications.
>
> > Consider the following code:
> > function getBanner() {
> >                 $banners_tmp = Cache::read('banners');
> >                 if(empty($banners_tmp))
> >                 {
> >                         $this->loadModel('Position');
> >                         $this->Position->cacheBanner();
> >                         $banners_tmp = Cache::read('banners');
> >                 }
>
> >                 //srand(floor(time() / (60*60*24)));
> >                //debug(rand() % count($banners_tmp));
>
> >                 //return $banners_tmp[rand() % count($banners_tmp)];
> >                 return $banners_tmp[array_rand($banners_tmp)];
> >         }
>
> > array_rand always return the first element of my array. I also used
> > srand and rand()%count() but with same result. Is there a problem with
> > cake and rands?
>
> > thanks a lot
>
> > Check out the new CakePHP Questions sitehttp://cakeqs.organdhelp others 
> > with their CakePHP related questions.
>
> > You received this message because you are subscribed to the Google Groups 
> > "CakePHP" group.
> > To post to this group, send email to cake-php@googlegroups.com
> > To unsubscribe from this group, send email to
> > cake-php+unsubscr...@googlegroups.com For more options, visit this group 
> > athttp://groups.google.com/group/cake-php?hl=en
>
> Check out the new CakePHP Questions sitehttp://cakeqs.organd help others with 
> their CakePHP related questions.
>
> You received this message because you are subscribed to the Google Groups 
> "CakePHP" group.
> To post to this group, send email to cake-php@googlegroups.com
> To unsubscribe from this group, send email to
> cake-php+unsubscr...@googlegroups.com For more options, visit this group 
> athttp://groups.google.com/group/cake-php?hl=en

Check out the new CakePHP Questions site http://cakeqs.org and help others with 
their CakePHP related questions.

You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en

Reply via email to