Jump to content

Recommended Posts

Posted

I would like to test if a certain single page exist... using a switch (true or false) or if statement. I can use $pages->find("/$contract/") or $pages->get("/$contract/"). find() is returning a pageArray, get() is returning a page or NULL. So which one is better to test?

Is a NULL page still evaluating to true?!

Posted

better count than find

if($pages->count("/contract/")){ 
    // exists
}

with find() it would be slightly less efficient

if($pages->find("/contract/")->count){ 
    // exists
}

Or get() will return a NullPage object if not found

if(!pages->get("/contract/") instanceof NullPage){
     // exists
}
  • Like 1
Posted

Then for accuracy count (your first example) is better. Thanks Soma!

Probably there is no way to make this shorter:

$contract = $sanitizer->text($input->post->contract);
if ($pages->count("/contract/{$contract}")) {
Posted

Well, still having trouble with this...

my default option for a dropdown is this: <option value=''>Any</option>

if ($input->post->submit) {

    if ( empty($input->post->contract) ) { echo "hello" ; }
    // is returning hello when "Any" is submitted. So we know it is empty!

   echo $input->post->contract; // is returning the value if other option then "Any" is chosen.

  $contract = $sanitizer->text($input->post->contract); echo $contract; 
  // is returning the value if other option then "Any" is chosen

  if($pages->count("/contract/{$contract}"))  echo "found";
  // this is wrong, it returns "found" with option "Any" or another value chosen. Empty or not!

And that last part I can't find out what's wrong, since clearly the page "Any" is not existing...

Posted

It's just an empty name used in the <option> and not a page.

The directory I check is /contract/$contract

where $contract is the $input->post->contract <option> selected

Maybe that's the issue: if $contract is empty, it checks the /contract/ which does exist... hmmm

how to do this?

-- EDIT --

$pages->count("/contract/, child={$contract}"); works. Not sure if this is the fastest way though...

not working

Posted

The problem is not the /contract/ but the $contract I need to check. So $contract represents a child page name in string format

parent: contract

child: $contract

path: /contract/$contract

Posted

$pages->get("parent=/contract/, name=$contract")

You might consider changing the name of the parent to contracts

Posted

$pages->get("parent=/contract/, name=$contract")

You might consider changing the name of the parent to contracts

Thanks Diogo / Soma !

I had to combine your replies into 1, or combine Soma's with your's. To make this short, I am testing to see if a page really exist.

Using get I need to add ->id, otherwise I am getting a false positive/negative (returning NullPage).

Using count() without ->id, otherwise that last is property of non-object (if page exist or not).

get() with id

if($pages->get("parent=/contracts/, name={$contract}")->id) {
    echo "found";
} else {
    echo "not found";
}

count() without id

if($pages->count("parent=/contracts/, name={$contract}")) {
    echo "found";
} else {
    echo "not found";
}
Posted

There's a difference between the get and the count. Count uses the find methode internally, thus it won't count the page if it is in a hidden state.

But the get will regardless of the state. 

Correct me here if i'm wrong.

Posted

Hey Martijn!

Count() and find() does not return NullPage is not exist.

Get() does. Unless you add specificly ->id

But for testing, between count() and find(), the API states count() just returns the quantity, and does not load the page (if it does find).

  • 4 years later...

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