[RESOLVED Temporarily] Filtering
Is it possible to filter a resultset of a db query? I dont want to hit the db with two calls and my query is joined to bring the related resultset back but upon iterating it may be best to loop through the one condition and then reloop through the other. There will be no duplication of items in the loop so its not a resource drain or waste.
For ex. if you have returned items that can be visible and invisible then it would be a waste to call the db twice just to loop through the visible ones and then another call and loop through the invisible ones. So if I have 5 visible and 5 invisible items then I only want to loop a total of 10 times; filter for visible, loop 5 times, unfilter, refilter for invisible, loop 5 more times and release resources.
Thanks
Re: [RESOLVED Temporarily] Filtering
yeah i didnt quite understand what you were trying to do either... well good luck
Re: [RESOLVED Temporarily] Filtering
Well I put in the dual db calls for now but I will change it later once its finished and we polish it up. I dont like depending on the order by clause to present the listing correctly as its not good logic of dependable.
So I cant do like #7 which is more reliable?
Re: [RESOLVED Temporarily] Filtering
If an ORDER BY clause wasn't reliable it would be useless.
I think I know what you're doing.
How about this?:
PHP Code:
$records = $db->query_read('SELECT blah.* FROM blah ORDER BY (blah.visible=TRUE) ASC');
while ($row = $db->fetch_array($records) && !$row['visible'])
{
# non-visible items
}
while ($row = $db->fetch_array($records))
{
# visible items
}
Edit: Scupper that: that'll enumerate the non-visible items twice if there are one or more non-visible items and no visible items.
Re: [RESOLVED Temporarily] Filtering
Yes, I just wasnt sure if the fetch_array would work twice as it may not have been scrollable, was my worry. The docs sstate it uses a pointer to read the recordset but fails to mention if its cached and is scrollable etc.
Yes, you are thinking correct.
Ps, check out the page its on as its FINISHED :D
Re: [RESOLVED Temporarily] Filtering
fetch_array returns each item in order, until you pass the last item, at which point it returns NULL and resets the pointer to the start. The recordset is cached; fetch_array merely enumerates it. Using a server-side cache (cursor) is somewhat trickier, and unnecessary in this situation.
Re: [RESOLVED Temporarily] Filtering
So then if you looped through the first 5 items as visible when you went to loop through the next 5 invisible items it should start at item 6.
I just dont like counting on counting as there may be instances where the data may be slightly inaccurate and this may lead to other issues.