Saturday, October 31, 2009

Re: Browse By Letter (a php question)

SELECT SUBSTR(productname, 0, 1), count(*)
FROM products
GROUP BY SUBSTR(productname, 0, 1)
ORDER BY SUBSTR(productname, 0, 1)

If you can create something like that in CakePHP, then you will get
each letter, plus the count of poducts.

If you don't need the count, just do something like:
SELECT DISTINCT SUBSTR(productname, 0, 1)
FROM products
ORDER BY SUBSTR(productname, 0, 1)

Do check that SUBSTR index starts from 0 or one!
Enjoy,
John

On Oct 31, 4:42 am, "gimperdan...@gmail.com" <gimperdan...@gmail.com>
wrote:
> I am working on a project where I would like to have a menu to search
> by Letters.
>
> For instance:
>
> A | B | C | D |E ....
>
> and so on...
>
> However I only want to show the letters that correspond to a product
> on the database.
>
> So if I have product:
>
> Apple, and Banana
>
> I would show the letters: A  | B
>
> As of right now, I do it in a really bad way.. I have a loop that goes
> from A->Z which SELECT COUNTS each letter and throws everything in one
> array.
>
> What's a better way to do that? Instead of have 26 queries?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

No comments: