Last active
November 16, 2021 19:45
-
-
Save solofeed/3742dbba40b521a3d554b5a64fa6d32d to your computer and use it in GitHub Desktop.
PHP assessment (refactoring and optimisation)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function store($variable){ | |
$insert = "INSERT INTO `".$variable['table']."`("; | |
foreach ($variable as $key => $value) { | |
if(trim($key) != 'table'){ | |
$insert = $insert.''.$key.','; | |
} | |
} | |
$insert = substr($insert,0,-1); | |
$insert = $insert.") VALUES("; | |
foreach ($variable as $key => $value) { | |
if(trim($key) != 'table'){ | |
if(trim(gettype($value)) == 'string'){ | |
$insert = $insert.'\''.$value.'\','; | |
}else{ | |
$insert = $insert.''.$value.','; | |
} | |
} | |
} | |
$insert = substr($insert,0,-1); | |
$insert = $insert.')'; | |
foreach ($variable as $key => $value) { | |
if(trim($key) != 'table'){ | |
if(trim(gettype($value)) == 'string'&&strlen($value) < 512){ | |
$variable[$key] = $key.' TEXT(512), | |
'; | |
}elseif(trim(gettype($value)) == 'string'&&strlen($value) > 512&&strlen($value) <= 1024){ | |
$variable[$key] = $key.' TEXT(1024), | |
'; | |
}elseif(trim(gettype($value)) == 'string'&&strlen($value) > 1024&&strlen($value) <= 2048){ | |
$variable[$key] = $key.' TEXT(2048), | |
'; | |
}elseif(trim(gettype($value)) == 'string'&&strlen($value) > 2048&&strlen($value) <= 4096){ | |
$variable[$key] = $key.' TEXT(4096), | |
'; | |
}elseif(trim(gettype($value)) == 'string'&&strlen($value) > 4096){ | |
$variable[$key] = $key.' TEXT(65535), | |
'; | |
} | |
if(gettype($value) == 'integer'){ | |
$variable[$key] = $key.' INT(128), | |
'; | |
} | |
if(gettype($value) == 'double'||gettype($value) == 'float'){ | |
$variable[$key] = $key.' FLOAT(53), | |
'; | |
} | |
} | |
}; | |
$table = " | |
CREATE TABLE ".$variable['table']."( | |
id INT(20) UNSIGNED AUTO_INCREMENT PRIMARY KEY, | |
"; | |
foreach ($variable as $key => $value) { | |
if(trim($key) != 'table'){ | |
$table = $table.''.$value; | |
} | |
}; | |
$table = substr($table,0,-3); | |
$table = $table.' | |
)'; | |
return array( | |
'insert' => $insert, | |
'table' => $table | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment