Jump to content

If checkbox checked after using find()


rooofl
 Share

Recommended Posts

I try to display all the pages `news` with a checkbox `show_in_slide` unchecked.

Here is my code:

$news_list = $pages->find("template=news, sort=name, show_in_slide=0");
if ($news_list) {
    echo "There is at least one news not shown in slide";
}else{
    echo "No news";
}

This works if I have such a news, but “There is at least one news not shown in slide” appears also if it is not the case. What I am doing wrong?

Edited by rooofl
Link to comment
Share on other sites

Well,

show_in_slide=

will miss the ones that have value 0

while

show_in_slide=0

will miss the ones that have no value

but

show_in_slide!=1

will fetch all that are empty and those with value 0 (or anything that has not value 1 for that matter)


@rooofl i like your avatar, you realy wrapped your head around it

  • Like 1
Link to comment
Share on other sites

Thank you for your quick replied, but none of the solutions seams to work.

Let me correct my first post: In the case the condition should display “no news”, it displays “There is at least one news not shown in slide”, and not nothing as I wrote before. Sorry for this confusion.

Link to comment
Share on other sites

  • 2 weeks later...

Your problem isn't the selector, but the if condition. $news_list is an object of the class PageArray. Objects always equal true in php. You need to check if there are entries in that object. 

$news_list = $pages->find("template=news, sort=name, show_in_slide!=1");

if ($news_list->count()) {
    echo "There is at least one news not shown in slide";
}else{
    echo "All news items are shown in the slide";
}
Link to comment
Share on other sites

Thank you LostKobrakai, it works perfectly by testing count(), my final code is:

$news_list = $pages->find("template=news, sort=name, show_in_slide!=1");

if ($news_list->count()) {
    echo "There is at least one news not shown in slide";
}else{
    echo "All news items are shown in the slide";
}
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...