in my classes i like to include methods to set the values from the form:
function set_from_form(){
GLOBAL $HTTP_POST_VARS;
$this->set_name($HTTP_POST_VARS['cat_name']);
$this->set_parent_id($HTTP_POST_VARS['cat_id']);
$this->set_is_default($HTTP_POST_VARS['is_default']);
}
i set the ID manually. so the function works for updating and inserting.
and when i load something from the db, i use a set from row:
function set_from_row($row){
if(gettype($row) != "array"){
return false;
}
if (count($row) == 0){
return false;
}
foreach ($row as $key => $value) {
switch($key){
case 'id':
$this->set_id($row[$key]);
break;
case 'date_added':
$this->set_date_added(stripslashes($row[$key]));
break;
case 'year':
$this->set_year(stripslashes($row[$key]));
break;
case 'month':
$this->set_month(stripslashes($row[$key]));
break;
case 'day':
$this->set_day(stripslashes($row[$key]));
break;
case 'title':
$this->set_title(stripslashes($row[$key]));
break;
case 'overview':
$this->set_overview(stripslashes($row[$key]));
break;
}
}
return true;
}
and a delete function...
function delete(){
if(!is_numeric($this->id)){
return false;
}
$sql = "delete from res where id=" . $this->id;
mysql_query($sql, $this->conn);
return true;
}
i also use individual get/set methods to make sure ints remain ints. just a suggestion 🙂
all my classes have this kind of basic structure... makes things easy for me, maybe will help you too
Frizzo