Show/hide data lines and columns in google chart (remove column onclick/toogle multiple checkboxes/labels)

How to show and hide lines data in standart google chart?
Example of chart build:

google.charts.load('current', {'packages': ['corechart']});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
    var data = google.visualization.arrayToDataTable([
        ['Year', 'Sales', 'Expenses'],
        ['2013', 1000, 400],
        ['2014', 1170, 460],
        ['2015', 660, 1120],
        ['2016', 1030, 540]
    ]);

    var columns = [];
    var series = {};
    for (var i = 0; i < data.getNumberOfColumns(); i++) {
        columns.push(i);
        if (i > 0) {
            series[i - 1] = {};
        }
    }

    var options = {
        title: 'Chart title',
        vAxis: {minValue: 0},
        tooltip: {trigger: 'selection'},
        aggregationTarget: 'category',
        series: series
    };

    var chart = new google.visualization.AreaChart(document.getElementById('chart_div'));
    chart.draw(data, options);

    google.visualization.events.addListener(chart, 'select', function () {
        var sel = chart.getSelection();
        if (sel.length > 0) {
            if (sel[0].row === null) {
                var col = sel[0].column;
                if (columns[col] == col) {
                    columns[col] = {
                        label: data.getColumnLabel(col),
                        type: data.getColumnType(col),
                        calc: function () {
                            return null;
                        }
                    };
                    series[col - 1].color = '#CCCCCC';
                }
                else {
                    columns[col] = col;
                    series[col - 1].color = null;
                }
                var view = new google.visualization.DataView(data);
                view.setColumns(columns);
                chart.draw(view, options);
            }
        }
    });
}

PHP. Save information into text/log file. Bacis examples.

Simple example for save information to file on server by PHP

$content = '[' . date('Y-m-d H:i:s') . ']: test info log' . PHP_EOL . '------------' . PHP_EOL;
$fp = fopen($_SERVER['DOCUMENT_ROOT'] . "/save_test.log", "a+");
fwrite($fp, $content);
fclose($fp);

Error log using example(logs $content [array|string|object] to the log file):

$obj = new StdClass();
$content = [$obj, [3, 51, '4k']];
error_log('[' . date('Y-m-d H:i:s') . ']:' . PHP_EOL . print_r($content, TRUE) . PHP_EOL, 3, $_SERVER['DOCUMENT_ROOT'] . "/save_test.log");

Also you can use file_put_contents method:
Continue reading PHP. Save information into text/log file. Bacis examples.

Native PHP Sort array of objects by property

Usually, it is very often necessary to sort the data in an array. But when in the array we have objects it happened little bit difficult.
Fast solution for sorting array objects by property name.
Bacis solution (PHP 5.3+) :

function sortArrayWithObjects($array, $property)
{
    usort($array, function ($a, $b) use ($property) {
        return (($a->$property == $b->$property) ? 0 : (($a->$property < $b->$property) ? -1 : 1));
        //[short version] return strcmp($a->$property, $b->$property);
    });
    return $array;
}

$obj1 = new stdClass();
$obj2 = clone $obj1;
$obj3 = clone $obj2;
$obj1->name = 'B';
$obj2->name = 'C';
$obj3->name = 'A';
$data = [$obj1, $obj2, $obj3];
$result = sortArrayWithObjects($data, 'name');
echo '<pre>';
var_dump($result);
echo '</pre>';

Result:
Continue reading Native PHP Sort array of objects by property

PHPExcel not working with PHP 7.+ – ZipArchive not found

PHPExcel ZipArchive not found

If you catch this error (and you use PHP 7 version) you need:
1. update PHPExcel to 1.8.+
or
2. try to use this preparing method:

PHPExcel_Settings::setZipClass(PHPExcel_Settings::PCLZIP);

P.s. https://github.com/PHPOffice/PHPExcel/issues/716

Web-server on Ubuntu (PHP + MySQL + Apache) in 2 commands

Fast and easy install (PHP + MySQL + Apache) LAMP on Ubuntu

Step 1: install tasksel

sudo apt-get install tasksel

Step 2: Run install PHP, MySQL & Apache as LAMP server

sudo tasksel install lamp-server

Completed! Now you have installed PHP, MySQL, and Apache on your Ubuntu

Additional: Create site area
Continue reading Web-server on Ubuntu (PHP + MySQL + Apache) in 2 commands

If you got 500 (Internal Server Error) PHP/Nginx/Apache – Where you can found logs/reason?

Oh, noooo. You got most popular developer error 🙂
Firstly, No despair – say “Yes, I CAN”, and concentrate on possible errors.

1 step: Double check your PHP/Python/ETC code.
find_errors_ercule
Show all errors (how to show all errors/warnings/nootices in php)
Also, you can do “fast debug” in main parts of you code (Sure, use die() command but very carefully)

 

2 step: Check server/Apache&PHP settings
domino_server_settings_phpFirstly check .htaccess file on strange symbols or sentences.
Also, if you using POST method with uploading files then check php settings in php.ini (max_post_size, max_executing_time…)

 

3 step: Check error logs (Ubuntu/Linux)
find_errors_in_logs
Standard Apache logs dev finiding here:

/var/log/apache2/...

nginx logs here:

/var/log/nginx/...

Also, some popular frameworks logs folders:
Continue reading If you got 500 (Internal Server Error) PHP/Nginx/Apache – Where you can found logs/reason?

UPDATE with random number between range 1-n [MYSQL UPDATE RAND]

Sometimes you need to update your latest added field in DB table by random numbers.
For that reason you can use this simple SQL command:
[1 – min, 100-max]

UPDATE `...` 
SET `columnName` = FLOOR( 1 + RAND( ) *100 );
WHERE ...

In this way you can easy update `columnName` by random value in defined range.
Happy SQLing… Continue reading UPDATE with random number between range 1-n [MYSQL UPDATE RAND]

How to add one/two/n days or hours to date in MySQL (DATE_ADD in MYSQL)

Suppose you have a table in the database where the stored data and you need to check or get that dates but with the changes – calculate directly by MySQL.
Example, you want to get users end dates (7-day licence after registration):

SELECT `register_date`, `uid`, 
DATE_ADD(`register_date`, INTERVAL 7 DAY) AS `end_date` 
FROM `...`
WHERE...

Perfect, now you can show for user that his licence if over `end_date`…
In addition, you can subtract the required number of days
Continue reading How to add one/two/n days or hours to date in MySQL (DATE_ADD in MYSQL)

Yii 2 Response formats: RAW, HTML, JSON, JSONP, XML

If you want that your method return data in specific format in Yii2 then you can use this simple construction:
Example method return in JSON format:

use yii\web\Response;
//......
public function actionJson()
    {
        $something = true; // or you can set for test -> false;
        $return_json = ['status' => 'error'];
        if ($something == true)
        {
            $return_json = ['status' => 'success', 'message' => ' is successfully saved'];
        }
        \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
        return $return_json;
    }

/*header results
Connection: Keep-Alive
Content-Length: 55
Content-Type : application/json; charset=UTF-8
*/

Continue reading Yii 2 Response formats: RAW, HTML, JSON, JSONP, XML

CSS for placeholders / Universal solution

If you use placeholders titles for your input fields then you can pay attention to fact that in different browsers(Firefox,IE,Opera,Safari,Chrome) we can see various types of decoration.
Nowadays, universal solution is :

:-moz-placeholder { /* Firefox 18 */
  color: white;
}
::-moz-placeholder { /* Firefox 19+ */
  color: white;
}
:-ms-input-placeholder { /* IE 10+ */
  color: white;
}
::-webkit-input-placeholder { /* Opera + Safari + Chrome */
  color: white;
}

Thats all…