15.06.15

Connecting to your Homestead Laravel 5.1 VM with your trusty phpMyAdmin

- MySQL, PHP, Tutorial -

Suppose your developing with the php Laravel 5.1 framework for Nginx, and for that your using a vagrant VirtualBox virtual machine for Nginx.

Homestead/Vagrant is easy. You can follow this great set-up.

Laravel 5 uses database migration and has support for database factories. But still sometimes you wanna use your trusty phpMyAdmin for database management on the VM. Of course you can take the time and install phpMyAdmin on your new virtual machine, but I’m gonna give you a faster tip.

Why don’t you use your phpMyAdmin on your local server to manage the database on the VM.

For that edit the phpMyAdmin config file

vi /etc/phpmyadmin/config.inc.php

and add this after $i++

$i++
  $cfg['Servers'][$i]['host'] = '127.0.0.1'; //provide hostname and port if other than default
    $cfg['Servers'][$i]['port'] = '33060';      //user name for your remote server
    $cfg['Servers'][$i]['user'] = 'homestead';  //user name for your remote server
    $cfg['Servers'][$i]['password'] = 'secret';  //password
    $cfg['Servers'][$i]['auth_type'] = 'config';       // keep it as config

Now you can select the VM database in phpMyAdmin in the current server select field.

Neat.

16.10.13

Converting CSV to JSON with PHP

- PHP -

JSON is a little bit more verbose then csv (comma seperated values), but widely used nowadays on the net, because it format is somewhat like a literal Javascript object. Now Javascript and PHP have native JSON support it is a very good and handy format to exchange data, much less verbose then XML and easier to create and parse on the client.

How easy?

In PHP:

 json_encode ($array);
 json_decode($json);

In Javascript:


JSON.stringify(myObject)
JSON.parse(myJson)


But csv is also still widely used:
  • myslq dump
  • spreadsheet
To concert CSV to JSON in PHP is actually quite simple:
$file="mysqlTable.csv";
$csv= file_get_contents($file);
$array = array_map("str_getcsv", explode("\n", $csv));
print json_encode($array);

The first two lines are simple, and do not need any explaining.

Convert CSV into an array

The third line, converts the csv file into a multidimensional array

$array = array_map("str_getcsv", explode("\n", $csv));

Explode the csv string on `line ends` into an array of lines, and convert the lines themselves with the safe native function  `str_getcsv` to an array. Although it is tempting to parse a line of csv with

explode(",", $line);

always use the native functions for csv like, str_getcsv.

Convert an array into JSON

Once you have the $array, simply convert it to JSON:

print json_encode($array,JSON_PRETTY_PRINT);

JSON_PRETTY_PRINT is optional and will print the JSON with whitespace.

Use it for debugging, don’t use it in production, it will add extra whitespace and thus size to your output.

Create JSON/Ajax server response

header('Content-Type: application/json');

print json_encode($array);

Turns your server into Ajax serving JSON.

07.11.12

Using the new upcoming Netbeans 7.3 HTML5

- PHP -

The new version 7.3 of Netbeans will offer improved support for Javascript/HTML5 apps. It looks really promising: a lot of code completion, documentation that is available during editing, and a build-in PHP server.

If you edit JavaScript a lot you know what “use strict” means. It’s a pragma that will let the program you use the new ECMAScript 5 `strict mode`. ECMAScript is the official standard of JavaScript.

To turn into `strict mode` just start your script with “use strict”. Or type it as the first rule in a function, to use strict mode only in that function,
Strict mode will protect you, among other things, to accidentally declare a variable in the global scope.

function(){
text = "just temporary text used in this function"
}

Will define a global variable text, which is a bad thing, you forgot to write :

function(){
var text = "just temporary text used in this function"
}

Strict mode protects you from making these kind of mistakes, it will warn you, this code won’t run:

function(){
"use strict";
text = "just temporary text used in this function"
}

To add this useful command to the code snippets in Netbeans.
Go to Tools -> Options -> Editor -> Code Templates and add new
Abbrevation: us
Expanded Text: “use strict”;

Now you can type `us` hit TAB and voila: “use strict”; is added in your JavaScript file.

14.05.12

Tips for using the Netbeans editor for Kohana and Kostache / Mustache templates using `surround with`

- PHP -

A nice feature of NetBeans is the support for `code templates`: abbreviations that expand into snippets of code when the TAB key is pressed after typing the a shortcut in the editor.

You can type fn TAB and have it expand to

function blabla () {

}

That’s very convenient for long and often needed code phrases.

`Surround with` code templates

Another very useful feature is the `surround with` code snippet. It isn’t very well documented on the Netbeans PHP pages, well more or less not at all, but you can add your own snippets for `surround with` too.

If your using Mustache templates with the Kohana framework, you will find the next snippet extremely useful, when you’re updating your templates multilingual. Mustache templates are very clean and elegant template files that work with a myriad of languages like PHP, Python and Javascript. Also they escape code automatically, so I really can recommend them. No more logic in the HTML template files, well just the most basic.

For example you have this Mustache template:

<h2>Recipes</h2>
<ul>
{{#recipes}}
	<li>{{name}}</li>
{{/recipes}}
</ul>

To make it multilingual for use with Kostache we have to change it to:

<h2>{{#__}}Recipes{{/__}}</h2>

To do that in Netbeans add this to the code templates: tools -> options -> editor -> tab `Code templates` -> new

It doesn’t matter what you choose for abbreviation, because were not gonna use it as code completion with shortcut TAB, but surround with is triggered with SHIFT ENTER.

Choose something like translation, then paste this:

{{#__}}${selection allowSurround}{{/__}}


Now go back to your template, select Recipes, and press SHIFT ENTER or click the yellow light-bulb. That will offer the extra command `surround with` your snippet.

Not documented on the Netbeans PHP wiki, but extremely useful.

The needed PHP function in your view class:

public function __() {
return array('i18n', 'get');
 }
15.02.12

CSS vendor prefix macro for Netbeans

- Tutorial -

There is all about CSS vendor prefixes at the moment. PPK doesn’t like them at all, the W3C is afraid that webkit might rule the world, developers seem to find that webkit is the only mobile browser around.

Agreed, the CSS vendor prefixes make verbose code. It’s ugly, and sometimes it’s a lot of typing. But they pinpoint a real problem, browsers need them, and although maybe obsolete in the future, if you want your code cross browser now and future-proof you simply have to use them. No matter if they’re going to be abolished or not. No matter what solutions is to be found tomorrow.

But what most people fail to notice, that it’s actually an IDE/deployment problem. If IDE can spellcheck and code-complete why can’t they extend CSS prefixed rules.

Well they can. So we created a macro for Netbeans. Netbeans is our favourite PHP-IDE.  The macro recorder in Netbeans is easy and fast, so it suits.

Just add  a Macro in Netbeans: tools -> options ->  editor -> macros -> new.

Paste this


copy-selection-else-line-down copy-selection-else-line-down copy-selection-else-line-down copy-selection-else-line-down copy-selection-else-line-down caret-up caret-up caret-up caret-up caret-begin-line caret-forward delete-next "moz" caret-down caret-begin-line caret-forward selection-forward "ms" caret-down caret-backward selection-backward "webkit" caret-down caret-backward caret-backward caret-backward caret-backward caret-backward selection-backward "khtml" caret-down caret-backward caret-backward caret-backward delete-previous delete-previous delete-previous

And give it a proper shortcut CTRL + ] is free.

Then start writing your code for the least popular browser, but it has the shortest prefix vendor: Opera.


-o-transition: opacity 1s ease-in 1s;

Put your cursor soemwhere in the line and press CTRL + ]

You get this result


-o-transition: opacity 1s ease-in 1s;
 -moz-transition: opacity 1s ease-in 1s;
 -ms-transition: opacity 1s ease-in 1s;
 -webkit-transition: opacity 1s ease-in 1s;
 -khtml-transition: opacity 1s ease-in 1s;
 transition: opacity 1s ease-in 1s;

Probably khtml can be skipped, I don’t know the market share, but let’s try to support everything.

Has become hell a heaven. No but it isn’t that bad any more. Are they’re caveats. Sure, like different implementations, but that should be corrected by hand.

Important thing is, the rule without a prefix should end the list.

Categories
Archives
Links