The joy of a simple SQL query
I was working on an application where I needed all unique brands from a mysql table to populate a HTML select box. I have no idea how to do that quickly with Zend_Table, so I found out you can just pass on your own SQL if you want.
A simple “SELECT DISTINCT brand FROM products ORDER BY brand ASC” will do in these cases. It would even be better if I had a seperate brands table, so I wouldn’t have this problem at all. But since this was a quick and dirty job, there was no brands table ;)
Anyway, these lines will do the job (where $db is an instance of your Db adapter):
$statement = $db->query('SELECT DISTINCT brand FROM products ORDER BY brand ASC');
$this->view->brands = $statement->fetchAll();
Use foreach() in your view to display the list:
echo ' ';
And that’s it!