Click to See Complete Forum and Search --> : Another OOp question


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>';
}
}

}

?>

chazzy
06-18-2008, 06:25 AM
huh? i don't follow what you're trying to get. can't you just do $category->catId ?

Dopple
06-18-2008, 06:52 AM
But the category class would have to be passed something to check the database against.
lets say we have 2 categories in the category table
c_id:1
c_name:CONNECT FAIL

c_id:2
c_name:CONNECT FAIL

If, from the class I am instantiating the category class (in this case it's called Incident), I want to find out which c_id relates to the c_name that has been entered by a user, calling $category->catId won't work as the class won't know which ID to return. The class will have to be passed the c_name so that itcan check the database and return the c_id.
I'm just wondering what the most efficient way of doing this is. I could easily make a single method for this which is passed the name as an argument and then returns $this->catId as this is a small class but the larger it gets I can't cover every eventuality.

Maybe a method which takes the value and also the mysql column name to check against?

Feck it I'll give it a bash and see what happens but if anyone is able to decode my ramblings, any input would be appreciated (Just like yours was Chazzy! :) ). It's hard to put this one into words without writing a novel about it.

NogDog
06-18-2008, 02:55 PM
Not sure I follow you, but I think maybe what you are asking would be addressed by one of these simplistic examples:

<?php
class Inner
{
public function hello($name)
{
echo "Hello, $name!";
}
}

class Outer
{
public $inner;
public function __construct()
{
$this->inner = new Inner();
}
}

$outer = new Outer();
$outer->inner->hello('NogDog');


<?php
class Inner
{
protected $name;
public function __construct($name)
{
$this->name = $name;
}
public function hello()
{
echo "Hello, " . $this->name . "!";
}
}

class Outer
{
public $inner;
public function __construct(Inner $inner)
{
$this->inner = $inner;
}
}

$outer = new Outer(new Inner('NogDog'));
$outer->inner->hello();

Either one will end up outputting "Hello, NogDog!"

Dopple
06-19-2008, 03:49 AM
What I did was created a load($column,$value) method which I can pass the whatever I like, as in id or name or whatever, but also pass the column name from the database. This then does a select * from table where $column = $value. It's a bit messy but it works.
Thanks for everyones help on this.