September 22

Multidimensional to nested array

Let us take an example to demonstrate purpose of this function. There is a database table that holds visit count data of web site. It has page, visit_time and visitor_ip columns. Following MySQL query shows visit count per page per day:

Result is following data set:

pagevisit_datecnt
/home2015-11-111312
/about2015-11-114543
/contact-us2015-11-113131
/home2015-11-121233
/about2015-11-124324
/contact-us2015-11-125435

This is transformed to PHP array:

This data structure is inconvenient if you try to retrieve visit count for page at particular date. You have to loop throughout entire array and ask if page and visit_date have specified values. Nested array is more convenient in this situation because it provides lookup direct by keys:

Multidimensional array can be transformed do nested using following function:

July 8

Asynchronous PHP

I stumbled upon this excellent video while searching for React PHP tutorials. It shows implementation of asynchronous processes using curl_multi_exec, pcntl_fork, socket select and many other techniques and libraries. After discussing the pros and cons of each approach it shows proper way of asynchronous implementation using ReactPHP library.

March 25

jQuery promises

Sometimes it is required to execute callback right after the last of related Ajax call is completed. In this example there are 6 div elements (result1, result2, result3, result4, result5 and totals). There is external PHP script (call.php) which returns random number. Result DIV elements are populated asynchronously. Totals DIV element should contain sum of result DIV elements.

Ajax request is sent 5 times but it is unknown which request will be at the end. So it is questionable where to put code for calculating totals. Although setTimeout or global Ajax events can be utilized we should avoid them because of unpredictable results.

Fortunately, there is a JavaScript Promises functionality. jQuery has its own implementation which is well integrated with Ajax event handlers.

Callback function inside done() event will always execute right after last Ajax call.

March 25

Smart image field for Yii2

This is replacement of classic file upload HTML form element. Advantages of this field are:
– You don’t have to handle file upload yourself. It is encapsulated in ImageUploadAction.
– ImageUploadAction resolves upload name clashes by default.
– If existing model is edited, image field is pre-populated.
– Uploaded image won’t be lost if form has validation errors after submit.
– There is a delete image link besides upload button.
Installation
The preferred way to install this extension is through composer.
Either run

or add

to the require section of your composer.json file.
Usage
1) Attach ImageUploadAction to designated controller

2) Utilize SmartImageField inside ActiveForm

Screenshot
Screen Shot 2016-02-12 at 3.59.41 PM

Screen Shot 2016-02-12 at 3.59.24 PM
Github
https://github.com/mmedojevicbg/yii2-smart-image-field
Packagist
https://packagist.org/packages/mmedojevicbg/yii2-smart-image-field

Category: PHP | LEAVE A COMMENT
February 9

Textarea with tokens for Yii2

This is drop-in replacement for textarea form element. It provides tokens below form element. Text is automatically inserted into textarea by clicking one of tokens.

Installation
The preferred way to install this extension is through composer.
Either run

or add

to the require section of your composer.json file.

Usage

Screenshot
text-area-tokens

Github
https://github.com/mmedojevicbg/yii2-text-area-tokens

Packagist
https://packagist.org/packages/mmedojevicbg/yii2-text-area-tokens

Category: PHP | LEAVE A COMMENT
February 9

jQuery data attribute

I believe that most of you are familiar with jQuery data() function. A lot of things can be done in convenient way using this functionality. I will show you one example. Let’s assume that you are creating news list widget. Content of this widget is based on category id. So there are two elements, category links and news list box.
example
Each of category link has jQuery click handler attached to itself. Click event triggers news box refresh.
example2
Let’s examine code behind this:

Each link has refresh-news class so that single event can be attached. Category id is stored using data-catid attribute. jQuery has its own mechanism for parsing data attribute so it is enough to call $(this).data(‘catid’) inside event handler.