Smorgasborg - Random Objects

Random Affirmation 2010 Apr 21

Small project that, on click, loads new affirmations. A photo from Leah, some jQuery with AJAX, and voila. I like it.

If anyone has suggestions for a “random thing of the day” type thing, please let me know.

The JavaScript I code to attach the behavior is:

		$.ajax(
			{type: "POST"
			, url: "generate-affirmation.php"
			, beforeSend: function(xhr){
				$('#affirmation').html('<div class="loading"><img src="/affirmations/ajax-loader.gif" alt="loading..." border="0" height="24" width="24" /></div>');
			return xhr;
			}, success: function(txt){
				$('#affirmation').html(txt).fadeIn('slow');
			}
		});

And the PHP of generate-affirmation.php is:

$raw_affirmations = array();
$raw_affirmations = @file("affirmations.txt");
$raw_commented_affirmations = array();
$html_affirmations = array();

foreach($raw_affirmations as $raw_affirmation) {
	$raw_affirmation = trim($raw_affirmation);
	if((substr($raw_affirmation,0,1) != '#')) {
		if(strip_tags($raw_affirmation) != $raw_affirmation) {
			$html_affirmations[] = $raw_affirmation;
		} else {
			$html_affirmations[] = htmlentities($raw_affirmation);
		}
	} else {
		$raw_commented_affirmations = $raw_affirmation;
	}
}
print $html_affirmations[array_rand($html_affirmations)];
Joe Crawford blogged this at 1:49pm in 2010 in April. The 21st was a Wednesday. You are reading this 8 years later. Make a public comment. There are no comments Tweet. Direct message. Send email. It has hashtags→ .

Leave a Reply

Comments Open; Trackbacks Open.