php


  • (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…

  • (PHP) Using route groups in Laravel to protect routes

    To protect routes, in Laravel, from unauthorized visits you can use route groups. All routes in group will be affected by the auth before filter. [php] Route::group(array(‘before’ => ‘auth’), function() { Route::get(‘controlpanel’, ‘CustomerPanel@showPanel’); Route::get(‘secret’, ‘Secret@showSecret’); Route::post(‘setup’, ‘SetupController@doSetup’); }); [/php]

  • (PHP) 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. [php] <?php if(isset($_POST[‘submit’])) { $temp_name = $_FILES["file"]["tmp_name"];…

  • 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

  • (PHP) Simple pagination

    Here’s a simple way to paginate when getting large amounts of posts from the database.

  • Generate dummy data

    Here’s a great tool for generating dummy data for your database etc. www.generatedata.com

  • Convert seconds to days, hours, minutes and seconds

    Converting seconds into days, hours, minutes and seconds int days = secondsTotal / 60 / 60 / 24; int hours = (secondsTotal / 60 / 60) % 24; int minutes = (secondsTotal / 60) % 60; int seconds = secondsTotal % 60;

  • Simple function to see if a number is Even or Odd.

    Sometimes you need to check if a number is even or odd. A simple way to do this is using the modulus operator (the %-sign). It will calculate and return the remainder when dividing a value with another value. So how do you use that to find out if a number is even or odd?…