индеец
Профиль
Группа: Участник Клуба
Сообщений: 1180
Регистрация: 20.10.2004
Репутация: нет Всего: 53
|
для работы с базой: Код | <?php /* db.class.php - contains ICM_DBase class description */
$GLOBALS['DB']=xparse_ini_file("{$GLOBALS['PTH']['conf']}dbase.conf", false); //database configs
/* class ICM_DB - class to work with database implements work with mysql database */ class ICM_DB { var $_link; //link to mysql database var $_host; //database host var $_login; //database login var $_pass; //database password var $_name; //database name /* methods: bool ICM_DB() mixed exeQuery(string query) mixed getResult(string query) array getTables() array getFields(string table_name) int lastID() */
/* constructor, connects to database receives database attributes returnes true if connected */ function ICM_DB() { $this->_host=$GLOBALS['DB']['host']; $this->_login=$GLOBALS['DB']['login']; $this->_pass=$GLOBALS['DB']['pass']; $this->_name=$GLOBALS['DB']['name']; $this->_link=mysql_connect($this->_host,$this->_login,$this->_pass); return mysql_select_db($this->_name,$this->_link); }
/* executes mysql query receives given query returnes mysql result */ function exeQuery($query) { if($GLOBALS['DB']['show_error']) $result=mysql_unbuffered_query($query,$this->_link) or die("Error ".mysql_errno().': '.mysql_error()."<br>Query: ".nl2br($query)); else $result=mysql_query($query,$this->_link); return $result; }
/* executes mysql query and returnes result receives string returnes result */ function getResult($query) { $result=$this->exeQuery($query); $data=mysql_fetch_array($result); return $data[0]; }
/* gets list of tables in current database receives nothing returnes names of tables */ function getTables() { $tables_names=array(); $tables=mysql_list_tables($this->_name, $this->_link); $property_name='Tables_in_'.$this->_name; while($table=mysql_fetch_object($tables)) array_push($tables_names, $table->$property_name); return $tables_names; }
/* gets fields' names of a given table receives table name returnes fields names */ function getFields($table) { $fields=mysql_list_fields($this->_name, $table, $this->_link); $fields_num=mysql_num_fields($fields); for($i=0; $i<$fields_num; $i++) $fields_names[$i+1]=mysql_field_name($fields, $i); return $fields_names; }
/* gets id of last inserted row receives nothing return ID */ function lastID() { return mysql_insert_id($this->_link); } } ?>
|
для работы с таблицей: Код | <?php /* table.class.php - contains ICM_Table class description */
$GLOBALS['TBL']=xparse_ini_file("{$GLOBALS['PTH']['conf']}tables.conf", true); //tables' configs require_once "db.class.php"; //ICM_DB description
/* class ICM_Table - class to work with database tables */ class ICM_Table { var $db; //database object var $_name; //table name var $_trans; //postfix for tables with translatable data var $_item; //key-item field name var $_lang; //language field name /* methods: bool ICM_Table(array table_attrib) int insert(array data) bool update(int id, array data) bool delete(int id) array select(int id) array all([...]) array selectAll([...]) mixed getValue(int id, string field); mixed getTValue(int id, int lang, string field); */
/* constructor receives table attributes returnes true */ function ICM_Table($table_name, $trans_postfix, $keyitem, $langfield) { $this->db=new ICM_DB; $this->_name=$table_name; $this->_trans=$trans_postfix; $this->_item=$keyitem; $this->_lang=$langfield; return true; }
/* inserts data in table receives data to insert returnes inserted ID */ function insert($data) {
/* first parse data, get fields and values if $data[$field] is an array it means that it's translatable data where $data[$field][$lang] - is it's value and $lang - language ID */ foreach($data as $field => $value) if(!is_array($value)) { if(isset($fields)) $fields.=", `$field`"; else $fields="`$field`"; if(isset($values)) $values.=" ,'$value'"; else $values="'$value'"; } else foreach($value as $lang => $value_trans) { if(isset($fields_trans[$lang])) $fields_trans[$lang].=", `$field`"; else $fields_trans[$lang]="`$field`"; if(isset($values_trans[$lang])) $values_trans[$lang].=", '$value_trans'"; else $values_trans[$lang]="'$value_trans'"; }
/* insert data */ $query="insert into `{$this->_name}` ($fields) values ($values)"; $result=$this->db->exeQuery($query); $last_id=$this->db->lastID();
/* if there is translatable data then insert it */ if(isset($fields_trans)) foreach($fields_trans as $lang => $fields) { $values=$values_trans[$lang]; $query="insert into `{$this->_name}{$this->_trans}` (`{$this->_item}`, `{$this->_lang}` ,$fields) values ('$last_id', '$lang', $values)"; $result=$this->db->exeQuery($query); }
return $last_id; }
/* updates data in table receives ID of a row, data to update returnes true if succeed */ function update($id, $data) {
/* first check if there is row with such ID if not we insert data instead of updating it */ $query="select count(*) from `{$this->_name}` where `id`='$id'"; $num=$this->db->getResult($query); if($num) { /* like in insert(), we parse data first */ foreach($data as $field => $value) if(!is_array($value)) if(isset($sets)) $sets.=", `$field`='$value'"; else $sets="`$field`='$value'"; else foreach($value as $lang => $value_trans) { $query="select count(*) from `{$this->_name}{$this->_trans}` where `{$this->_item}`='$id' and `{$this->_lang}`='$lang'"; $num=$this->db->getResult($query); if($num) if(isset($sets_trans[$lang])) $sets_trans[$lang].=", `$field`='$value_trans'"; else $sets_trans[$lang]="`$field`='$value_trans'"; else $query="insert into `{$this->_name}{$this->_trans}` (`{$this->_item}`, `{$this->_lang}`, $field) values ('$id', '$lang', '$value_trans')"; $this->db->exeQuery($query); }
/* update data */ $query="update `{$this->_name}` set $sets where `id`='$id'"; $result=$this->db->exeQuery($query);
/* update translatable data */ if(isset($sets_trans)) foreach($sets_trans as $lang => $set) { $query="update `{$this->_name}{$this->_trans}` set $set where `{$this->_item}`='$id' and `{$this->_lang}`='$lang'"; $result=$this->db->exeQuery($query); } } else { $data['id']=$id; $result=$this->insert($data); } return (bool)$result; }
/* deletes data from table receives ID of a row to delete returnes true if succeed */ function delete($id) {
/* delete data */ $query="delete from `{$this->_name}` where `id`='$id'"; $result=$this->db->exeQuery($query);
/* delete translatable data if exists */ if(in_array($this->_name.$this->_trans, $this->db->getTables())) { $query="delete from `{$this->_name}{$this->_trans}` where `{$this->_item}`='$id'"; $result=$this->db->exeQuery($query); }
return (bool)$result; }
/* gets row with given ID receives row ID returnes associative array of data or empty array if row does not exists */ function select($id) { $data=array();
/* check whether row with given ID exists */ $query="select count(*) from `{$this->_name}` where `id`='$id'"; $num=$this->db->getResult($query); if($num) { /* get data */ $query="select * from `{$this->_name}` where `id`='$id'"; $result=$this->db->exeQuery($query); $data=mysql_fetch_assoc($result); mysql_free_result($result);
/* get translatable data if exists */ if(in_array($this->_name.$this->_trans, $this->db->getTables())) { /* list of system fields we do not need to select from table with translatable data */ $sys_fields=array('id', $this->_item, $this->_lang);
$query="select * from `{$this->_name}{$this->_trans}` where `{$this->_item}`='$id'"; $result=$this->db->exeQuery($query); while($tdata=mysql_fetch_assoc($result)) foreach($tdata as $field => $value) if(!in_array($field, $sys_fields)) $data[$field][$tdata[$this->_lang]]=$value; } }
return $data; }
/* gets all data from table receives: nothing or: where or: start, limit or: order, [dir] or: start, limit, order, [dir], [where] returnes array of IDs */ function all() {
/* get arguments and parse them */ $args=func_get_args(); $args_num=count($args);
if($args_num==1) { if(is_string($args[0])) $where=$args[0]; elseif(is_numeric($args[0])) $limit=$args[0]; } elseif($args_num==2) { if(is_numeric($args[0]) && is_numeric($args[1])) { $start=$args[0]; $limit=$args[1]; } elseif(is_string($args[0]) && is_string($args[1])) { $order=$args[0]; $dir=$args[1]; } } elseif($args_num>=3) { if(is_numeric($args[0])) $start=$args[0]; if(is_numeric($args[1])) $limit=$args[1]; if(is_string($args[2])) $order=$args[2]; if(isset($args[3])) if(is_string($args[3])) $dir=$args[3]; if(isset($args[4])) if(is_string($args[4])) $where=$args[4]; }
$row=array(); $order4query=''; $limit4query=''; $where4query='';
if(isset($order)) { if(in_array($this->_name.$this->_trans, $this->db->getTables())) $all_fields=array_merge($this->db->getFields($this->_name), $this->db->getFields($this->_name.$this->_trans)); else $all_fields=$this->db->getFields($this->_name); if(in_array($order, $all_fields)) { $order4query="order by `$order`"; if(isset($dir)) if(in_array($dir, array('desc', 'asc'))) $order4query.=" $dir"; } } if(isset($limit)) { if(isset($start)) $limit4query="limit $start, $limit"; else $limit4query="limit $limit"; } if(isset($where)) $where4query=" $where";
if(in_array($this->_name.$this->_trans, $this->db->getTables())) $query="select tbl.id from `{$this->_name}` as tbl, `{$this->_name}{$this->_trans}` as tbl_t where tbl.id=tbl_t.{$this->_item} $where4query $order4query $limit4query"; else $query="select id from `{$this->_name}` where 1 $where4query $order4query $limit4query";
$result=$this->db->exeQuery($query); while($data=mysql_fetch_assoc($result)) if(!in_array($data['id'], $row)) array_push($row, $data['id']);
return $row; }
/* selects all data from table gets all data from table receives: nothing or: where or: start, limit or: order, [dir] or: start, limit, order, [dir], [where] returnes associative array of data */ function selectAll() { $data=array(); $args=func_get_args(); $rows=call_user_func_array(array(&$this, 'all'), $args); foreach($rows as $id) array_push($data, $this->select($id)); return $data; }
/* gets field value of row with given ID, if such exists receives row ID and field name returnes field value or false if row doesn't exists */ function getValue($id, $field) { $value=false;
/* check whether row with given ID exists */ $query="select count(*) from `{$this->_name}` where id='$id'"; $num=$this->db->getResult($query); if($num) { /* check whether given field exists */ if(in_array($field, $this->db->getFields($this->_name))) { $query="select `$field` from `{$this->_name}` where `id`='$id'"; $value=$this->db->getResult($query); } }
return $value; }
/* gets value of translatable field receives row ID, language ID, field name returnes field value or false if does not exists */ function getTValue($id, $lang, $field) { $value=false;
/* check whether row with given ID and language ID exists */ $query="select count(*) from `{$this->_name}{$this->_trans}` where `{$this->_item}`='$id' and `{$this->_lang}`='$lang'"; $num=$this->db->getResult($query); if($num) { /* check whether given field exists */ if(in_array($field, $this->db->getFields($this->_name.$this->_trans))) { $query="select `$field` from `{$this->_name}{$this->_trans}` where `{$this->_item}`='$id' and `{$this->_lang}`='$lang'"; $value=$this->db->getResult($query); } }
return $value; }
} ?>
|
|