I have a catalog class.
class Catalog {
private $db;
private $categories = array(); // array of categories
function Catalog($db) {
$this->db = $db;
$this->getCategories();
}
private function getCategories() {
$result = $this->db->query('SELECT c.id, c.name FROM categories c');
while ( $row = $result->fetch_row() ) {
$this->categories[] = new Category($this->db, $row[0], $row[1]);
}
}
public function categories() {
return $this->categories();
}
public function test() {
echo $this->categories[0]->id();
}
}
And a category class.
class Category {
private $db;
private $id;
private $name;
private $items = array(); // array of items
function Category($db, $id, $n) {
$this->db = $db;
$this->id = $id;
$this->name = $n;
$this->getItems();
}
private function getItems() {
$query = 'SELECT i.id, i.name, i.datetime FROM items i, catalog
WHERE i.id = catalog.item_id AND catalog.category_id = '.$this->id;
$result = $this->db->query($query);
while ( $row = $result->fetch_row() ) {
$this->items[] = new Item($this->db, $row[0], $row[1], $row[2]);
}
}
public function id() {
return $this->id;
}
public function name() {
return $this->name;
}
public function items() {
return $this->items;
}
}
And an item class.
class Item {
private $db;
private $id;
private $name;
private $datetime;
function Item($db, $id, $n, $dt) {
$this->db = $db;
$this->id = $id;
$this->name = $n;
$this->datetime = $dt;
}
private function get($column) {
return $column;
}
public function id() {
return $this->id;
}
public function name() {
return $this->name;
}
public function datetime() {
return $this->datetime;
}
}
And I’ve got an index.php with the following:
$db = new Database(HOST, USERNAME, PASSWORD, DATABASE);
$catalog = new Catalog($db);
foreach ( $catalog->categories() as $category ) {
foreach ( $category->items() as $item ) {
echo $item->datetime();
}
}
And when I tried running it on IIS7, Vista, I got a stack overflow error so I switched to Apache 2.2.6 with PHP 5.2.4. …and that didn’t help anything. By the way, everything that needed to be included is included and calling $catalog->test(); works fine.
Any ideas?