« Coniston version of email_functions with BCC | Main | Python script for getting changed files »
April 15, 2008
Academi version of add_update_functions
function date_splitter($key){ global $r; $b = explode('/', $r[$key]); $ts = mktime(0,0,0, $b[1], $b[0], $b[2]); $r[$key] = $ts; } function get_options($reply, $vcol, $lcol){ $opts = array(); if(count($reply['rows']) > 0){ foreach($reply['rows'] as $row){ $opts[] = ' '.$row[$lcol]; }} try{ return implode("\n", $opts); } catch(Exception $e){ return $opts[0]; } } function update_table_extra($table, $r, $where, $pk){ global $user_id; if(!$where){ $where = '1'; } $sql = "desc `".$table."`"; $reply = just_sql($sql); $exclusions = array('insert_by','update_by','insert_date','update_date'); $pk_col = NULL; $fields = array(); $text_fields = array(); foreach($reply['rows'] as $row){ if($row['Key'] == "PRI"){ $pk_col = $row['Field']; } else{ if(!in_array($row['Field'], $exclusions)){ $fields[] = $row['Field']; } if(strstr($row['Type'], 'text')){ $text_fields[] = $row['Field']; } if(strstr($row['Type'], 'varchar')){ $text_fields[] = $row['Field']; } } } $sets = array(); if(in_array($key, $fields)){ if(in_array($key, $text_fields)){ if((strtolower($value) != 'nulled') && (strtolower($value) != 'null')){ $value = "'".quote_me($value)."'"; } } if(strtolower($value) == 'nulled'){ $sets[] = " ".$key." = NULL"; } elseif(has_value($value)){ $sets[] = " ".$key." = ".$value; } } $sql = "update `".$table."` set ".implode(',',$sets).", update_by = ".$user_id.", update_date = unix_timestamp() where ".$where." and ".$pk_col." = ".$pk; $reply = just_sql($sql); return $reply; } function update_table($table, $r, $pk){ global $user_id; $sql = "desc `".$table."`"; $reply = just_sql($sql); $exclusions = array('insert_by','update_by','insert_date','update_date'); $pk_col = NULL; $fields = array(); $text_fields = array(); foreach($reply['rows'] as $row){ if($row['Key'] == "PRI"){ $pk_col = $row['Field']; } else{ if(!in_array($row['Field'], $exclusions)){ $fields[] = $row['Field']; } if(strstr($row['Type'], 'text')){ $text_fields[] = $row['Field']; } if(strstr($row['Type'], 'varchar')){ $text_fields[] = $row['Field']; } } } $sets = array(); foreach($r as $key => $value){ if(in_array($key, $fields)){ if(in_array($key, $text_fields)){ if((strtolower($value) != 'nulled') && (strtolower($value) != 'null')){ $value = "'".quote_me($value)."'"; } } if(strtolower($value) == 'nulled'){ $sets[] = " ".$key." = NULL"; } elseif(has_value($value)){ $sets[] = " ".$key." = ".$value; } } } $sql = "update `".$table."` set ".implode(',',$sets).", update_by = ".$user_id.", update_date = unix_timestamp() where ".$pk_col." = ".$pk; $reply = just_sql($sql); return $reply; } function add_new_row($table){ global $r; global $user_id; $sql = "insert into `".$table."`(insert_by, insert_date) values(".$user_id.", unix_timestamp())"; $reply = just_sql($sql); return $reply['insert_id']; }
Posted by pj at April 15, 2008 04:24 PM