Don’t Hard Code Your Age!

Even though this is not at all as important as not hardcoding copyright years. It can still a time saver if you happen to hard code your age into a biography on a website.

Lets make those ages DYNAMIC!

I wrote a little code snippet which updates your age automatically. All you do is enter your birth date and php does the rest! I will use my birthday in the example:

$birth_date = getdate(strtotime("August 1st 1988")); $current_date = getdate(time()); echo ($current_date['mday'] >= $birth_date['mday'] AND $current_date['mmonth'] >= $birth_date['mmonth']) ? ($current_date['year'] - $birth_date['year']) : (($current_date['year'] - $birth_date['year']) - 1);

This would return 21, which is how old I am currently. If the current time was July 31st 2009, it would return 20. I made sure it checked both the day and month, not just subtract the years, to figure out the date. Just replace “August 1st 1988″ with your birth date.

It’s that Simple.

CodeIgniter’s alternator() function


It’s surprising to me how often I find little functions for tedious tasks, that CodeIgniter already has built in. One of these functions is the alternator() function in the String Helper.

To begin using this function, make sure you have loaded the String Helper with the following code:

$this->load->helper('string');

What the alternator() function does is allow two or more items to be alternated between when iterating through a loop. Example from the CodeIgniter User Guide:

for($i = 0; $i < 10; $i++)
{
	echo alternator('string one', 'string two');
}

There is also no limit to how many parameters you can have:

...
echo alternator('one', 'two', 'three', 'four', 'five');
...

Put it to Use

What would you ever need that for? Well, what about if you are creating a list of items and every other needs class="alt" attached to it for styling differences? I run into this issue all the time.

This is how I used to do it:

<ul>
	<?php $count = 1; ?>
	<?php foreach($list as $item) : ?>
		<?php (empty($count)) ? $count = 1 : $count = 0; ?>
		<li <?=($count == 1) ? 'class="alt"' : ''?>>
			<?=$item?>
		</li>
	<?php endforeach; ?>
</ul>

And this is with the alternator() function:

<ul>
	<?php foreach($list as $item) : ?>
		<li <?=alternator('class="alt"', '')?>>
			<?=$item?>
		</li>
	<?php endforeach; ?>
</ul>

The alternator() function makes the ability to do this, much easier and cleaner than my original way. Hopefully I’ve helped someone out who had no idea this function was available.

The Poll Place Launched!

Hey everyone!

I’ve had this project cooking for a while now, and felt I needed to launch it sooner than later. So, here it is: The Poll Place (http://thepollplace.com)

Check it out! Make some Polls! Vote on others! But most of all, share it with others! My goal is to have this become a competitor to some of the big polling sites out there, namely Poll Daddy. So please, spread the word! There is a new kid in town!

I have got plenty of todo’s for The Poll Place, a couple major issues and a handful of minor issues. As the site grows you will notice new features being added that you can take advantage of.

I hope you all enjoy this site and use it often. Thanks in advanced for sharing it around!