Category Archives: CODING

(HTML) Form element arrays

Sometimes you might want to group form elements in arrays.
Here’s a way to do that:

<form>
    <input type="text" name="textboxes[]">
    <input type="text" name="textboxes[]">
    <input type="text" name="textboxes[]">
</form>

The structure of the $_POST array will then be:

Array
(
    [textboxes] => Array
        (
            [0] => value 1
            [1] => value 2
            [2] => value 3
        )

)

(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

<div id="container">
   <div class="centered">My dynamic content, that will be centerd</div>
</div>

CSS

.centered {
   margin: 0 auto;
   display: table;
}

(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 settings in windows.  It kind of sucks that MS won’t let the user choose this on export, but that’s how it is.

So I solved it the quick and easy way, by making a function that simply replaces any delimiter character to  my desired delimiter. (Note that the tab character is in double quotes, since it won’t be interpreted as tab otherwise):

    /**
     * Will replace a number of CSV delimiters to one specific character
     * @param $file     CSV file
     */
    private function replaceDelimiters($file)
    {
        // Delimiters to be replaced: pipe, comma, semicolon, caret, tabs
        $delimiters = array('|', ';', '^', "\t");
        $delimiter = ',';

        $str = file_get_contents($file);
        $str = str_replace($delimiters, $delimiter, $str);
        file_put_contents($file, $str);
    }

(PHP) Eloquent doodles for Laravel

// 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();

Different ways of getting a single model and checking if it’s there.


// 1
$model= MyModel::where('someAttribute', $someValue)->get();

if (!$model->isEmpty()) {
    $firstModel= $model->first()
}


// 2
try {
    $model= MyModel::where('someAttribute', $someValue)->firstOrFail();
    // Do stuff with model
} catch (ErrorException $e) {
    // Do stuff if it doesn't exist.
}


// 3
$models = MyModel::where('someAttribute', $someValue)->get(); // Collection of models
$models = MyModel::where('someAttribute', $someValue)->first(); // Single model or null

if (count($models)) {
    //Collection: to get the first item
    $users->first().
    
    //if you used first() just use the $models
}

(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
if(isset($_POST['submit']))
{
    $temp_name = $_FILES["file"]["tmp_name"]; // get the temporary filename/path on the server
    $name = $_FILES["file"]["name"]; // get the filename of the actual file
 
    // print the array (for reference)
    print_r($_FILES);
         
    // Create uploads folder if it doesn't exist.
    if (!file_exists("uploads")) {
        mkdir("uploads", 0755);
        chmod("uploads", 0755); // Set read and write permissions of folder, needed on some servers
    }
         
    // Move file from temp to uploads folder
    move_uploaded_file($temp_name, "uploads/$name");
    chmod("uploads/$name", 0644); // Set read and write permissions if file
 
}
?> 
     
<form action="" method="post" enctype="multipart/form-data">
    <input type="file" name="file" id="file" />
    <input type="submit" name="submit" value="submit"/>
</form>