Skip to content

Instantly share code, notes, and snippets.

View mrwadson's full-sized avatar
:shipit:
So Working Hard..

Vadim Danilov mrwadson

:shipit:
So Working Hard..
View GitHub Profile
@mrwadson
mrwadson / csv_read.php
Last active April 8, 2024 11:31
Simple read CSV files (str_getcsv)
<?php
const CSV_FILENAME = __DIR__ . '/somefile.csv';
$csvFile = file(CSV_FILENAME);
$data = [];
foreach ($csvFile as $line) {
$data[] = str_getcsv($line);
}
@mrwadson
mrwadson / scan_dir.php
Last active February 14, 2024 12:55
Recursive scan directory examples
<?php
// Recursive scan directories and collect data (use `scandir` function)
function scan_dir($dir)
{
$result = [];
$filenames = array_diff(scandir($dir), ['.', '..']);
foreach ($filenames as $file) {
if (is_dir("$dir/$file")) {
array_push($result, ...scan_dir("$dir/$file"));
@mrwadson
mrwadson / Cache.php
Last active August 17, 2023 16:27
Cache in files
<?php
/*
* Usage [PHP >= 7.1]
*
require_once __DIR__ . '/Cache.php';
$cache = new Cache(__DIR__ . '/cache.json');
$cache->set('key1', ['key1' => 'value1']);
print_r($cache->get('key1'));
@mrwadson
mrwadson / Curl.php
Last active May 22, 2023 18:25
cURL class
<?php
/*
* Usage [PHP >= 7.1]
*
require_once __DIR__ . '/Curl.php';
$curl = new Curl('https://example.com');
$curl->get();
$curl->post(['key1' => 'value1']);
@mrwadson
mrwadson / mysqldump_zip.php
Last active May 22, 2023 18:27
Dump and Zip MySql DB
<?php
ini_set('max_execution_time', 600);
ini_set('memory_limit', '1024M');
// --- CONFIG ---------------------------------
const DB_NAME = 'your_database';
const DB_USER = 'root';
const DB_PASS = 'xxxxxx';
const DUMP_DIR = 'dumps';
@mrwadson
mrwadson / zip_dir.php
Last active October 26, 2022 21:05
Zip directory or file
<?php
ini_set('max_execution_time', 600);
ini_set('memory_limit', '1024M');
// -------------------------------
$source = __DIR__ . '/images';
$dest = __DIR__ . '/images.zip';
// -------------------------------
printf("Start zip '%s' %s to '%s'\n%s", $source, is_dir($source) ? 'directory' : 'file', $dest, zipDirOrFile($source, $dest));
@mrwadson
mrwadson / 1_product_queries.sql
Last active January 27, 2020 17:40 — forked from DanielSousa/1_product_queries.sql
Magento 2 - Remove duplicate store view-specific product and category data
#
# These queries check attributes values at the none-global store view scopes.
#
-- catalog_product_entity_datetime
SELECT a.*
FROM (SELECT * FROM catalog_product_entity_datetime) AS a
-- This inner join finds all store view-specific rows that exactly match the global scope value. The b.store_id = 0 conditional is key as it targets the global row
INNER JOIN (SELECT * FROM catalog_product_entity_datetime) AS b ON a.attribute_id = b.attribute_id AND a.entity_id = b.entity_id AND a.value = b.value AND b.store_id = 0
WHERE a.store_id <> 0
GROUP BY a.attribute_id, a.entity_id, a.value, a.store_id;
@mrwadson
mrwadson / Csv.php
Last active August 10, 2023 17:27
CSV read and write
<?php
/*
* Usage [PHP >= 7.1]
*
require_once __DIR__ . '/Csv.php';
$csv = new CsvFile(__DIR__ . '/read_csv_file.csv');
$csvRows = $csv->getRows();
...
$csv->saveRows($csvRows, __DIR__ . '/save_csv_file.csv');
*/
@mrwadson
mrwadson / MySql.php
Last active June 14, 2023 18:31
MySQL class
<?php
/*
* Usage
*
require_once __DIR__ . '/MySql.php';
$mysql = new MySql('database', 'db_username', 'db_password');
$rows = $mysql->select('table', 'col1 > 3 AND col2 = 10', 'col1, col2 DESC, col3, col4 ASC', [10 => 5]); // LIMIT 10 OFFSET 5
$rows = $mysql->select(['table' => true], 'col1 > 3 AND col2 = 10', 'col1, col2 DESC'); // DISTINCT