tags:

views:

29

answers:

2

I'm creating eBay style search panes that allow users to narrow their results set by certain criteria. For example:

Tags

Literature (8) Fiction (4) English (4) American (3) Non-fiction (2)

The number of results which have that property is in brackets. Initially, I built the counters by putting this code in my display results loop:

if(isset($tags[$row['tags']])) {
    $tags[$row['tags']]++;
} else {
$tags[$row['tags']] = 1;
}

Then I paginated my results and that stopped working - the query only returns enough data for the page you are on, so the counters only represent the results on that page.

Can anyone suggest another approach?

A: 

You'll have to run a second query for tag, COUNT(*) in <table> GROUP BY tag to get total numbers for each tag.

edit:

Check this out. This is a new one by me, but it looks like what you want.

Start your query with SELECT SQL_CALC_FOUND_ROWS, then follow up your query with SELECT FOUND_ROWS();

keithjgrant
Tags are just one of several criteria. If at all possible I would prefer to avoid additional queries.
Dae
Your other option is to do a full query, without a limit. Then you can count up your tags and do pagination logic in PHP. This will be a heavier approach, at least in the PHP if not on the database, but it should be more flexible.
keithjgrant
You're right, but it's a tough call - extra queries versus grabbing the whole data set on every page. I wonder how other sites that display this kind of information structure their code.
Dae
I know what you mean. This has to be a common problem. The `FOUND_ROWS()` thing is really helpful, but I didn't see anything about how to pair it with a `GROUP BY`.
keithjgrant
A: 
SELECT COUNT(*) FROM books WHERE tagid=$tagid

This would require another query however. Please supply more info about your pagination methods.

Lotus Notes
I paginate with LIMIT: $from = (($page * $results_per_page) - $results_per_page); $sql .= " LIMIT ".$from.",".$results_per_page;
Dae