Page 1 of 1

how to get list of categories from a non-relational database

Posted: Thu Sep 28, 2006 12:26 pm
by Luke
I have a database that I didn't design... nor do I have any control over... and it is set up like this:

Code: Select all

category	memid	address		etc.
"restaurants"	23	1234 Ros rd.	Bla
"lawyers"	234	3214 Poo rd.	BLAB
"restaurants"	23213	1123 Foo Rd.	asdf
"landscapers"	675	5485 Lelu rd.	asdfkj
"restaurants"	7642	234 Poopies	234
What would be the most efficient way to select all categories without duplicates IE:
restaurants
lawyers
landscapers

Posted: Thu Sep 28, 2006 12:38 pm
by Luke
Well... here's what I'm doing for now... please let me know if there's a better way to accomplish this:

Code: Select all

public function loadCategories(){
		$db = $this->DB();
		$sql = "SELECT `category` FROM `" . $this->_table . "`";
		$results = $db->execute($sql);
		if ($results)  {
			while (!$results->EOF) {
				$ret[] = $results->fields[0];
				$results->MoveNext();
			}
			$results->Close();
		}
		return array_unique($ret);
	}

Posted: Thu Sep 28, 2006 12:38 pm
by Benjamin
Probably

Code: Select all

select distinct category

Posted: Thu Sep 28, 2006 12:40 pm
by Luke
awesome! That's exactly what I was looking for! :D

Code: Select all

public function loadCategories(){
		$db = $this->DB();
		$sql = "SELECT DISTINCT `category` FROM `" . $this->_table . "`";
		$results = $db->execute($sql);
		if ($results)  {
			while (!$results->EOF) {
				$ret[] = $results->fields[0];
				$results->MoveNext();
			}
			$results->Close();
		}
		return $ret;
	}