🐘Useful PHP functions
One line year array
// will get the past 3 year including current year. eg: 2021,2022,2023
for ($i=-2; $i <= 0 ; $i++) $data['year'][date('Y')+$i] = date('Y')+$i;
Unset attribute in array of array object
$body_list[$count] = array(
'attr' => array(
'1' => 'one',
'2' => 'two',
),
'data' => (object) array(
'3' => 'three',
'4' => 'four',
)
);
unset($body_list[$count]['data’]->{‘3’}); // will remove attribute ‘three’
Merge/combine array with duplicate keys while preserving the values
// use this inside loops where + wont work between each iteration
foreach ($array2 as $value) $array1[] = $value;
return $array1;
Divide string in two parts based on certain lengths
private function _divide_string($text, $length) {
$words = explode(" ", $text);
$top_text = $bottom_text = "";
$total_length = 0;
foreach ($words as $word) {
$word_length = strlen($word) + 1;
if ($total_length + $word_length <= $length) {
$top_text .= $word . " ";
$total_length += $word_length;
} else {
$bottom_text .= $word . " ";
}
}
return [trim($top_text), trim($bottom_text)];
}
List out all files in a folder into a html table for easy copying into excel/spreadsheets
public function spout_nonsense()
{
$string = shell_exec("ls application/controllers | paste -sd ',' -");
$controller_list = explode(',', $string);
echo '<html><head></head><body><table><tbody>';
foreach ($controller_list as $file_name) {
echo '<tr><td>'.str_replace('.php', '', $file_name).'</td></tr>';
}
echo '</tbody></table></body></html>';
}
Export current db into another db as backup
/**
* Export current db into another db as backup
* @param String $api_key
*/
public function backup_yesterday($api_key = NULL)
{
if ($api_key != SMAP_API_KEY) exit('No access');
$start_time = time();
$user = $_SERVER['DB_USER'];
$pass = $_SERVER['DB_PASSWORD'];
$db = $_SERVER['DB_NAME'];
$backup_db = 'smap_backup';
$file_path = PATH_IMAGE . 'temp_backup/yesterday.sql.gz';
$mysql_path = '/Applications/MAMP/Library/bin'; // for MAMP only, change this to suit your local machine
if (!file_exists(dirname($file_path))) { // create folder if not exist
mkdir(dirname($file_path), 0755, TRUE);
}
file_put_contents($file_path, ''); // create temporary file
// export db into temporary file
$export_command = "$mysql_path/mysqldump --single-transaction --quick -u $user -p'$pass' $db | gzip > $file_path";
exec($export_command, $output, $result);
$messages = '';
if ($result == 0) {
$messages .= "DB '$db' exported to $file_path \n\n";
// import db
$import_command = "gunzip < $file_path | $mysql_path/mysql -u $user -p'$pass' $backup_db";
exec($import_command, $output, $result);
if ($result == 0) {
$messages .= "DB '$backup_db' imported from $file_path \n\n";
} else {
$messages .= "Error importing DB \n\n";
}
} else {
$messages .= "Error exporting DB \n\n";
}
// delete temporary file
if (unlink($file_path)) {
$messages .= "Deleted $file_path \n\n";
} else {
$messages .= "Error deleting file \n\n";
}
$messages .= 'Runtime: ' . (time() - $start_time) . ' seconds';
echo nl2br($messages);
}
📝 Useful mysql scripts
Error something something group by
SET GLOBAL sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));
List all tables inside a database into a string
USE db_name;
SELECT GROUP_CONCAT(table_name SEPARATOR ', ') AS tables FROM information_schema.tables WHERE table_schema = ‘db_name’;
Sanitise data in table
UPDATE table_name
SET email = NULL, phone = NULL
WHERE email NOT LIKE '%@getnada.com%';
Useful js funcs
Format currency strings like ’20,000.00’ into floats ’20000.00’
function stringToFloat(str) {
return parseFloat(str.replace(/,/g, ''));
}
Comments
Post a Comment