Posts

Link Post: The Ultimate Guide to Solving iOS Battery Drain

Helpful if you have an iOS device.

http://www.overthought.org/blog/2014/the-ultimate-guide-to-solving-ios-battery-drain

Installing a Specific Version of a Gem

Last week, I needed to setup a new VM with a copy of the STAGES website so I could do some testing. We use SASS to preprocess our CSS files but I started running into weird errors when tried to compile them. In order to fix this I had to install the same version we were using on our webserver into the VM. In order to do this you need to pass the -v flag:

gem install sass -v 3.2.12

Link Post: Your Greatest Code Quality Threats and How to Solve Them

I find that heterogeneity can really hurt productivity. I'll sometime brood over how someone didn't use braces for an if statement or didn't tab the correct number of times (really, how hard is this!) and I'll get lost in the fact that I'm there looking to fix a bug. The other items on this list are good too.

http://blog.codacy.com/2014/06/19/your-greatest-code-quality-threats-and-how-to-solve-them/

A Fix For MySQLs 1292 Error

I was trying to add a new column to an old table and I ran into a problem:

Error running query: ALTER TABLE table ADD COLUMN column1 VARCHAR(255) NOT NULL
SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect date value: '0000-00-00' for column 'column' at row 2

It turns on something similar to this problem has existed for a long time (http://bugs.mysql.com/bug.php?id=33240) and the fix is to run the command below before running the alter:

set SQL_MODE = '';

Link Post: Don’t Reset My Password

An interesting read. 'Don’t use security questions' is one I think all developers should LEGALLY be required to adhere to.

http://kennethjorgensen.com/blog/2014/dont-reset-my-password/

Looping Your Unit Tests

When I write my code I like to have my unit tests run automatically but sometimes I need to focus on a single test but I don't want to tab to the window where you're running your unit tests to run them (I know, how lazy can you be). When this happen I like to do two things.

The first is that I can run phpunit in a continuous loop with a one second delay so I can read the screen:

while(true); do vendor/bin/phpunit -c tests --filter <filter>; sleep 1; done

This works well but you end up having the tests run constantly which can drain your battery (if like me you aren't plugged in every so often). In this case I use the following:

while(true); do vendor/bin/phpunit -c tests --filter <filter>; read -p "Press Enter..."; done

Which will wait for an enter key before running the tests a second time.

Link Post: Subtle Patterns

A huge list of patterns that you can use as background images for your website.

http://subtlepatterns.com/

Via Reddit

Working With CSV Files in PHP

Working with CSV files sucks. You have to handle different line endings and entries can be wrapped in double quotess which could have a comma inside of it preventing you from just using explode(). Thankfully, PHP provides two functions for working with CSV files that are super helpful.

fgetcsv

The first is fgetcsv which reads from a file handle and brings in single line broken into an array. If you've every worked with processing csv files before you know how awesome this is. As an example,

// this prevents problems with different line endings
// see http://www.thisprogrammingthing.com/2012/oddity-with-fgetcsv/
ini_set('auto_detect_line_endings', true);

$file = fopen('filePath', 'r') or die('Unable to open file!');

while(($row = fgetcsv($file)) !== false){
    // work with the lines here
}

fclose($file);

fputcsv

The other is fputcsv which writes to a file with the correct formatting for a CSV file:

$file = fopen('filePath', 'w') or die('Unable to open file!');

foreach($array as $row)
    fputcsv($file, $row);
}

fclose($file);

Output to the Browser

Occasionally, we want to create a CSV file for a user to download. We could save the file to disk and then redirect to the output but I would rather just output the results:

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"output.csv\";" );
header("Content-Transfer-Encoding: binary"); 

// we're going to write directly to the output buffer
$file = fopen('php:://output', 'w') or die('Unable to open file!');

// output the header row
fputcsv($file, array_get_keys($array[0]));

foreach($array as $row)
    fputcsv($file, $row);
}

fclose($file);

Converting a CSV File Into an Associative Array

Using fgetcsv we can convert a CSV file into an associative array. I like this approach because then it doesn't mater what order the columns are in the CSV.

// this prevents problems with different line endings
// see http://www.thisprogrammingthing.com/2012/oddity-with-fgetcsv/
ini_set('auto_detect_line_endings', true);

$file = fopen('test.csv', 'r') or die('Unable to open file!');

$returnVal = array();
$header = null;

while(($row = fgetcsv($file)) !== false){
    if($header === null){
        $header = $row;
        continue;
    }

    $newRow = array();
    for($i = 0; $i<count($row); $i++){
        $newRow[$header[$i]] = $row[$i];
    }

    $returnVal[] = $newRow;
}

fclose($file);

// $returnVal now contains the contents of the CSV file
var_dump($returnVal);

Troubleshooting No Diskspace

A couple weeks ago I was working on an older development VM that was setup with a smaller hard drive and I started getting unexpected end of file error messages. I couldn't find the problem in my file so I was going to run git diff on the file to see what I've changed. I started do this and ran into a problem:

user@VM:/var/www/$ ls -l <tab>bash: cannot create temp file for here-document: No space left on device
bash: cannot create temp file for here-document: No space left on device

Shit!

Well, that explains why I couldn't find the problem...
Read More

Link Post: Bootstrap 3 Tips and Tricks You Might Not Know

http://scotch.io/bar-talk/bootstrap-3-tips-and-tricks-you-might-not-know

RSS

Join Our Mailing List!

View previous campaigns.

Top Posts

  1. Working With Soft Deletes in Laravel (By Example)
  2. Fixing CMake was unable to find a build program corresponding to "Unix Makefiles"
  3. Upgrading to Laravel 8.x
  4. Get The Count of the Number of Users in an AD Group
  5. Multiple Vagrant VMs in One Vagrantfile
  6. Fixing the "this is larger than GitHub's recommended maximum file size of 50.00 MB" error
  7. Changing the Directory Vagrant Stores the VMs In
  8. Accepting Android SDK Licenses From The OSX Command Line
  9. Fixing the 'Target class [config] does not exist' Error
  10. Using Rectangle to Manage MacOS Windows

subscribe via RSS

All content copyright This Programming Thing 2012 - 2021
Blogging about PHP, MySQL, Zend Framework, MySQL, Server Administration and Programming in general