I'm new to PHP OOP. The project need a site class and the below is my code ():
class Sites {
private $siteName;
private $location;
private $postcode;
function __construct($name, $loc, $pc) {
$this->siteName = $name;
$this->location= $loc;
$this->postcode = $pc;
//use "insert SQL" to store new added site info to DB
$insertSQL = "INSERT INTO table_name (siteName, location, postcode) VALUES ($siteName, $location, $postcode)";
}
function getSiteName($SiteID){
selectSiteName = "SELECT siteName FROM site WHERE siteID = $SiteID";
return $this->siteName;
}
function getSiteLocation($SiteID){
selectLocation = "SELECT location FROM site WHERE siteID = $SiteID";
return $this->location;
}
function getPostCode($SiteID){
selectPostcode = "SELECT postcode FROM site WHERE siteID = $SiteID";
return $this->postcode;
}
function getSiteID(){
//what shoud write here?
return $this->siteID;
}
}
Fields in the Site Table include 'siteName', 'location', 'postcode' and 'siteID'. Here 'siteID' is Primary key and AUTO INCREMENT value.
I have few questions:
1.is the above code correct?
2.How to get 'SiteID'? For example, sitename is 'ABC', Should get ID by using "SELECT * FROM site WHERE siteName = 'ABC'". But site name is not unique value.
3.For methods like 'DeleteSiteByID', 'EditSiteByID', 'hasSubSites',shold those methods be in the Site Class?
Thanks for all your help.