x
 
Create New Page
 View Previous Changes    ( Last updated by tbritton )

Rotate Sidebar Ads Sequentially

Category:Tricks
Category:Images
Category:Advertising
Category:Coding
Category:Embeds
Category:EE Coding
Category:HowTo
Category:Organizing Data Display
Category:PHP

This article is clear as mud, but I wanted to get something up to share. I’ll write a clearer article later on.
.........................................

I like to stay free from using javascript, and get as much done as possible by using only PHP and ExpressionEngine native tags.

For display of sidebar ads, random is a nice parameter for use in the orderby parameter, but I needed a not-random way to rotate sidebar ads sequentially through X number of ad banners in several sections of a newspaper site. We needed to daily rotate the top ad to the bottom position, allowing the remaining lower ads to scurry upwards to take the upper positions in sequence.

[addendum: This is intended only for a rotate upon refresh. A timer-driven change-over without a manual refresh would likely require Javascript. PHP is only good for generating complete pages on-the-fly and sending them along at refresh time, as far as I understand it.

We’ve applied the rotate-per-minute version (see algorithms at bottom) so that visits to sub-sections show a rotation of ads, which is working quite nicely. A sub-section visit is, of course, a form of refresh (as it is a re-write of the same template code in a different moment).]

There were changing numbers of ads per newspaper section. I didn’t want to maintain this manually, as that would soon become a nightmare! So, this is how I automated it.

Categories were used to differentiate the ads among newspaper “departments”, and the ads all resided in one “ads_sidebar_ads” weblog. I limited the files to those in a category that had status=“open” to make a counter for the built-in total_results variable that was used to populate a PHP variable used for the offset and limit parameters in an embedded template.

I used PHP for the modulo function it makes available to us. Using modulo (the remainder= function in PHP) on the day-of-year numeral derived from the date function date(“z”), I could tell it how many days it had to rotate X number of ads (this being flexible, allowing new ads to be entered or removed or status set to closed with the code now adapting automatically).

Here is the main template that calls the functions and then calls the embedded template. Make certain you enable PHP for this template and set the parsing to “Output”:

// Change Category in Two Places 

{exp:weblog:entries weblog="ads_sidebar_ads" category="15" orderby="entry_id" sort="asc" dynamic="off" status="open"
// Category and Status limit the number of entries counted in total_results 

<?PHP
$dateR 
date("z"); // this gets the day-of-the-year numeral for the following operation
$offset = ($dateR "{total_results}"); // this divides the day-of-the-year number
// by the total results (status="open") and, using the modulo operator, gives the
// remainder of zero through whatever, which becomes the offset and limit in the
// embedded template workhorse.
$totalR "{total_results}"  // this isn't used yet, but might be useful
?>

{
/exp:weblog:entries} 

// <?PHP echo $dateR."  ".$date10."  ".$totalR; ?> a handy little tester 

// The all-important embed code:
{ embed="My_embed_templates/_sidebar_ads_rotator" theCat="15"  theOffset="<?PHP echo ($offset); ?>" theTotal="<?PHP echo $totalR; ?>" 

The embedded template (called “My_embed_templates/_sidebar_ads_rotator” above) takes this passed value called “theOffset” and uses the “offset=” parameter in the first exp.weblog.entries tag to move the sidebar ads down by the number of days needed to rotate through all of them, then uses the “limit=” parameter to show the remaining ads in the list.

That is, notice how in the first of the pair, we have:

offset=”{embed:theOffset}”

while in the second one we have:

limit=”{embed:theOffset}” used instead!

{exp:weblog:entries offset="{embed:theOffset}" weblog="ads_sidebar_ads" category="{embed:theCat}" orderby="entry_id" sort="desc" dynamic="off" status="open"}
{if ad_image}
<p>{if advertiser_url}<a href="{advertiser_url}" target="_blank">{/if}<img src="{site_url}images/advertising/sidebar/{ad_image}" border="0" alt="{advertiser_name}" />{if advertiser_url}</a>{/if}</p>{/if}
{
/exp:weblog:entries}

{exp
:weblog:entries limit="{embed:theOffset}" weblog="ads_sidebar_ads" category="{embed:theCat}" orderby="entry_id" sort="desc" dynamic="off" status="open"}
{if ad_image}
<p>{if advertiser_url}<a href="{advertiser_url}" target="_blank">{/if}<img src="{site_url}images/advertising/sidebar/{ad_image}" border="0" alt="{advertiser_name}" />{if advertiser_url}</a>{/if}</p>{/if}
{
/exp:weblog:entries} 

“ad_image”, “advertiser_url”, and “advertiser_name” are custom fields used in the ads_sidebar_ads weblog.

The use of the date(“z”) operator is handy to get the day-of-year numeral I needed, but one could also do the same thing with the hour or any other timestamp elements by altering the code just slightly.

Voila - true sequentially rotating ad banners that change daily to satisfy the advertisers’ needs to be on top occasionally!

—————————————————————————————————————-

Here’s some more algorithms for rotating every five minutes and for every minute.

{!-- 5-minute driven version --}
{exp
:weblog:entries weblog="ads_sidebar_ads" category="10" orderby="entry_id" sort="asc" dynamic="off" status="open"}
<?PHP
$minuteR 
date("i"); // this gets the day-of-the-year numeral for the following operation
$offset =  (floor ($minuteR 5) % "{total_results}"); // this divides the minute number by 5 (status="open") and using the modulo operator gives the remainder of zero through whatever
$totalR "{total_results}"  // this isn't used yet, but might be useful
$floorR = (floor ($minuteR 5)); // used in tester
?>
{
/exp:weblog:entries} 


{
!-- <?PHP echo $minuteR."  ".$offset."  ".$totalR."  ".$floorR?> a little tester --
{!-- by the minute version --}
{exp
:weblog:entries weblog="ads_sidebar_ads" category="8" orderby="entry_id" sort="asc" dynamic="off" status="open"}
<?PHP
$minuteR 
date("i"); // this gets the minute numeral for the following operation
$offset =  ($minuteR "{total_results}"); // this uses the modulo operator gives the remainder of zero through whatever changing by the minute
$totalR "{total_results}"  // this isn't used yet, but might be useful
$floorR = (floor ($minuteR)); // used in tester
?>
{
/exp:weblog:entries} 

 {
!-- <?PHP echo $dateR."  ".$offset."  ".$totalR."  ".$floorR?> a little tester --

Terry Leigh Britton

Category:EE1