Archive for the ‘undef’ Category

bad, { display: table } good

http://colintoh.com/blog/display-table-anti-hero

Advertisements

Dynamically Flip a Table (Interchange Rows and Columns)

Got a big table in a web page and would rather look at it with the rows and columns exchanged? i.e. If the headers are along the top, you’d rather they were along the left. Use this bookmarklet which asks for a (jQuery) CSS selector. The default CSS selector of “table” will flip the axes of every table on the page.

http://bookmarkify.it/41

If you don’t trust bookmarkify.it (but you do trust Google for the jQuery CDN), here’s some safe code:

javascript:var s=document.createElement('script');s.setAttribute('src','//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js');document.getElementsByTagName('body')[0].appendChild(s);$(function(){javascript:$(prompt("Flip Table(s) with CSS selector","table")).each(function(){var e=[];$(this).find("tr").each(function(){var t=[];$(this).find("td,th").each(function(){t.push($(this))});e.push(t)});$(this).empty();var t=0;while(true){foundOne=false;var n=$("<tr/>");for(var r=0;r<e.length;r++){if(e[r].length>t){e[r][t].appendTo(n);foundOne=true}else{$("<td/>").appendTo(n)}}if(!foundOne){break}n.appendTo($(this));t++}})});

PHP sort return values

$fruits = array("lemon", "orange", "banana", "apple");
print_r(chain_user_func('sort', $fruits));

function chain_user_func() {
$args = func_get_args();
$func = array_shift($args);
$args[0] = &$args[0];
call_user_func_array($func, $args);
return $args[0];
}

Network Solutions FTP censorship

A funny thing happens if you try to upload a file to a Network Solutions FTP site with the following string in it:

.addcslashes(htmlspecialchars

The FTP session times out and the file is only partially written. A simple solution is to add whitespace:

. addcslashes(htmlspecialchars

Go figure.

Inheritance is overrated; prefer composition wherever it makes sense.

array_flatten() in PHP

$arrays ? call_user_func_array(‘array_merge’, $arrays) : array()

How to sort a PHP array-of-arrays

array_multisort(
array_pluck($records, 'field_to_sort_on'), SORT_ASC,
//array_pluck($records, 'another_field_to_sort_on'), SORT_ASC,
//array_pluck($records, 'another_field_to_sort_on'), SORT_DESC,
$records
);

/*

NOTE: this requires array_pluck():

function array_pluck($recordList, $targetField) {
$result = array();
foreach ($recordList as $recordKey => $record) {
if (!array_key_exists($targetField, $record)) { continue; }
$result[ $recordKey ] = $record[$targetField];
}
return $result;
}

*/