Problems, need help? Have a tip or advice? Post it here.
7 posts Page 1 of 1
Okay all. I'll try explain it.

I have a section on the website called Adsense this is a clone page that i have created. I have then placed into another page the code snippet. <cms:pages masterpage='adsense/index.php' > Now i want to limit this to lets say 5 adverts. Now the plan is i want to have it that they are show randomly.

The full code that goes onto pages,

Code: Select all
<cms:pages masterpage='adsense/index.php' >
<div id="tt_area">
<div id="feature_title"><a href="<cms:show adlink />" target="_blank">
  <cms:excerpt count='40' truncate_chars='1'><cms:show k_page_title /></cms:excerpt></a></div>
<div id="feature_image"><img src="<cms:show ad_image />" alt="" width="90" height="80" /> </div>
<div id="feature_text"><cms:excerpt count='120' truncate_chars='1'><cms:show ad_content /></cms:excerpt></div>
</div>
</cms:pages>


adsense.jpg
adsense.jpg (78.87 KiB) Viewed 2450 times


KK sorted this out for me last time, bellow. and wasnt too sure if this could be done for showing random posts, so if refreshed it would change, and pick random clone pages and post the most random ones.



Code: Select all
<cms:php>

   $path_to_images = 'uploads/image/random-images/';
   $count_rand = 4; // Number of random images to pick
   
   // Valid extensions
   $extensions = array( 'jpg','jpeg','gif','png','bmp' );
   
   // Open folder and get files
   $images = array();
   if( $handle = opendir('<cms:show k_admin_path />' . $path_to_images) ){
      while (false !== ($file = readdir($handle))) {
         if( $file != "." && $file != ".." ){
            $ext = strtolower( substr(strrchr($file, "."), 1) );
            if( in_array($ext, $extensions) ){
               $images[] = '<cms:show k_admin_link />' . $path_to_images . $file;
            }
         }
      }
      closedir($handle);
   }
   
   // Select the indicated number of random images
   $count_rand = ( count($images)<$count_rand ) ? count($images) : $count_rand;
   $rand_keys = array_rand( $images, $count_rand );
   for( $x=0; $x<count($rand_keys); $x++ ){
      echo '<img src="'.$images[$rand_keys[$x]].'" /><br/>';
   }

</cms:php>
Hi Simon,

Displaying random posts has become easier now :)
Try this -
<cms:pages masterpage='adsense/index.php' orderby='random' limit='5'>
...
</cms:pages>

The orderby='random' parameter will take care of randomizing the posts fetched.

Hope this helps.
KK wrote: Hi Simon,

Displaying random posts has become easier now :)
Try this -
<cms:pages masterpage='adsense/index.php' orderby='random' limit='5'>
...
</cms:pages>

The orderby='random' parameter will take care of randomizing the posts fetched.

Hope this helps.


WOW!! i was exspecting a massive ball ache, you are outstanding! :o Words are unreal at the moment. haha!

massive help, massive..
Using 'random' ordering prevents caching does it not, so wouldn't this stop all caching if for example you had this snippet on every page? Would it not be helpful in light of this usage scenario to be able to control whether the randomization is done per-visit or only once, then cached?
@cheesypoof
Your are right - using this parameter will prevent the page it is used on from being cached. If you decide to use this snippet on all the pages - all the pages will remain uncached.

That is a tradeoff one will have to accept, I'm afraid.
You suggested
randomization is done...per-visit, then cached?

The idea of caching is to avoid loading the Couch core stack. It is checked at the very beginning of a request whether a cached copy is available - if it is, Couch's core files are not loaded and the cached copy is returned. This is extremely fast and that is what caching is all about.

To keep track 'per visit' would be too resource heavy and would beat the very purpose of using a cache.

randomization is done...only once, then cached?

Think about it, would this be randomization? A visitor gets to see a randomized copy and then always gets the same copy.

In short, there is no easy way out.
The caching feature as it stands now is fast and simple. In the future, we can go with caching discrete regions of a page and that would be the solution, I think.

Right now, what Simon could do is avoid using this snippet in *all* his pages.
It should be perfectly acceptable to use it on the home-page etc. One uncacheable page wouldn't matter.
My question/suggestion was constructed poorly. I meant to communicate that it may be helpful to be able to choose between the current method of per-visit randomization with no caching and one-time randomization with caching. (per-visit without caching) || (one-time with caching) :)

I definitely see the benefits of caching as you described, it is very fast. That's why it was my intention to question whether there was a way to preserve this functionality while using 'random' ordering. Caching and randomization are inherently conflicting concepts, so I understand your coding choice.

I still maintain though it would be useful in this situation to be able to choose one-time randomization with caching. If I was serving advertisements in this manner and on every page just like lots of blogs/websites, I would view my suggestion as a good option to have available. On a per-visit basis it would not be technically random as you said, but in totality across all of the cached pages, the advertisements would be random, which would be more ideal than sacrificing caching altogether or doing without randomization.

Sorry Simon, didn't intend to hijack the thread. ;)
@cheesypoof
OK, I see your point now :)

With the new features that debut with the coming version of Couch, the cache functionality will require changes too. I think we'll have a even better solution then (caching or not-caching specific regions from a page).
7 posts Page 1 of 1
cron