Scale div / element and maintain aspect ratio with and without box-sizing and padding-top

Kind of ran across this by accident today. I have looked for solutions for this from time to time and always ended up using some sort of hack, but this works, no hacks.

I need this when I want a rectangle to keep it’s ratio without having an image nested inside, but instead using the image as the background.

Use case for this today was a responsive Flexslider implementation with a different set of images with different ratios shown with a media query for smaller devices.

This works with and without box-sizing but if you look at the element in your inspector, box-sizing makes more sense.

There may already be a solution or the same post somewhere else, if so, let me know.

.scales { 
box-sizing: border-box; 
max-width: 100%; 
padding-top: 25%; 
border: 1px solid red; 
}
<div class="scales"></div>

Squeeze your browser and check it out. I didn’t test in every browser.

GoDaddy HTML 5 Boilerplate htaccess file 500 error

I recently had to deal with a client’s site on shared hosting that was returning a 500 error.

After a little diggining I found the offender was the .htacess that comes with HTML5BP.

If you are experiencing the same issue due to some changes GD made,  try commenting out these parts of the .htaccess file:

# AddType text/x-vcard vcf

# HTML, TXT, CSS, JavaScript, JSON, XML, HTC:

# <IfModule filter_module>
# FilterDeclare COMPRESS
# FilterProvider COMPRESS DEFLATE resp=Content-Type $text/html
# FilterProvider COMPRESS DEFLATE resp=Content-Type $text/css
# FilterProvider COMPRESS DEFLATE resp=Content-Type $text/plain
# FilterProvider COMPRESS DEFLATE resp=Content-Type $text/xml
# FilterProvider COMPRESS DEFLATE resp=Content-Type $text/x-component
# FilterProvider COMPRESS DEFLATE resp=Content-Type $application/javascript
# FilterProvider COMPRESS DEFLATE resp=Content-Type $application/json
# FilterProvider COMPRESS DEFLATE resp=Content-Type $application/xml
# FilterProvider COMPRESS DEFLATE resp=Content-Type $application/xhtml+xml
# FilterProvider COMPRESS DEFLATE resp=Content-Type $application/rss+xml
# FilterProvider COMPRESS DEFLATE resp=Content-Type $application/atom+xml
# FilterProvider COMPRESS DEFLATE resp=Content-Type $application/vnd.ms-fontobject
# FilterProvider COMPRESS DEFLATE resp=Content-Type $image/svg+xml
# FilterProvider COMPRESS DEFLATE resp=Content-Type $image/x-icon
# FilterProvider COMPRESS DEFLATE resp=Content-Type $application/x-font-ttf
# FilterProvider COMPRESS DEFLATE resp=Content-Type $font/opentype
# FilterChain COMPRESS
# FilterProtocol COMPRESS DEFLATE change=yes;byteranges=no
</IfModule>

Quick and dirty way to migrate from old Coldfusion / SQL Server to PHP / MySQL using jSON

There may be another way to do this out there, but when I needed it, I couldn’t find an easy free way to do it, so I made one.

You first need to create your new tables in your new MySQL DB and make sure you have allow_url_fopen on in htaccess or in your PHP config. I use PDO so you may have to modify the code if you don’t.

Step 1: The CF file

I made one of these for each table I wanted to move. Map the jSON key to the new column names in your new DB.

<cfquery datasource="yourDSN" name="qName">
 select * from table order by id
 </cfquery>
 [
 <cfoutput query="qName">
 {
 "id":"#URLEncodedFormat(id)#",
 "col1":"#URLEncodedFormat(col1)#",
 "col2":"#URLEncodedFormat(col2)#"
 }
 <cfif currentrow neq recordcount>,</cfif>
 </cfoutput>
 ]

 Step 2: The PHP file

You can run this in your browser using https://yourserver/import.php?records=yourCFfilenameonyourremoteserver

function get_web_page($url)
{
	$options = array( 'http' => array(
			'user_agent' => 'spider', // who am i
			'max_redirects' => 30, // stop after 10 redirects
			'timeout' => 220, // timeout on response
		) );
	$context = stream_context_create( $options );
	$page = @file_get_contents( $url, false, $context );

	$result = array( );
	if ( $page != false )
		$result['content'] = $page;
	else if ( !isset( $http_response_header ) )
			return null; // Bad url, timeout

		// Save the header
		$result['header'] = $http_response_header;

	// Get the *last* HTTP status code
	$nLines = count( $http_response_header );
	for ( $i = $nLines-1; $i >= 0; $i-- )
	{
		$line = $http_response_header[$i];
		if ( strncasecmp( "HTTP", $line, 4 ) == 0 )
		{
			$response = explode( ' ', $line );
			$result['http_code'] = $response[1];
			break;
		}
	}

	return $result;
}

error_reporting(E_ALL);
ini_set("display_errors", 1);
ini_set('memory_limit', '128M');

require_once('db.php');

$records = $_GET['records'];

$inserts = get_web_page("https://pathtoyourserverwithcfandsql/".$records.".cfm");

print_r($inserts);

if ($inserts['http_code'] == 200)
{
	$inserts = $inserts['content'];
}

if (json_decode($inserts,true) == true)
{

	$insertsR = json_decode($inserts,true);

	foreach ($insertsR as $items)
	{
		$cols = array();
		$vals = array();

		foreach ($items as $key => $value)
		{
			$cols[] = $key;
			$vals[] = urldecode($value);
		}

		$colsI = implode(',',$cols);

		$questions = '';

		$valsC = count($vals);

		for ($i=1; $i <= $valsC; $i++)
		{
			$questions .= ($i < $valsC) ? '?,' : '?'; 		} 		print_r($cols); 		print_r($vals); 		echo "insert ignore into $records (".implode(',',$cols).") values ($questions)"; 		print_r($vals); 		$statement = $db->prepare("insert ignore into $records (".implode(',',$cols).") values ($questions)");
		$statement->execute($vals);

	}

}

else
{
	echo 'no bueno';
}

Canadian provinces in PHP multi-dimensional array

This was useful when using the shipping address PayPal returns for CA addresses to get rates from the UPS API. The UPS API seems to only accept CA provinces as the two-digit code, PayPal sends it back is full words.

$caProv = array("Alberta" => "AB",
"British Columbia" => "BC",
"Manitoba" => "MB",
"New Brunswick" => "NB",
"Newfoundland and Labrador" => "NL",
"Northwest Territories" => "NT",
"Nova Scotia" => "NS",
"Nunavut" => "NU",
"Ontario" => "ON",
"Prince Edward Island" => "PE",
"Quebec" => "QC",
"Saskatchewan" => "SK",
"Yukon" => "YT");
$state = ($httpParsedResponseAr['SHIPTOCOUNTRYCODE'] == "CA") ? $caProv[$httpParsedResponseAr['SHIPTOSTATE']] : $httpParsedResponseAr['SHIPTOSTATE'];

Sorting muilti-dimensional arrays in PHP

I ALWAYS forget this so I am posting it for myself and anyone else who stumbles upon it.

function subval_sort($a,$subkey) {
 foreach($a as $k=>$v) {
 $b[$k] = strtolower($v[$subkey]);
 }
 asort($b);
 foreach($b as $key=>$val) {
 $c[] = $a[$key];
 }
 return $c;
}
$songs = subval_sort($songs,'artist'); 
print_r($songs);
Array
(
 [0] => Array
 (
 [artist] => Fleetwood Mac
 [song] => Second-hand News
 )

 [1] => Array
 (
 [artist] => The Decemberists
 [song] => The Island
 )

 [2] => Array
 (
 [artist] => The Smashing Pumpkins
 [song] => Cherub Rock
 )

)

Taken from this site:
http://www.firsttube.com/read/sorting-a-multi-dimensional-array-with-php/

Thanks!

DuckDuckGo Automator Script for control-click search.

I have never made an Automator script before today, but I needed something.

If you use DuckDuckGo and don’t want to get too crazy about adding a search option on your contextual menu, you can create this script to add a feature just like the default “Search in Google/Yahoo/Bing”.

Open Automator and choose File > New and choose Service.

Scroll the left pane down to Run Shell Script and drag it over to the right pane.

You should be able to figure out the rest by looking at the example.

Once you save the script, it will appear when you select text and control-click. Depending on how many services are on your machine, it may be under the Services menu.