CODING
-
(HTML) Form element arrays
Sometimes you might want to group form elements in arrays. Here’s a way to do that: [html] <form> <input type="text" name="textboxes[]"> <input type="text" name="textboxes[]"> <input type="text" name="textboxes[]"> </form> [/html] The structure of the $_POST array will then be: [php] Array ( [textboxes] => Array ( [0] => value 1 [1] => value 2 [2] =>…
-
(PHP) sorting an array on a specific value
Simple way to sort an array with usort on a specific value using a comparator. [php] usort($myArray, function ($a, $b) { return $a[‘foo’] > $b[‘foo’]; }); [/php]
-
(CSS) How to center a div without width
I needed to center a div with dynamic content, so I couldn’t set a fixed width. Since margin: 0 auto; really won’t do anything if the element doesn’t have a fixed width you can solve it like this: HTML [html] <div id="container"> <div class="centered">My dynamic content, that will be centerd</div> </div> [/html] CSS [css] .centered…
-
(PHP) Allow any delimiter in CSV file
In a Laravel app I made the users would upload CSV files. The problem was that fgetcsv only allows a single delimiter character, and it needs to be defined. The problem with that is that when a user exports a CSV from MS Excel – it could end up using a wide array of delimiters depending on the locality…
-
Test localhost email with a simplified SMTP server
For checking the emails going out from your local server (without actually sending them). Use papercut. https://papercut.codeplex.com/ If you’re using Laravel, then make sure to setup your app/config/mail.php correctly: ‘driver’ => ‘smtp’, ‘host’ => ‘localhost’, ‘port’ => <your papercut port>, ‘encryption’ => ”,
-
(PHP) Eloquent doodles for Laravel
[php] // Get model by primary key $myModel = MyModel::find($id); //Where can use short syntax for equal comparison. $myModels = MyModel::where(‘someAttribute’, ‘=’, ‘someValue’)->get(); $myModels = MyModel::where(‘someAttribute’, ‘someValue’)->get(); // Delete models $affectedRows = MyModel::where(‘someAttribute’, ‘someValue’)->delete(); // Select distinct $distincts = MyModel::distinct()->select(‘someAttribute’)->where(‘someAttribute’, ‘someValue’)->get(); // Select with Limit and offset $myModels = MyModel::limit(30)->get(); $myModels = MyModel::limit(30)->offset(30)->get(); [/php] Different…
-
Simple picture/file upload script
Here is a simple tutorial script that uploads a file to a folder on the server. In this example it is assumed that the file is an image and it will be outputted on screen in an img-tag. But the file uploading mechanics works for any file type.
-
How to make OSX use MAMP php instead of system version
Getting OSX to install Laravel properly is a minor nightmare. Here are some good instructions on how to get it to use MAMP php instead of the broken default one that comes with OSX. https://gist.github.com/irazasyed/5987693
-
(Android) Delay method call
This is how you could delay a method call for a given amount of time. I’ve used this to have an alert dialog pop up a few second after starting an activity. This will fire myMethod() after 3000 miliseconds: [java] Handler handler = new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { myMethod(); }},…
-
(Android) Check if Google Play Store is installed on device
Here’s a quick way to check if Google Play Store is installed. I use this to keep activities that use in-app billing from crashing on devices that doesn’t have Google Play Store. Works fine in the emulator as well. If the method returns false, I’ll just give the user an alert telling them they need…