Jump to content

Recommended Posts

Posted

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?

 

 

 

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

          }
 

Posted

Even shorter:

$products = $pages->find("template=product, limit=9, sort=sort")->count(3);
if ($products) d($products);

 

  • Like 1
Posted
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. 

Posted
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. 

Posted
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...

Posted
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
Posted

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
Posted (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 by jens.martsch
Corrected the code example
Posted

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

  • Like 1
Posted
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

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...