Jump to content

get data of pages with checked and unchecked property


fabjeck
 Share

Recommended Posts

Hello community, In my code I have a function (see below) that finds the needed articles before they get rendered by another function. And depending on where I want this articles to be shown on the website I want to filter them by the checkbox "force" - all articles with force = unchecked or all articles with force = checked. 

function findArticles($limit, $force) {
    $selector = "template=article, sort=-published, limit=$limit, force=$force, " . trim($selector, ", "); 
    $articles = pages($selector);
    return $articles;
}

The code above works fine, but now in one case I want to show all the articles, no matter if checked or not. If I call the function with $force equal to empty (" ") it only gives me back the articles that aren't checked. 

I could do it like that (see below) but it isn't ideal. Setting $limit to empty (" ") works so why not with the checked?

function findArticles($limit, $force, $all) {
	if($all == true) {
	$selector = "template=article, sort=-published, limit=$limit, " . trim($selector, ", "); 
	} else {
    	$selector = "template=article, sort=-published, limit=$limit, force=$force, " . trim($selector, ", "); 
	}
    $articles = pages($selector);
    return $articles;
}

As always, thanks for helping ;)

Link to comment
Share on other sites

Hi,
Written in the browser – so please excuse my possible mistakes – but one possible solution is not to include the 'limit=$limit' part when you do not need it. Something like this

function findArticles($limit, $force) {
    $force_str = $force === "" ? "" : "force=$force, "; //inject nothing into the selector string if force is empty
    $selector = "template=article, sort=-published, limit=$limit, $force_str" . trim($selector, ", "); 
    $articles = pages($selector);
    return $articles;
}

May the force be with you, BTW ;) 

  • Like 2
Link to comment
Share on other sites

Hi @szabesz. Of course! I could have come up with it by myself.
One little addition: I had to change the comparison operator from equal (==) to identical (===) because comparing an empty string to the value zero (for unchecked) gave back true. And that is finally the reason why i can set $limit to an empty string but not force...

  • 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

×
×
  • Create New...