Jump to content

find vs get - page exist true or false


bwakad
 Share

Recommended Posts

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?!

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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}")) {
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

$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";
}
Link to comment
Share on other sites

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

Link to comment
Share on other sites

  • 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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...