PHP and Rotation

I’m on my first of six days off from work, and already the lack of four to six hours of daily programming is getting to me. Aaaah! Must program. Need programming to do… Then I saw this post by CCY of Mega Megane Moe. Looking at the code included there, my eye glistened, and I went at it faster than Sayo-pe on a detective case. (Just finished reading book nine of GALS! today… One more to go!)

Here’s how I envision the code CCY’s presented:

echo print_from_rotation('ccy_rotate.ini', '<h3>Do you read... <a href="[link]">[title]</a>?</h3><p>[text]</p>');

// $rotation_ini_file: Define the server path/file name for the INI file containing site data.
// $rotation_markup: Define the markup to insert the site's data into. Use [title] for the section text.
// $rotation_section_name: Define the text to replace with the section's text.
function print_from_rotation($rotation_ini_file, $rotation_markup, $rotation_section_name = 'title')
    // Read the lines from the INI file into an array
    $all_data = parse_ini_file($rotation_ini_file, true);
    // Pick a section get its details.
    $section = array_rand($all_data);
    $values = $all_data[$section];

    // Replace keys with their values.
    $rotation_markup = str_replace("[$rotation_section_name]", $section, $rotation_markup);
    foreach ($values as $key=>$value)
        $rotation_markup = str_replace("[$key]", $value, $rotation_markup);

    return $rotation_markup;

I know! The layout cuts it off! The code can be viewed as a text file, and saved from there. Since this code is written from scratch by me, it’s placed here as public domain. It relies on an INI file. Hopefully CCY doesn’t mind me using his data set here, and he retains complete copyright over it. (If he complains, I’ll replace it!)

The basic idea is this: An INI file has multiple sections of data, which appear as such:

[Mega Megane MoƩ]
link =
author = CCY
text = A wonderful site run by a spectacular person whom I’m most certain won’t mind my using his random blog text in the sample INI associated with this post! (Maybe…?)

When calling the script’s function, pass it the filename of the INI file (relative to the file on the server), and the markup to insert the data into. In this example, the data is “link”, “author”, and “text”, but it can be anything one wants to include. The section name (the part in square brackets) is considered to be “title”. (This can be changed to something other than “title”. Just pass the text to use as a third parameter to the print_from_rotation() function). The function will return a string variable with the marked up text. You can store this in a variable to use later, or just print it out right from the function call (as done in the example function call in the PHP file.)

The markup is a template of HTML to put the values into, with [title] inserted where the section text should go, and (in the case of this INI), the values [link], [author], and [text].

Since one can add in any fields (such as link, author, and text) they want, this code can be adapted to use in other ways, such as CCY’s anime recommendation rotation, or even a random image. (Maybe even CCY will find use for the code?)

Ah, five and a half more days of rest and relaxation. Maybe I’ll do the Linus Torvalds thing and write an operating system kernel or something… (But probably not!)

Edit: If anyone wants to improve on the script, here are some things it can use:

  • Verification that an INI file exists, and return an appropriate error message string if not found.
  • Utilize a global array storing the loaded files, with the keys being the filenames. This allows using the same file twice in one page load (optionally with removing the randomly picked data from the array so it won’t be picked again) with the benefit of loading the file only once and the cost of storing it in memory.

2 Responses to “PHP and Rotation”

  1. CCY Says:

    Good to see that I’m not the only one who will impulsively start programming whenever I see something interesting.

    And a great job you did! I’m ashamed as a programmer to have had to resort to 40 nested ‘if’ statements, out of sheer laziness, but yours makes it easily modifiable and readable.

    I’ll have to kick it around later and get it working on my blog. Thanks for the hard work!

  2. Chris Says:

    The if statements did make me wonder if you’d never met the switch =P (I’ve been working in VB.Net so long, I almost said select here. Eep!)

    Impulse programming. That’s definitely what it is.

    As for laziness, I’m sure I can do a lot to improve this. For one, there’s no error checking on it the file to load cannot be found.

    Yeah, the enemy of the impulsive programmer is the lazy programmer. Once “it works for me”, it’s done =D