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.

Recommended Answers

All 4 Replies

1. The returning value is incorrect

function getSiteName($SiteID){
 
        selectSiteName = "SELECT siteName FROM site WHERE siteID = $SiteID";
 
        return $this->siteName;
    }

Why are you going to return a null value?

2. Yes I think it's better to use unique IDs to avoid conflicts

Hi MooGeek, thanks for your quick reply. You methoded "returning value is incorrect".

If I use getSiteID() to obtain siteID first then pass the siteID to getSiteName() to retrive siteName. Is the return value correct. Thanks again!

1. The returning value is incorrect

function getSiteName($SiteID){
 
        selectSiteName = "SELECT siteName FROM site WHERE siteID = $SiteID";
 
        return $this->siteName;
    }

Why are you going to return a null value?

2. Yes I think it's better to use unique IDs to avoid conflicts

I did not include a constructor like you have, but recommend that you change yours. Your very base construct should not run a query and try to do an insert, the purpose of a class (in my opinion) is to one be a 'container' to hold the values of your object, and two easily call functions to do 'certain' work that is required like calculating things or concatenating strings or insert, update delete etc... split each of those things out. also, sorry about the 'documentation' it is automatically generated by the generator I use.
The insert code in this class is expecting siteID to be an auto-increment on your database, if it is not then you would have to adjust the insert code. also when you call get_siteID() or something, you must remove your queries from there, it 'can' be done like that but that is very poor implementation of code. that said, take a look at this class
Sites.class.php

<?php
// begin generated class -->
/*
* -------------------------------------------------------
* CLASSNAME:        Sites
* GENERATION DATE:  15.09.2011
* CLASS FILE:       C:\wamp\www\PHP_LIB\sql_class_generator\generated_classes\Sites.class.php
* FOR MYSQL TABLE:  sites
* FOR MYSQL DB:     test
* -------------------------------------------------------
* CODE GENERATED BY:
* MY PHP-MYSQL-CLASS GENERATOR
* from: >> www.voegeli.li >> 
* CLASS MODIFIED AND EXTENDED BY:
* dmd
* -------------------------------------------------------
*/
// define your actual db connection values here:
define('DB_HOST','localhost');
define('DB_USER','root');
define('DB_PASS','');
define('DB_BASE','test');	
   
// **********************
// CLASS DECLARATION
// **********************
// class : begin
class Sites { 
// **********************
// ATTRIBUTE DECLARATION
// **********************
	var $siteID;   // KEY ATTR. WITH AUTOINCREMENT
	var $siteName;   // (normal Attribute)
	var $location;   // (normal Attribute)
	var $postcode;   // (normal Attribute)

// **********************
// CONSTRUCTOR METHOD
// **********************
	/**
	* @author - emily
	* @type	- public
	* @desc	- constructor
	* @param - Sites
	* @return - Instance of $class
	* @vers	- 1
	* @Mod - 
	**/
	function Sites() {
		// this can be used as your constructor or to initialize an empty object.
	}
// **********************
// GETTER METHODS
// **********************
	/**
	* @author - emily
	* @type	- public
	* @desc	- get variable value
	* @param - get_siteID()
	* @return - variable
	* @vers	- 1
	* @Mod - 
	**/
	function get_siteID() {
		return $this->siteID;
	}
	
	/**
	* @author - emily
	* @type	- public
	* @desc	- get variable value
	* @param - get_siteName()
	* @return - variable
	* @vers	- 1
	* @Mod - 
	**/
	function get_siteName() {
		return $this->siteName;
	}
	
	/**
	* @author - emily
	* @type	- public
	* @desc	- get variable value
	* @param - get_location()
	* @return - variable
	* @vers	- 1
	* @Mod - 
	**/
	function get_location() {
		return $this->location;
	}
	
	/**
	* @author - emily
	* @type	- public
	* @desc	- get variable value
	* @param - get_postcode()
	* @return - variable
	* @vers	- 1
	* @Mod - 
	**/
	function get_postcode() {
		return $this->postcode;
	}
	// **********************
	// SETTER METHODS
	// **********************
	
	/**
	* @author - emily
	* @type	- public
	* @desc	- set variable value
	* @param - set_siteID($val)
	* @return - void
	* @vers	- 1
	* @Mod - 
	**/
	function set_siteID($val) {
		$this->siteID = $val;
	}
	
	/**
	* @author - emily
	* @type	- public
	* @desc	- set variable value
	* @param - set_siteName($val)
	* @return - void
	* @vers	- 1
	* @Mod - 
	**/
	function set_siteName($val) {
		$this->siteName = $val;
	}
	
	/**
	* @author - emily
	* @type	- public
	* @desc	- set variable value
	* @param - set_location($val)
	* @return - void
	* @vers	- 1
	* @Mod - 
	**/
	function set_location($val) {
		$this->location = $val;
	}
	
	/**
	* @author - emily
	* @type	- public
	* @desc	- set variable value
	* @param - set_postcode($val)
	* @return - void
	* @vers	- 1
	* @Mod - 
	**/
	function set_postcode($val) {
		$this->postcode = $val;
	}
	
	// **********************
	// print_to_screen()
	// a simple function to see the values that are held in this class 
	// **********************
	function print_to_screen() { 
		echo "siteID = ". $this->siteID ." \n <br />";
		echo "siteName = ". $this->siteName ." \n <br />";
		echo "location = ". $this->location ." \n <br />";
		echo "postcode = ". $this->postcode ." \n <br />"; 
		echo "end p_t_s function \n "; 
	}
	
	// **********************
	// process a form $_POST()
	// **********************
	function process_form_post($_POST) { 
		$this;
		if (!empty($_POST['siteID'])) { 
			$this->set_siteID($_POST['siteID']);
		}
		if (!empty($_POST['siteName'])) { 
			$this->set_siteName($_POST['siteName']);
		}
		if (!empty($_POST['location'])) { 
			$this->set_location($_POST['location']);
		}
		if (!empty($_POST['postcode'])) { 
			$this->set_postcode($_POST['postcode']);
		} 
		//echo "end post function \n "; 
		// if you are processing a form, you can just call insert now...
		//$this->insert();
		return $this;
	}
	
	// **********************
	// INSERT
	// **********************	
	/**
	* @author - emily
	* @type	- public
	* @desc	- insert this object
	* @param - insert
	* @return - boolean
	* @vers	- 1
	* @Mod - 
	**/
	function insert() {
		$this->siteID = ""; // clear key for autoincrement
		$sql = "INSERT INTO sites (siteName,location,postcode ) VALUES ( '".$this->slashes($this->siteName)."','".$this->slashes($this->location)."','".$this->slashes($this->postcode)."' )";
		$dblink = null;
		try	{
			$dblink = mysql_connect(DB_HOST,DB_USER,DB_PASS);
			mysql_select_db(DB_BASE,$dblink);
		} catch(Exception $ex) {
			echo "Could not connect to " . DB_HOST . ":" . DB_BASE . "\n";
			echo "Error: " . $ex->message;
			exit;
		}
		
		$retid = mysql_query($sql,$dblink) or die(mysql_error());
		if (!$retid) { 
			echo( mysql_error()); 
		}		
		// sets your siteID off of last insert id
		$this->siteID = mysql_insert_id($dblink);
		
		if(is_resource($dblink)) {		
			mysql_close($dblink);
		}		
		// return this object
		return $this;		
	}

	// **********************
	// UPDATE
	// **********************
	
	/**
	* @author - emily
	* @type	- public
	* @desc	- update this object
	* @param - update($id)
	* @return - boolean
	* @vers	- 1
	* @Mod - 
	**/
	function update($id) {
		$sql = "UPDATE sites SET siteName = '$this->siteName',location = '$this->location',postcode = '$this->postcode' WHERE siteID = $id ";
		$dblink = null;
		try	{
			$dblink = mysql_connect(DB_HOST,DB_USER,DB_PASS);
			mysql_select_db(DB_BASE,$dblink);
		} catch(Exception $ex) {
			echo "Could not connect to " . DB_HOST . ":" . DB_BASE . "\n";
			echo "Error: " . $ex->message;
			exit;
		}
		
		$retid = mysql_query($sql,$dblink) or die(mysql_error());
		if (!$retid) { 
			echo( mysql_error()); 
		}		
		
		if(is_resource($dblink)) {		
			mysql_close($dblink);
		}				
		return $retid;
	}

	// **********************
	// SELECT METHOD / LOAD
	// **********************	
	/**
	* @author - emily
	* @type	- public
	* @desc	- load this object
	* @param - select_by_id($id)
	* @return - sets this object from "$id"
	* @vers	- 1
	* @Mod - 
	**/
	function select_by_id($id) {
		$dblink = null;
		try	{
			$dblink = mysql_connect(DB_HOST,DB_USER,DB_PASS);
			mysql_select_db(DB_BASE,$dblink);
		} catch(Exception $ex) {
			echo "Could not connect to " . DB_HOST . ":" . DB_BASE . "\n";
			echo "Error: " . $ex->message;
			exit;
		}
		$sql =  "SELECT * FROM sites WHERE siteID = ".$this->slashes($id);
		
		$retid = mysql_query($sql,$dblink) or die(mysql_error());
		if (!$retid) { 
			echo( mysql_error()); 
		}
		if ($row = mysql_fetch_array($retid)) {	

			$this->siteID = $row['siteID'];
			$this->siteName = $row['siteName'];
			$this->location = $row['location'];
			$this->postcode = $row['postcode'];
		}
		
		if(is_resource($dblink)) {		
			mysql_close($dblink);
		}
		return $this;
	}
	
	// **********************
	// SELECT METHOD / LOAD ALL RECORDS
	// **********************	
	/**
	* @author - emily
	* @type	- public
	* @desc	- load this database table
	* @param - select_all_sites()
	* @return - sets array of all Sites to be returned
	* @vers	- 1
	* @Mod - 
	**/
	function select_all_sites() {
		$return_array = array();
		$dblink = null;
		try	{
			$dblink = mysql_connect(DB_HOST,DB_USER,DB_PASS);
			mysql_select_db(DB_BASE,$dblink);
		} catch(Exception $ex) {
			echo "Could not connect to " . DB_HOST . ":" . DB_BASE . "\n";
			echo "Error: " . $ex->message;
			exit;
		}
		$sql = "SELECT * FROM sites";
		
		$retid = mysql_query($sql,$dblink) or die(mysql_error());
		if (!$retid) { 
			echo( mysql_error()); 
		}
		// notice the while here as we are expecting all records on the table.
		while ($row = mysql_fetch_array($retid)) {	
			// instantiate the object and set all of your fields
			$site = new Sites();			
			$site->siteID = $row['siteID'];
			$site->siteName = $row['siteName'];
			$site->location = $row['location'];
			$site->postcode = $row['postcode'];
			// push each site object to an array
			array_push($return_array,$site);
		}
		
		if(is_resource($dblink)) {		
			mysql_close($dblink);
		}
		// return your array of all sites
		return $return_array;
		/* on your web pate you can do something like this:
		include('Sites.class.php');
		$s = new Sites();
		$sites = array();		
		$sites = $s->select_all_sites();
		foreach($sites as $site) {
			$site->print_to_screen();
		}
		*/
		
	}	
	// **********************
	// DELETE
	// **********************
	
	/**
	* @author - emily
	* @type	- public
	* @desc	- delete this object
	* @param - delete($id)
	* @return - boolean
	* @vers	- 1
	* @Mod - 
	**/
	function delete($id) {
		$dblink = null;
		try	{
			$dblink = mysql_connect(DB_HOST,DB_USER,DB_PASS);
			mysql_select_db(DB_BASE,$dblink);
		} catch(Exception $ex) {
			echo "Could not connect to " . DB_HOST . ":" . DB_BASE . "\n";
			echo "Error: " . $ex->message;
			exit;
		}
		$sql = "DELETE FROM sites WHERE siteID = $id;";
		
		$retid = mysql_query($sql,$dblink) or die(mysql_error());
		if (!$retid) { 
			echo( mysql_error()); 
		}
		
		if(is_resource($dblink)) {		
			mysql_close($dblink);
		}
		return $retid;
	
	}

	/**
	* @author - emily
	* @type	- public
	* @desc	- $str
	* @param - adding mysqlrealescapestring if necessary
	* @return - db safe string
	* @vers	- 1
	* @Mod - 
	**/
	function slashes($str) {
		if (!empty($str)) {
			return mysqlrealescapestring($str);
		} else {
			return $str;
		}
	}	
	/**************************************************************/
	/*          ADD YOUR CUSTOM FUNCTIONS BELOW			       */
	/**************************************************************/
// class : end
}
?>

ohh, there is an error, line 419 it should be:

return mysql_real_escape_string($str);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.