1.5M ratings
277k ratings

See, that’s what the app is perfect for.

Sounds perfect Wahhhh, I don’t wanna

Fixing the Closed Event for Bootstrap’s Alert Boxes

I’ve been working with Bootstrap 2.0 for the last few days and I came across an odd bug in the 2.0 and now the 2.0.1 release with the events triggered when you close an alert box. The problem I had related to some JavaScript I was using to set a fixed element on scroll which required a calculation of the element’s offset from the top of the window on load. This worked great unless there was an alert placed on the page before the element; once the alert was closed I had to find a way to recalculate the element’s offset. The closed event seemed like the perfect solution, but there in lies the problem - it doesn’t work. Take a look at the code that is responsible for removing the alert box (source: bootstrap.js; line: 108):

function removeElement() {
	$parent
		.trigger('closed')
		.remove()
}

As you can see the closed event is triggered before the element is removed. This means if I bind to the closed event and try to recalculate the element’s offset the alert box will still be present in the DOM making the recalculation erroneous. The solution I came up with was to add a new event triggered after the alert box was removed from the DOM, but use the $(window) instead of $parent:

function removeElement() {
	$parent
		.trigger('closed')
		.remove()
	$(window).trigger('alertclosed')
}

I then used jQuery’s bind() function to listen for the new alertclosed event:

$(window).bind('alertclosed', function () {
	// recalculate offset here
});

fin

UPDATE (2012-02-19 17:24:00) - I forgot to mention that the same issue affects the scrollspy plugin and you can use the new event to reset scrollspy (thanks to Fraxinus at Stack Overflow for the scrollspy reset):

$(window).bind('alertclosed', function () {
	// recalculate offset here
	// reset scrollspy; thanks for Fraxinus:
	$('[data-spy="scroll"]').each(function () {
		$(this).scrollspy('refresh');
	});
});
Bootstrap jQuery JavaScript

PHP Bingo Cards

PHP Bingo Cards

My wife is the room parent for my daughter’s first grade class, and an hour ago she was trying to print off bingo cards for the kid’s Valentine’s Day party this week. The problem was the site she was using to print the template had a link to a site that no longer existed. Using the Wayback Machine I was able to find a cached version of the cards, but it was a static image, which means every card would be the same - what fun would that be? So, after taking a quick look at the card I told her to wait ten minutes and I’d work out a solution; here is what I created (use the refresh button to randomize the cards).

It was a simple enough bit of code, but I thought I’d share it in case others are looking for a way to create BINGO cards from home. You can view the source at the link above to see how I styled the page, but lets peek under the hood at the PHP code I created to generate these cards.

First the full code, and I apologize for this not being color coded [download a copy here]:

# create array of numbers for the cards, 1-75
$numbers 	= range(1,75);

# setup templates
$table 		= '<table cellspacing="0" cellpadding="0"><tr><th>H</th><th>E</th><th>A</th><th>R</th><th>T</th></tr>%rows%</table>';
$row 		= '<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>';
$center 	= '<tr><td>%s</td><td>%s</td><td>FREE</td><td>%s</td><td>%s</td></tr>';

# vars to hold formatted templates after loops
$cards 		= '';
$rows 		= '';

# shuffle array to randomize numbers
shuffle($numbers);

# set a counting var for moving through the numbers array
$n = 0;

# four cards to a sheet, create a four step loop
for ( $c=0; $c<4; $c++ )
{
	# for each card loop through five times to build each row of numbers
	# start iteration at 1 to make it easier to read
	for ( $i=1; $i<6; $i++ )
	{
		# if this is the center row (3) use a different template with free space
		$template = ( $i==3 ) ? $center : $row;
		
		# create array to hold numbers for this row
		$num = array();
		
		# loop through the numbers array five at a time to build row numbers
		for ( $d=0; $d<5; $d++ )
		{
			# if the number counter is already at the end of the numbers array reset and rehuffle
			# important so the fourth card properly displays and isn't a duplicate of the first card
			if ( $n == 75 ) 
			{
				shuffle($numbers);
				$n = 0;
			}
			
			# add number to array for the row and increment pointer for numbers array by 1
			$num[] = $numbers[$n];
			$n++;
		}
		# add row to rows var
		$rows .= sprintf($template,$num[0],$num[1],$num[2],$num[3],$num[4]);
	}
	# add card to cards var and reset rows var for next card pass
	$cards .= str_replace('%rows%',$rows,$table);
	$rows = '';
}

Let’s take this one step at a time, first I create an array holding 75 numbers:

# create array of numbers for the cards, 1-75
$numbers 	= range(1,75);

This will be used to populate the rows of our cards. Next I setup the templates for output using the PHP sprintf format:

# setup templates
$table 		= '<table cellspacing="0" cellpadding="0"><tr><th>H</th><th>E</th><th>A</th><th>R</th><th>T</th></tr>%rows%</table>';
$row 		= '<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>';
$center 	= '<tr><td>%s</td><td>%s</td><td>FREE</td><td>%s</td><td>%s</td></tr>';

You can replace the text in the $table var’s table header cells to something more appropriate, but as I stated this was for the kid’s Valentine’s Day party so my wife wanted it to say HEART instead of BINGO. Next I setup a few vars that will hold our formatted output, randomize the array using the PHP shuffle function, and set a counting var that will enable the script to iterate through the $numbers array as we loop through each row and card:

# vars to hold formatted templates after loops
$cards 		= '';
$rows 		= '';

# shuffle array to randomize numbers
shuffle($numbers);

# set a counting var for moving through the numbers array
$n = 0;

Now we begin our loops, first we want four cards to a page to cut down on printing, so we build a simple for loop and begin with the first card:

# four cards to a sheet, create a four step loop
for ( $c=0; $c<4; $c++ )

Next we loop through each row on the card:

# for each card loop through five times to build each row of numbers
# start iteration at 1 to make it easier to read
for ( $i=1; $i<6; $i++ )

I decided to start the counting at 1 to make it easier to read, because on the third row we want to use the center row template setup above which has the free spot:

# if this is the center row (3) use a different template with free space
$template = ( $i==3 ) ? $center : $row;

Now lets build a new array that will hold the 5 numbers we’re going to grab from the shuffled numbers array to use in this row, then setup a for loop to go through the first 5 numbers in the numbers array:

# create array to hold numbers for this row
$num = array();

# loop through the numbers array five at a time to build row numbers
for ( $d=0; $d<5; $d++ )

Now, since I’m only using 75 numbers I want to check if the array pointer is at the end of the array, if so I’ll reset the pointer and reshuffle the array so as to not end up with the first and fourth cards as identical twins. Then we add the number from the numbers array to the temporary array we setup and increase the counter by 1:

# if the number counter is already at the end of the numbers array reset and rehuffle
# important so the fourth card properly displays and isn't a duplicate of the first card
if ( $n == 75 ) 
{
	shuffle($numbers);
	$n = 0;
}
			
# add number to array for the row and increment pointer for numbers array by 1
$num[] = $numbers[$n];
$n++;

To finish the row we format it using sprintf and add it to the $rows var:

# add row to rows var
$rows .= sprintf($template,$num[0],$num[1],$num[2],$num[3],$num[4]);

Finally we end the card loop by replacing the %rows% string in our table template and adding the card to the $cards var, then reset the $rows var for the next card:

# add card to cards var and reset rows var for next card pass
$cards .= str_replace('%rows%',$rows,$table);
$rows = '';

The only thing required to display the cards is to copy the CSS from the finished page and echo/print the cards to your HTML BODY tag:

<?php echo $cards; ?>

That’s it! 

I know there are more elegant ways to pull this off, but I wasn’t going to spend time generating a function or get into OOP for a simple task. In the end my wife is very happy, and that’s all that matters ;)

Happy coding!

PHP Bingo
wearethe99percent
People like this should be ridiculed and made to look like the fools they are. Here is a woman who says she has a dual BA (doesn&rsquo;t say in what fields) and makes &ldquo;$52K+&rdquo; a year and claims she pays 35% in taxes. She &ldquo;need[s] help&rdquo; and wants the government to do something. Irony much? Let me explain why this woman needs a quick kick to reality:
For the moment lets assume her 35% tax figure is accurate (it isn&rsquo;t, and more on that in a minute, but stick with me) take a look at her expense sheet. There are some problems with her lifestyle choices that have landed her in this spot.
$1000 for rent? We have no idea where she lives so I&rsquo;ll skip this one for now.
$650 a month for her college debt. $650!? My wife graduated from TCU on loans from SallieMae and her monthly payments aren&rsquo;t $650 a month. What dual BA did she earn that gave her the earning power to make $52K a year and what overpriced school did she get those degrees from? Obviously choosing a better field of study while racking up expensive college fees and loans would have made more sense. Attending a community college would have been a better investment.
$550 for bills and utilities. Living in an apartment that has a $1000 rent means this isn&rsquo;t all for electric/gas and water. She doesn&rsquo;t break out credit cards, cable, cell phone or internet, but my guess is half of that amount are things she can choose to stop paying for to help her bottom line but chooses not to.
$505 for car and insurance. Unless she has had a lot of accidents in her life she is obviously driving a car she can&rsquo;t afford.
$250 for gas for a 65 mile daily commute. Move closer to your work or buy a car with better gas milage (at $3.40/gal, $250 a month for gas for a 65 mile commute means she&rsquo;s getting about 17MPG; my 12 year old Mustang gets better MPG). If she can&rsquo;t do that, then she&rsquo;s better off occupying the White House and telling that occupant to start drilling.
$100 for tolls, see point 5 above about moving closer to work. Surely she can find a $1000 apartment closer to work, or if she&rsquo;s smart one that costs less.
Obviously our mystery woman has made some poor choices in her life and is now content with placing blame on others and requires the big caring government to help her out. How sweet. But what she&rsquo;s actually doing is asking you and I to bail her out for the ridiculous choices she has already made and continues to make by trying to live a lifestyle she obviously can&rsquo;t afford. 
Now remember that bit about her paying the ridiculous and outrageous 35% of her pay in taxes? I think it&rsquo;s safe to say that our mystery woman isn&rsquo;t an accounting major, because for a single individual the total federal income tax on $52,000 of income is $9,188 or 17.7% (source: IRS 1040 Instructions; page 80). But what about state taxes you say? Well there isn&rsquo;t a single state in the nation that taxes someone making $52,000 a year at a 17.3% rate (source: Tax Foundation). At most, if she is in a state with an income tax, she likely pays around an additional 4%. So she is either a liar or needs to stop using TurboTax.
Most of these 99% signs show a generation (mostly my own - sad to say) of Americans that have been spoon fed some ridiculous notion that they&rsquo;re entitled to someone else&rsquo;s life (the money you earn is what you receive in trade for giving up part of your life to work towards your pay) to bail them out from making poor decisions. We&rsquo;ve raised a generation of individuals that can&rsquo;t fathom the idea of failing, the &ldquo;participation&rdquo; and &ldquo;good feelings&rdquo; educational programs hoisted upon these people while kids in public school has produced an group aghast if things don&rsquo;t work out for them as described by their fairy tale spinning counselors: go to college and all will be yours! It&rsquo;s what I call the underwear gnomes guide to higher education:
Phase 1: Go to College
Phase 2: &hellip;
Phase 3: Profit!
These people seem to think that they deserve step 3 because no one explained step 2 to them: make good choices, work hard, be dedicated to your craft or profession and live within your means. Not only that, but in a delicious twist of irony, she wants the government to do something while bemoaning the fact that the government takes too much of her pay! If only there was a group she could support that is against high taxation. 

People like this should be ridiculed and made to look like the fools they are. Here is a woman who says she has a dual BA (doesn’t say in what fields) and makes “$52K+” a year and claims she pays 35% in taxes. She “need[s] help” and wants the government to do something. Irony much? Let me explain why this woman needs a quick kick to reality:

For the moment lets assume her 35% tax figure is accurate (it isn’t, and more on that in a minute, but stick with me) take a look at her expense sheet. There are some problems with her lifestyle choices that have landed her in this spot.

  1. $1000 for rent? We have no idea where she lives so I’ll skip this one for now.
  2. $650 a month for her college debt. $650!? My wife graduated from TCU on loans from SallieMae and her monthly payments aren’t $650 a month. What dual BA did she earn that gave her the earning power to make $52K a year and what overpriced school did she get those degrees from? Obviously choosing a better field of study while racking up expensive college fees and loans would have made more sense. Attending a community college would have been a better investment.
  3. $550 for bills and utilities. Living in an apartment that has a $1000 rent means this isn’t all for electric/gas and water. She doesn’t break out credit cards, cable, cell phone or internet, but my guess is half of that amount are things she can choose to stop paying for to help her bottom line but chooses not to.
  4. $505 for car and insurance. Unless she has had a lot of accidents in her life she is obviously driving a car she can’t afford.
  5. $250 for gas for a 65 mile daily commute. Move closer to your work or buy a car with better gas milage (at $3.40/gal, $250 a month for gas for a 65 mile commute means she’s getting about 17MPG; my 12 year old Mustang gets better MPG). If she can’t do that, then she’s better off occupying the White House and telling that occupant to start drilling.
  6. $100 for tolls, see point 5 above about moving closer to work. Surely she can find a $1000 apartment closer to work, or if she’s smart one that costs less.

Obviously our mystery woman has made some poor choices in her life and is now content with placing blame on others and requires the big caring government to help her out. How sweet. But what she’s actually doing is asking you and I to bail her out for the ridiculous choices she has already made and continues to make by trying to live a lifestyle she obviously can’t afford. 

Now remember that bit about her paying the ridiculous and outrageous 35% of her pay in taxes? I think it’s safe to say that our mystery woman isn’t an accounting major, because for a single individual the total federal income tax on $52,000 of income is $9,188 or 17.7% (source: IRS 1040 Instructions; page 80). But what about state taxes you say? Well there isn’t a single state in the nation that taxes someone making $52,000 a year at a 17.3% rate (source: Tax Foundation). At most, if she is in a state with an income tax, she likely pays around an additional 4%. So she is either a liar or needs to stop using TurboTax.

Most of these 99% signs show a generation (mostly my own - sad to say) of Americans that have been spoon fed some ridiculous notion that they’re entitled to someone else’s life (the money you earn is what you receive in trade for giving up part of your life to work towards your pay) to bail them out from making poor decisions. We’ve raised a generation of individuals that can’t fathom the idea of failing, the “participation” and “good feelings” educational programs hoisted upon these people while kids in public school has produced an group aghast if things don’t work out for them as described by their fairy tale spinning counselors: go to college and all will be yours! It’s what I call the underwear gnomes guide to higher education:

  • Phase 1: Go to College
  • Phase 2: …
  • Phase 3: Profit!

These people seem to think that they deserve step 3 because no one explained step 2 to them: make good choices, work hard, be dedicated to your craft or profession and live within your means. Not only that, but in a delicious twist of irony, she wants the government to do something while bemoaning the fact that the government takes too much of her pay! If only there was a group she could support that is against high taxation

wearethe99percent

These are the types of videos I love to show my classes, Play by Play sits down with Ryan Singer of 37signals as he works through a hypothetical design problem. To see how various designers attack issues like these is invaluable for students. I wonder if more web/UX designers would be willing to offer a couple of hours to walk through similar exercises over video chat with web design students?

web design