I am just wondering if my usual way of creating classes can be simplified as I always seem to be repeating a lot of code and db field names in both the class and the page where it is used??
An snippet from one of my current classes:
class Dogs extends DatabaseConnect {
public $dog_id;
public $account_id;
public $main_handler_id;
public $owner_id;
public $kc_name;
public $pet_name;
public $kc_number;
public $grade_id;
public $height_id;
public $breed_id;
public $sex;
public $dob_day;
public $dob_month;
public $dob_year;
public $aw_type;
public $grade_updated;
//champ wins publiciable
public $champ_win_id;
public $winshow;
public $windate;
public $winclass;
//grade change publiciables
public $grade_changes_id;
public $previous_grade_id;
public $new_grade_id;
public $win_date;
private $order_by='';
public function Dogs ()
{
$this->order_by='kc_name, pet_name asc';
$connection=$this->DatabaseConnect();
}
// Check if dog is already registered on account
public function checkExists()
{
if (!empty($this->pet_name) || !empty($tnhis->kc_name)) {
$strSQL = mysql_query("select dog_id from scl_dogs where (kc_name='".$this->kc_name."' OR pet_name='".$this->pet_name."') and account_id='".$_SESSION['account_id']."'");
list($this->dog_id) = mysql_fetch_row($strSQL);
}
}
// Get all dogs registered to the account
public function getDogs()
{
$strSQL="select * from scl_dogs where account_id='".$_SESSION['account_id']."' ORDER BY '".$this->order_by."'";
$this->executeQuery($strSQL);
}
// Get all details for dog
public function getDetails()
{
$strSQL = mysql_query("select * from scl_dogs where account_id='".$_SESSION['account_id']."'");
list($this->dog_id,
$this->account_id,
$this->main_handler_id,
$this->owner_id,
$this->kc_name,
$this->pet_name,
$this->kc_number,
$this->grade_id,
$this->height_id,
$this->breed_id,
$this->sex,
$this->dob_day,
$this->dob_month,
$this->dob_year,
$this->aw_type,
$this->updated) = mysql_fetch_array($strSQL);
}
public function addDog ()
{
mysql_query("insert into scl_dogs (
account_id,
main_handler_id,
owner_id,
kc_name,
pet_name,
kc_number,
grade_id,
height_id,
breed_id,
sex,
dob_day,
dob_month,
dob_year,
aw_type)
VALUES (
'".$_SESSION['account_id']."',
'".$this->main_handler_id."',
'".$this->owner_id."',
'".$this->kc_name."',
'".$this->pet_name."',
'".$this->kc_number."',
'".$this->grade_id."',
'".$this->height_id."',
'".$this->breed_id."',
'".$this->sex."',
'".$this->dob_day."',
'".$this->dob_month."',
'".$this->dob_year."',
'".$this->aw_type."')") or die(mysql_error()) ;
$this->dog_id=mysql_insert_id();
}
public function editDog ()
{
mysql_query("update scl_dogs set
main_handler_id='".$this->main_handler_id."',
owner_id='".$this->owner_id."',
kc_name='".$this->kc_name."',
pet_name='".$this->pet_name."',
kc_number='".$this->kc_number."',
grade_id='".$this->grade_id."',
height_id='".$this->height_id."',
breed_id='".$this->breed_id."',
sex='".$this->sex."',
dob_day='".$this->dob_day."',
dob_month='".$this->dob_month."',
dob_year='".$this->dob_year."',
aw_type='".$this->aw_type."',
updated=NOW()
WHERE dog_id='".$_SESSION['dog_id']."'") or die(mysql_error()) ;
}
}
If I am using the class to load a dog’s details for viewing/editing, I call the class and then have to repeat all the variable names again, them same when sending the information to be updated.
I am just thinking that there must be a better way to do this without having to write the same code/field names over and over and also if there is I could re-use in other classes.