Smorgasborg - Random Objects

April 21st, 2010

Random Affirmation

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('
loading...
'); 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);
	if1)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)];

References   [ + ]

1. substr($raw_affirmation,0,1) != '#'

Leave a Reply