All pastes #352054 Raw Edit

Miscellany

public text v1 · immutable
·published 2007-02-12 15:41 UTC
rendered paste body
<?php

// since you dont want to do mass updates, theres no reason to extend the
// item class here. So were going to just make a simple list class.

class InventoryList {
	protected $list;
	protected $filter, $sort, $offset;

	public function getInstance () {
		static $object;
		if (!$object) {
			$object = new self ();
		}
		return $object;
	}

	public function getList () {
		return $this->list;
	}

	public function setList ($item_list) {
		$this->list=$item_list;
	}

	public function getFilter () {
		return $this->filter;
	}

	public function setFilter ($filter_column, $filter_value) {
		unset ($this->filter);
		$this->filter['column']=$filter_column;
		$this->filter['value']=$filter_value;
	}

	public function getSort () {
		return $this->sort;
	}

	public function setSort ($sort_column, $sort_descending = null) {
		unset ($this->sort);
		$this->sort['column']=$sort_column;
		$this->sort['descending']=$sort_descending;
	}

	public function load () {
		$querystr="
			SELECT *
			FROM inventory";

		// first add the filter if its set
		if ($this->filter) {
			$querystr.="
				WHERE ".$this->filter['column']."='".$this->filter['value']."'";
		}

		// next add the sort if its set.
		if ($this->sort) {
			$querystr.="
				ORDER BY ".$this->sort['column'];
			if ($this->sort['descending'])
				$querystr.="DESC";
		}

		// last add the offset if its set, or just a limit if its not set
		if ($this->offset) {
			$querystr.="
				LIMIT ".$this->offset.",30";
		} else {
			$querystr.="
				LIMIT 30";
		}

		$db=DB::getInstance ();
		if (!$db->query ($querystr))
			die ("Failed to query inventory list");
		$this->list=$db->fetch ();
		return true;
	}
}

?>