Robert1995 Posted December 3, 2011 Report Posted December 3, 2011 Cadou de la mine. db_query presupun ca va prindeti ce face/** * Wrap my array * @param array the array you want to wrap * @param string wrapper , default double-quotes(") * @return an array with wrapped strings */function wrap_my_array($array , $wrapper = '"') { $new_array = array(); foreach($array as $k=>$element){ if(!is_array($element)){ $new_array[$k] = $wrapper . $element . $wrapper; } } return $new_array;}/** * Implode an array with the key and value pair giving * a glue, a separator between pairs and the array * to implode. * @param string $glue The glue between key and value * @param string $separator Separator between pairs * @param array $array The array to implode * @return string The imploded array */function array_implode( $glue, $separator, $array ) { if ( ! is_array( $array ) ) return $array; $string = array(); foreach ( $array as $key => $val ) { if ( is_array( $val ) ) $val = implode( ',', $val ); $string[] = "{$key}{$glue}{$val}"; } return implode( $separator, $string );}/** * @param string db_name * @param array data * @uses wrap_my_array * @uses array_implode */function db_insert($db_name , $data){ if(is_array($data) && !empty($data)){ $data = array_map('mysql_escape_string', $data); $keys = array_keys($data); $sql = 'INSERT INTO {'.$db_name.'} (' .implode("," , wrap_my_array($keys , '`')) .') VALUES (' .implode("," , wrap_my_array($data)) .')'; db_query($sql); return true; } return false;}/** * @param string db_name * @param array data * @param array/string where * @uses wrap_my_array * @uses array_implode */function db_update($db_name , $data = array() , $where = array()) { if(is_array($data) && !empty($data)){ $data = array_map('mysql_escape_string', $data); $data = wrap_my_array($data); $sql = 'UPDATE {'.$db_name.'} SET '; $sql .= array_implode("=" , "," , $data); if(!empty($where)){ $sql .= ' WHERE '; if(is_array($where)){ $where = wrap_my_array($where); $sql .= array_implode("=" , "AND" , $where); }else{ $sql .= $where; } } db_query($sql); return true; } return false;}/** * @param string db_name * @param array/string where * @uses wrap_my_array * @uses array_implode */function db_delete($db_name , $where = array()){ $sql = 'DELETE FROM {'.$db_name.'} '; if(!empty($where)){ $sql .= ' WHERE '; if(is_array($where)){ $where = wrap_my_array($where); $sql .= array_implode("=" , "AND" , $where); }else{ $sql .= $where; } } db_query($sql);} Quote
endemic Posted December 3, 2011 Report Posted December 3, 2011 ORM is the way, cand ai timp verifica http://redbeanphp.com/. Cred ca termenul e destul de sugestiv "object relational mapping". Quote