Jump to content

Better way to check for minimum count  if($total > 3)


hollyvalero
 Share

Recommended Posts

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?

 

 

 

Link to comment
Share on other sites

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) {

          }
 

Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

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.

  • Like 1
Link to comment
Share on other sites

@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 by jens.martsch
Corrected the code example
Link to comment
Share on other sites

41 minutes ago, Autofahrn said:

ok, too early... ?

...at least we managed to make this a hot topic: ?

ContMin3.jpg.b78b2735a6042c5330172d4cb094c628.jpg

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!

 

 

  • Like 1
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...