Chunk arrays with php
For a while now I have been using a bit of code for reading the contents of a directory, pushing the found items into an array, then looping over the array with a foreach to create markup for a slideshow, header images, backgrounds, etc. A current project I am working on needs this same functionality but with a twist. I still need to loop over the directory but I need to group the contents into groups or chunks. Enter, PHP’s array_chunk(). This does exactly what I need, but to get the array’s contents neatly on screen I am nesting a set of foreach loops. One for the first subarray, then the other for that subarray’s contents. It goes something like this:
// Initialize the array to hold images $thelist = array(); // Open directory and add files into array if ($handle = opendir($dir_path)) { // Loop over the directory. while (false !== ($file = readdir($handle))) { // Don't include the "." and // ".." files. As well as the // DS_Store files left by the Mac OS if ( $file != "." && $file != ".." && $file != ".DS_Store" ) { array_push($thelist,$file); } } closedir($handle); } natsort($thelist); // Put the array in alphabetical order //shuffle($thelist); // Put the array in random order // Split array into chunks $subarray = (array_chunk($thelist, 5)); // Layout the contents echo "<ul>\n"; foreach ($subarray as $arraygroup) { echo "<li>"; foreach ($arraygroup as $arrayitem) { // Do pretty markup here echo "<p>Item: $arrayitem</p>"; } // END nest echo "</li>"; } // END main echo "</ul>\n";Published on August 31, 2011
Twitter RSS Feed
Back in the good ‘ol days finding your Twitter RSS feed was quite easy, as it was displayed on your profile page. Now that Twitter is all grown up, things aren’t so easy anymore.
So, thanks to Google and the internet community I’ve found a current solution that I’m posting here for the sake of posterity.
1) RSS Feed URL structure: http://twitter.com/statuses/user_timeline/[YOUR TWITTER ID].rss
2) Get your Twitter ID from here: idfromuser.com
There you have it. Now plug that in to your script/class/etc.
Published on August 24, 2011UAR 2011
Did the New Belgium Urban Assault Ride again this year with my friend Chris. With a new strategy for the course route we did much better.
Men’s Class 13th
Place overall 22nd
Bib Num 116
Team: Repetition Sucks Sucks
Time: 1:46:53.2
Max lifetime for a PHP session
I was receiving complaints about a multi- page form I developed for a client. I couldn’t replicate the error, so I set out to research the ins-and-outs of PHP sessions. I’ve come to find out that any session data that is set will expire in the default setting of 24 minutes (1440 seconds) which is set in php.ini. To extend that time you have to either update the php.ini file (not a good idea) or update your script with ini_set() which will set the max lifetime for a session for your script. Simply call it on all the pages of your script with:
< ?php ini_set("session.gc_maxlifetime", "5400"); ?>
Now your users can spend, in my case, 90 minutes on the page without their session data expiring.
Published on July 11, 2011User Agent Targeting with PHP
I’m currently working on a site for a client and they have attached PDFs that open in modal windows, but have included language for users to right-click and download them adjacent to the links. I used the generic language “Save link as …” but my client couldn’t find that option in the right-click contextual dialog. So, I thought to myself, “hmm, how should I fix this for them?”
So, I wrote a quick little PHP function to produce User Agent specific “download this link” language.
function contextual_download_text(){
if(isset($_SERVER['HTTP_USER_AGENT'])){
$agent = $_SERVER['HTTP_USER_AGENT'];
}
if(preg_match('/.*?Firefox/i',$agent)){
// Firefox
$dl_link = "Save Link As ...";
} elseif (preg_match('/.*?MSIE/i',$agent)) {
// IE
$dl_link = "Save Target As ...";
} elseif (preg_match('/.*?Chrome/i',$agent)) {
// Chrome
$dl_link = "Save link As ...";
} elseif (preg_match('/.*?Safari/i',$agent)) {
// Safari
$dl_link = "Download Linked File As...";
} else {
// default to IE, why not?
$dl_link = "Save Target As ...";
}
return $dl_link;
}
Then echo the function in your markup.
< ?php echo contextual_download_text(); ?>
Published on June 29, 2011
Add Post Thumbnails to RSS Feeds in WordPress
function rss_post_thumbnail($content) { global $post; if(has_post_thumbnail($post->ID)) { $content = '<p>' . get_the_post_thumbnail($post->ID) . '</p>' . get_the_content(); } return $content; } add_filter('the_excerpt_rss', 'rss_post_thumbnail'); add_filter('the_content_feed', 'rss_post_thumbnail');Published on June 14, 2011
Regex Reverse Matching
The fact that regex doesn’t support inverse matching is not entirely true. You can mimic this behavior by using negative look-arounds:
^((?!mystring).)*$
The regex above will match any string, or line without a line break, not containing the (sub) string ‘mystring’. This is not something regex is “good” at (or should do), but still, it is possible.
Thanks to a poster at stackoverflow.com for the solution.
Published on June 14, 2011Adding media upload capabilities to contributor role in wordpress
Here’s the trick:
if ( current_user_can('contributor') && !current_user_can('upload_files') )
add_action('admin_init', 'allow_contributor_uploads');
function allow_contributor_uploads() {
$contributor = get_role('contributor');
$contributor->add_cap('upload_files');
}
Published on June 14, 2011
Listening Habits
I love data. I’m also quite privy to the visualization of data. When I can mix this with my daily music listening habits, the thought just gets me all tingly. Enter the last.fm playground – there’s all sorts of fun to be had. This visualization in particular was of note to me, because according to the data, I should be a gender neutral fluid 24 year old.
Moving Violations
Ouch! Austin bike laws have officially been updated. No more running stop signs I guess.
Published on March 3, 2011