if (!function_exists('apache_ request _ headers ')) { function apache_request_headers() { foreach($_SERVER as $key=>$value) { if (substr($key,0,5)=="HTTP_") { $key=str_replace(" ","-",ucwords(strtolower(str_replace("_"," ",substr($key,5))))); $out[$key]=$value; }else{ $out[$key]=$value; } } return $out; } }
time to string
function magtime($inputs,$to = '2str'){ if($to=='2str'&&!(is_numeric($inputs))&&!($to=='2num'&&preg_match("/^[a-z]{2,10}$/",$inputs))) return false; $skeys = $akeys = array('f','a','d','e','c','y','z','t','k','g'); sort($skeys); $inputs = str_split($inputs); if($to=='2str'){ $ft_key = array_rand($akeys); $keys = $ft_key%2==0 ? $skeys : $akeys; $rs = ''; foreach($inputs as $input){ $rs .= $keys[$input]; } $rs .= $akeys[$ft_key]; }elseif($to=='2num'){ $footer = array_pop($inputs); $ft_key = array_search($footer,$akeys); if($ft_key===false) return false; $keys = $ft_key%2==0 ? $skeys : $akeys; $keys = array_flip($keys); $rs = ''; foreach($inputs as $input){ $rs .= $keys[$input]; } } return $rs;}$time = '1416896158';echo $time;echo "";$str = magtime($time);echo $str;echo "";$num = magtime($str,'2num');echo $num;echo "";
/** * 多条更新 * @param string $table_name * @param array $val_cases * @param array $fields * @param string $pri_key * @return UPDATE */function update_more($table_name,array $val_cases,array $fields,$pri_key = 'id'){ $arr = update_lots_combine($val_cases,$fields,$pri_key); return update_lots($table_name,$arr['values'],$arr['cases'],$pri_key);}/** * 多条更新 * @param string $table_name * @param array $values array('name'=>array('kate','jim'++),'age'=>array('16','18'++)) * @param array $cases array(array('id'=>'1','rank'=>'2'),array('id'=>'4','rank'=>'3')++); * @param $pri_key * @return UPDATE user SET `name`= CASE WHEN id='1' AND rank='2' THEN 'kate' WHEN id='4' AND rank='3' *THEN 'jim' ELSE `name` END, `age`= CASE WHEN id='1' AND rank='2' THEN '16' WHEN id='4' AND rank='3' *THEN '18' ELSE `age` END WHERE `id` IN (1,4,5) */function update_lots($table_name,$values,$cases,$pri_key = 'id'){ if(!is_array($values) || !is_array($cases) || !count($values)) return false; foreach($cases as $case_key => $case_fields){ foreach($case_fields as $case_field => $case_value){ !isset($pri_key) && $pri_key = $case_field;//for where ids !isset($ids[$case_key]) && $ids[$case_key] = $case_value;//for where ids $case_sql_piece[]=$case_field."='".$case_value."'"; } $case_sql[] = implode(' AND ',$case_sql_piece); unset($case_sql_piece); } !empty($ids)&&$where = " WHERE `$pri_key` IN (".implode(',',$ids).") "; $sql = "UPDATE ".$table_name.' SET '; $csql = ''; foreach($values as $field=>$fieldvalues){ $csql .= "`".$field."`"."= CASE "; foreach($values[$field] as $key=>$fieldvalue){ !isset($case_sql[$key]) && $case_sql[$key] = $case_sql[0]; $csql .= " WHEN { $case_sql[$key]} THEN '{ $fieldvalue}'"; } $csql .= " ELSE `{ $field}` END,"; } $sql .= rtrim($csql,','); isset($where) && $sql .= $where; return $sql;}/** * 多条更新参数设置 * @param array $val_cases array(array('value'=>array('kate',16),'where'=>array($pri_key=>'1','rank'=>'2')), * array('value'=>array('jim',18),'where'=>array($pri_key=>'4','rank'=>'3'))) * @param array $fields array('name','age') * @param string $pri_key id * @return boolean|unknown[] */function update_lots_combine(array $val_cases,array $fields,$pri_key = 'id'){ if(!is_array($val_cases) || !is_array($fields)) return false; foreach($val_cases as $row){ foreach($fields as $k=>$field){ if(!isset($row['value'][$k])){ exit('批量更新参数不匹配!');} $values[$field][] = $row['value'][$k]; } $cases[] = $row['where']; } return array('values'=>$values,'cases'=>$cases);}