hollyvalero Posted May 17, 2019 Posted May 17, 2019 I'm trying to come up with an automatic feature for a home page that will display 3 products (or more) in a slider carousel as long as there is a minimum of 3 items that qualify If there are less than 3, they would just not show up. I've been trying to add a count and this is working but it seems, at best, inelegant... $total = $pages->count("template=product"); if($total > 3) { $products = $pages->find("template=product, limit=9, sort=sort" ); } Is there a better/shorter way to put this?
Autofahrn Posted May 17, 2019 Posted May 17, 2019 I'd probably do the find and then check if $products contain the minimum amount. Otherwise you'll have two database queries.
hollyvalero Posted May 17, 2019 Author Posted May 17, 2019 5 minutes ago, Autofahrn said: I'd probably do the find and then check if $products contain the minimum amount. Otherwise you'll have two database queries. I had something like this but it wasn't working... $products = $pages->find("template=product, limit=9, sort=sort" ); if($products > 3) { }
Autofahrn Posted May 17, 2019 Posted May 17, 2019 try: $products = $pages->find("template=product, limit=9, sort=sort" ); if($products->count() > 3) { } https://processwire.com/api/ref/wire-array/count/ 3
hollyvalero Posted May 17, 2019 Author Posted May 17, 2019 16 minutes ago, Autofahrn said: try: $products = $pages->find("template=product, limit=9, sort=sort" ); if($products->count() > 3) { } https://processwire.com/api/ref/wire-array/count/ Thank you! 1
dotnetic Posted May 17, 2019 Posted May 17, 2019 Even shorter: $products = $pages->find("template=product, limit=9, sort=sort")->count(3); if ($products) d($products); 1
hollyvalero Posted May 17, 2019 Author Posted May 17, 2019 21 minutes ago, jens.martsch said: Even shorter: $products = $pages->find("template=product, limit=9, sort=sort")->count(3); if ($products) d($products); THAT'S very cool. Just need to figure out what the "d" in d($products) means.
elabx Posted May 17, 2019 Posted May 17, 2019 9 minutes ago, hollyvalero said: THAT'S very cool. Just need to figure out what the "d" in d($products) means. It's a debug command from Tracy Debugger module.
Autofahrn Posted May 17, 2019 Posted May 17, 2019 28 minutes ago, jens.martsch said: Even shorter Is this mentioned somewhere that PageArray::count resp. WireArray::count may return something else than an int? Always learning...
dragan Posted May 17, 2019 Posted May 17, 2019 https://adrianbj.github.io/TracyDebugger/#/debug-methods?id=dump In case you haven't installed Tracy Debugger yet, you should do it NOW ? 3
elabx Posted May 17, 2019 Posted May 17, 2019 4 minutes ago, Autofahrn said: Is this mentioned somewhere that PageArray::count resp. WireArray::count may return something else than an int? Always learning... What's happening is that integer 0 loosely evaluates to false and any value 1+ evaulates to true: https://www.php.net/manual/es/types.comparisons.php#types.comparisions-loose
Autofahrn Posted May 17, 2019 Posted May 17, 2019 Just now, elabx said: What's happening is that integer 0 loosely evaluates to false that's clear. But to fulfill the initial request, $products should hold the full result from the find in case it contains at least three elements. So PageArray::count(int) is expected to be something conditional and returns the full array if the array contains the requested number or something empty. 1
elabx Posted May 17, 2019 Posted May 17, 2019 Yes sorry! Jumped ahead too fast! Now I am in intrigued too ? Could you throws us a lead @jens.martsch ? Like, I cannot find PaginatedArray or PageArray classes to have their own implementation of count. 1
dotnetic Posted May 17, 2019 Posted May 17, 2019 (edited) @elabx You are also correct with your statement, that integer 0 loosely evaluates to false. But as @Autofahrn said, the original poster @hollyvalero wants to work with the product array afterwards. But as my query does only return false or the integer number, that is not what he needs. So something like $products = $pages->find("template=news, limit=9, sort=sort" ); if($products->count > 3) { echo $products->each(function($product) { return "<p>$product->title</p>"; }); } would be correct Edited May 17, 2019 by jens.martsch Corrected the code example
Autofahrn Posted May 17, 2019 Posted May 17, 2019 For me the count() always returns the amount of pages found, regardless of its argument. But I'm on 3.0.123 still... 1
dotnetic Posted May 17, 2019 Posted May 17, 2019 Damn, I am very sorry. I mislead you. I thought it returns false, if the count fails, but it turns out, it really returns the number. Sorry. I updated my example above, which is basically the same as @Autofahrn's 1
Autofahrn Posted May 17, 2019 Posted May 17, 2019 33 minutes ago, Autofahrn said: Always learning... ok, too early... ? ...at least we managed to make this a hot topic: ? 3
hollyvalero Posted May 17, 2019 Author Posted May 17, 2019 41 minutes ago, Autofahrn said: ok, too early... ? ...at least we managed to make this a hot topic: ? Ha ha! Thank you for all your help. I did use Tracy Debugger when I first started and I wasn't skilled enough for it to be helpful... I will try that again! Good idea! 1
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now