Jump to content
hollyvalero

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

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?

 

 

 

Share this post


Link to post
Share on other sites

I'd probably do the find and then check if $products contain the minimum amount. Otherwise you'll have two database queries.

Share this post


Link to post
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) {

          }
 

Share this post


Link to post
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. 

Share this post


Link to post
Share on other sites
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. 

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

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.

  • Like 1

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

For me the count() always returns the amount of pages found, regardless of its argument. But I'm on 3.0.123 still...

  • Like 1

Share this post


Link to post
Share on other sites
33 minutes ago, Autofahrn said:

Always learning... 

ok, too early... 😉

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

ContMin3.jpg.b78b2735a6042c5330172d4cb094c628.jpg

  • Like 3

Share this post


Link to post
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

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...