Dopple
06-18-2008, 05:13 AM
If I create a new instance of the Class below from within another Class by using $category = new Category;
From the class which is instantiating the Category Class, how could I pass a name to $category (The instance of the class) and get back the id number ($catId)?
<?php
class Category{
public $catName;
public $catId;
public $catDate;
private $data = array();
public function __construct(){
// ADODB connection
require_once( 'includes/adodb5/adodb.inc.php' );
$conn = NewADOConnection('mysql');
$conn->Connect("mysql1.100ws.com", "gramac4_wiki", "xxxxxx", "gramac4_wiki");
}
// auto-include any Class files
function __autoload($className){
require_once $className . '.php';
}
public function __set($name, $value){
$this->data[$name] = $value;
}
public function __get($name){
return $this->data[$name];
}
// displays html form for input of a new category record
function displayForm(){
echo '
<form action="category.php" method="post">
<label for="category">Category</label>
<input type="text" name="categoryname" /><br />
<input name="submit" value="Submit" type="submit" /><input name="reset" value="Reset" type="reset" />
</form>';
}
// given an ID number, sets all objects properties to the corresponding database record's field properies
function load($catId){
$ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;
$sql = "SELECT * FROM hd_category WHERE c_id = '$catId'";
$result = &$conn->Execute('select CustomerID,OrderDate from Orders');
if (!$result)
print $conn->ErrorMsg();
else
while (!$result->EOF) {
$this->catId = $result->fields['c_id'];
$this->catName = $result->fields['c_name'];
$this->catDate = $result->fields['c_date'];
$result->MoveNext();
}
}
// saves all objects properties to hd_category table
function save(){
$sql = "INSERT INTO hd_category (c_name,c_time) VALUES ('$this->catName', " . time() .")";
if ($conn->Execute($sql) === false) {
print '<p>error inserting: ' . $conn->ErrorMsg();
echo '<br />' . $sql . '</p>';
}
}
// given an ID number, sets that databse records properties to the objects properties
function update($catId){
$sql = "UPDATE hd_category SET c_name = '$this->catName' WHERE c_id = '$this->catId'";
if ($conn->Execute($sql) === false) {
print '<p>error updating: ' . $conn->ErrorMsg();
echo '<br />' . $sql . '</p>';
}
}
}
?>
From the class which is instantiating the Category Class, how could I pass a name to $category (The instance of the class) and get back the id number ($catId)?
<?php
class Category{
public $catName;
public $catId;
public $catDate;
private $data = array();
public function __construct(){
// ADODB connection
require_once( 'includes/adodb5/adodb.inc.php' );
$conn = NewADOConnection('mysql');
$conn->Connect("mysql1.100ws.com", "gramac4_wiki", "xxxxxx", "gramac4_wiki");
}
// auto-include any Class files
function __autoload($className){
require_once $className . '.php';
}
public function __set($name, $value){
$this->data[$name] = $value;
}
public function __get($name){
return $this->data[$name];
}
// displays html form for input of a new category record
function displayForm(){
echo '
<form action="category.php" method="post">
<label for="category">Category</label>
<input type="text" name="categoryname" /><br />
<input name="submit" value="Submit" type="submit" /><input name="reset" value="Reset" type="reset" />
</form>';
}
// given an ID number, sets all objects properties to the corresponding database record's field properies
function load($catId){
$ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;
$sql = "SELECT * FROM hd_category WHERE c_id = '$catId'";
$result = &$conn->Execute('select CustomerID,OrderDate from Orders');
if (!$result)
print $conn->ErrorMsg();
else
while (!$result->EOF) {
$this->catId = $result->fields['c_id'];
$this->catName = $result->fields['c_name'];
$this->catDate = $result->fields['c_date'];
$result->MoveNext();
}
}
// saves all objects properties to hd_category table
function save(){
$sql = "INSERT INTO hd_category (c_name,c_time) VALUES ('$this->catName', " . time() .")";
if ($conn->Execute($sql) === false) {
print '<p>error inserting: ' . $conn->ErrorMsg();
echo '<br />' . $sql . '</p>';
}
}
// given an ID number, sets that databse records properties to the objects properties
function update($catId){
$sql = "UPDATE hd_category SET c_name = '$this->catName' WHERE c_id = '$this->catId'";
if ($conn->Execute($sql) === false) {
print '<p>error updating: ' . $conn->ErrorMsg();
echo '<br />' . $sql . '</p>';
}
}
}
?>