Created
June 26, 2014 16:45
-
-
Save shawn-crigger/abb1aec1b25400d22640 to your computer and use it in GitHub Desktop.
Method to populate empty database fields, just some random ghetto superstar code.
This file contains 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 | |
/** | |
* Populates empty database fields, ghetto fix for not having default values. | |
* | |
* @param string $table Table name to grab fields from | |
* @param array $data Array of data to merge the default values into. | |
* @param object $db Reference to the database connection, yea I'm a ghetto superstar. | |
* | |
* @return array | |
*/ | |
function populate_empty_fields($table = NULL, $data = array(), $db) | |
{ | |
if ( FALSE === is_string($table) ) return $data; | |
//this is pretty much what $db->getFields does.... | |
//$result = mysql_query("SHOW COLUMNS FROM $tbl"); | |
$db->getFields($table, TRUE); | |
$fields = $db->fields; | |
foreach ($fields as $field) { | |
if ( array_key_exists($data, $field['Field']) ) CONTINUE; | |
if ( is_null($field['Default']) ) | |
{ | |
// kinda ghetto but check if the field type is a INT and either make a default zero or empty string. | |
$data["{$field['Field']}"] = ( FALSE !== strpos($field['Type'],'int') ) ? 0 : NULL; | |
} | |
} | |
return $data; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment