Thank you, gentlemen ... you're hitting pretty close here.
We're pretty pragmatic here ... I'm not above ugly if it works.
Weedpacket when will you have a collection that you might want to know the population of but never the actual elements of the collection?
Pretty sure we just want to know if the collection's empty or not; see below.
Weedpacket if $products has nothing to iterate over then the foreach will loop zero times and isn't that what you want?
I'm pretty sure the conditional was put there to decide whether to display the collection or show a nice error page saying "there's nothing here for the params specified." The foreach() is in the non-error loop; it seemed to me, ever so long ago, that if count()
returned non-zero for an array, that meant that we had a non-empty collection and foreach()
could operate. I need to pin down the exact state/nature of $products
when we get this error; obviously foreach()
works as expected thousands of times a day and we see this only a couple dozen times a day in an errorlog.
In a more basic setting, or the Elder Days, I'd probably do something likeif ($result->num_rows) {
, but we've got things being held in objects, so in many cases $products
is actually $category->products
or $search->products
and the SQL's long since over && done. I guess we could refactor all those objects so they'd know (and advertise?) if they had rows, but then there's the issue of pagination which will also be touched ... which was also "solved" quite some time ago and will probably make a refactor a tad more challenging; off the top of my head I can't remember what we did there (seems it was maybe some math to figure beginning and ending numbers for the SQL LIMIT clause ... maybe it wouldn't be terrible.
Not to mention I have several projects with pending changes to be committed and the Tracker hates me:
@MantisBT Viewing Issues 1 - 50 / 160 😬
Those are the unresolved ones ....