Hello I am searching for a way to display a message when toBool() === true is returning empty ?
How can I achieve that ?
Thanks for your help.
<?php if ($projetPage = page('projets')): ?> <?php foreach ($projetPage->children()->listed() as $projet): ?>
<?php if ($projet->ontheshop()->toBool() === true) { ?>
<?= $projet->Productname() ?>
<?php } else {?>
// here is my problem
<?php ($projet->ontheshop()->toBool() === true)->isEmpty() { ?>
<p>No product</p>
<?php } ?><?php endforeach ?><?php endif ?>
texnixe
December 24, 2020, 2:58pm
2
That doesn’t make sense, toBool()
returns true or false, not empty. What do you want to achieve?
Welcome to our forum and merry Christmas
So either it’s true, then your echo the productname, or its false, then you don’t.
Hi thank you ! Merry christmas to you too !
Well I have a shop page. In that shop page, I display product based on if there are toggle $projet->ontheshop()->toBool() === true
And if I have no product toggle true, I want to display a message. Is it possible to achieve ?
texnixe
December 24, 2020, 3:11pm
4
As I already mentioned above, toBool()
either returns true or false, depending on the field value, and also false
if the field is empty. So it works like this:
if ($projet->ontheshop()->toBool() === true ) {
echo 'Yeah, products';
} else {
echo 'Sorry, no products';
}
So your second if statement is superfluous.
Thank you for your reply. Well when I do that, it return
‘Sorry, no products Sorry, no products Sorry, no products Sorry, no products Sorry,’ x10
instead of
'Sorry, no products"
It seem to loop through all the post that are false
texnixe
December 24, 2020, 3:30pm
7
Oh, I see. But to achieve that we need a different approach:
<?php if ($projetPage = page('projets')): ?>
<?php $products = $projetPage->children()->listed()->filterBy('onTheShop', true); ?>
<?php if ($products->isNotEmpty() ): ?>
<?php foreach ( $products as $product ) : ?>
<?= $product->productname() ?>
<?php endforeach; ?>
<?php else: ?>
<?php echo 'Sorry, no products available today'; ?>
<?php endif; ?>
<?php endif;
1 Like
You just gave me the best christmas gift ever !
It work !! Thank you so much
1 Like