Your web home for how to's for web beginners. A jargon-free zone dedicated to helping your web knowledge grow.

Help For Web Beginners

Press CTRL + D to
Bookmark this Page

Adding Random Elements to Gallery 2 Photo Pages

After spending the better part of a day trying to find a way to randomize the advertising on my Gallery 2 photo pages, I failed miserably. I had found it virtually impossible to combine php, smarty variables and my limited knowledge of both to actually get something to work.

I thought that there has to be a way, even if it isn't 100% random. So, I went back to the database fields and realized the item table held a 10-digit long time stamp for each image. Since the timestamp included seconds, I realized that I had just found a way to create a fairly random ad generator.

Large Timestamp + Modulous Qualifier = Random Number

With a little bit of trial and error and the use of the modulous qualifier, I found a way to execute seemingly random code on my Gallery 2 photo pages. I opted to allow for up to 100 random options to allow for future expansion, to help build a bit more randomness than 10 options would create and to allow for a bit of weighting of the options.

In case you are not familiar with the term, modulous is a mathematical term used with division which refers to the remainder. For example, 6 divided by 4 equals 1 with 2 remaining. It is the 2 remaining that the modulous qualifier would return. Therefore, if you are working with a ten digit number and only want to obtain the last two numbers, you would divide by 100 and use the remainder as your "random number".

Seems like this would work for any Smarty-driven content management program including Gallery 2 and other image scripts as well as directory scripts and a variety of others.

Random Code for Random Advertisements

I had four different advertisements I wanted to cycle. You could use more or less and would just need to change some of the numbers and the code I used. This technique would also work well if you only wanted to periodically show advertisements or other content on say every 5th image which will be explained later.

Keep in mind, if you are not using Gallery 2, your variable name will be different. Either ask in the support forums of your script for the variable name of a time stamp that you can use or use phpMyAdmin to find the field name of the timestamp or some other value that changes from record to record.

Find the photo.tpl file in your installation. It should be in the themes/<theme>/templates folder if you have not edited this file before. (I am using the matrix theme, so in my case the folder would be themes/matrix/templates)

If you have not edited the photo.tpl file before, save a copy of the file to the themes/<theme>/templates/local folder prior to making any changes and only edit the new file.

Place the following code within the photo.tpl file where you wish the random ad or other code to display. It might take a bit of trial and error to get the code placement just right.

{if $theme.item.originationTimestamp % 100 > 75}
random ad code 1 goes here
{elseif $theme.item.originationTimestamp % 100 > 50}
random ad code 2 goes here
{elseif $theme.item.originationTimestamp % 100 > 25}
random ad code 3 goes here
random ad code 4 goes here

The code shown above checks the variable $theme.item.originationTimestamp (the ten-digit time code associated with each photo in Gallery 2) and divides it by 100 which results in a number between 0 and 99 (the remainder of the date code divided by 100).

Following the example above, for every image's timestamp that ends in 76-99 it would execute random ad code 1, timestamps ending in 51-75 would cause random ad code 2 to be executed, timestamps ending in 26-50 would cause random ad code 3 to execute and timestamps less than 25 would cause random ad code 4 to execute.

Use more or less {elseif} statements as needed.

At this point you can either place the random ad or other code right within the if-then-else statement or have each random ad code in it's own template file.

If you want to use template files, create a new .tpl file in the same folder/directory as your photo.tpl (should be templates/local) and simply insert whatever code you wish to run. To call the template from your if-then-else statement, use
{g->theme include="NewTemplateName.tpl"}

Only Display Advertising Some of The Time

In the case of wanting to display advertising only on a percentage of your images, this method provides a fairly good solution. It will not however, ensure that you will only have one advertisement per five images viewed but it should come fairly close to only showing the advertisement 20% of the time.

{if $theme.item.originationTimestamp % 100 < 20}
random ad code goes here

If you want to show the random advertising or other code a different percentage of the time, replace the 20 in the above example with the appropriate value; i.e. 50 for 50%, 75 for 25% or 90 for 10% - use any number between 1 and 99.

I hope this tutorial will work for your situation. Please understand, I am providing this tutorial as-is without extended support. Working with the various CMS templates can be quite time consuming and varies greatly from script to script and I simply don't have the time to help everyone. Since it took me so long to figure this out on my own, I thought I would share a solution that worked for my needs; hopefully it will work for you as well. You are welcome to use the code without attribution but I request that you do not offer it as your own tutorial or "how to" and consider providing a link on your site to mine.

About the Author is a website dedicated to providing free, easy to understand, online How To's for true web beginners. While the materials are not free or available for reprints, they are offered freely for individual use. Please use the contact page to let Michele know if this tutorial has been helpful or if there are any other beginner web programming or MySpace related tutorials you would like to see.