Jump to content

Checking if Pages are already created, if not; create page


louisstephens
 Share

Recommended Posts

I have a bit of a head scratcher for myself. I have a form (first name, last name, and domain) that I use to create pages. I have the first name and last name being combined and saving as the title, and being saved in their individual fields as well. The domain name is just being saved in it's designated field under the page. This all works quite well in fact.

(just a note: for a special case, I am combining all three values so it looks like johndoe@domainselected.com)

However, I thought I might need to check to see if the page already existed, and if it does, provide the user with an error. If it doesn't already exist, follow through with creating the page. I didnt want to save the entire address in the title as I knew "@" wouldn't fly with the url to the page.

I thought I could use a foreach, but I understand that outputs an array, when I need to check just one entry.

Does anyone have any suggestions? I am probably making this much more difficult than it needs to be.

 

 

I have fixed the issue... "==" seemed to fix it.

Link to comment
Share on other sites

Can you post your code where the page from the formis being created?

It's just a IF/ELSE request..

Something like (not tested):
 

 

<?php
$firstname = $sanitizer->text($input->get->firstname); // firstname should be the name of the input field
$lastname = $sanitizer->text($input->get->lastname); 
$fullname = $firstname . $lastname;
$sitetocheck = strtolower($fullname);

if ( $pages->count("name=$sitetocheck") {
  echo "Sorry, seems you 're already registered";
} else {
  $p = new Page(); 
  $p->template = 'basic-page'; // set template
  $p->parent = wire('pages')->get('/about/'); // set the parent
  $p->name = '$fullname'; 
  $p->title = '$fullname'; 
  $p->save();
  echo "Thanks for registration";
}
?>

 

Link to comment
Share on other sites

Instead of $pages->count you may prefer to use $pages->findOne, so you can search for pages hidden or unpublished. 

See: https://processwire.com/api/ref/pages/find-one/

So:

<?php
$firstname = $sanitizer->text($input->get->firstname); // firstname should be the name of the input field
$lastname = $sanitizer->text($input->get->lastname); 
$fullname = $firstname . $lastname;
$sitetocheck = strtolower($fullname);
$p = $pages->findOne("name=$sitetocheck");

if ( $p->id ) {
  echo "Sorry, seems you 're already registered";
} else {
  $p = new Page(); 
  $p->template = 'basic-page'; // set template
  $p->parent = wire('pages')->get('/about/'); // set the parent
  $p->name = '$fullname'; 
  $p->title = '$fullname'; 
  $p->save();
  echo "Thanks for registration";
}
?>

 

  • Like 1
Link to comment
Share on other sites

10 minutes ago, Sérgio Jardim said:

Instead of $pages->count you may prefer to use $pages->findOne, so you can search for pages hidden or unpublished. 

I think $pages->count() supports the same selectors as $pages->find(), plus you can supply an options array with "findAll", so should be no problem counting hidden or unpublished pages.

  • Like 2
Link to comment
Share on other sites

9 minutes ago, Robin S said:

I think $pages->count() supports the same selectors as $pages->find(), plus you can supply an options array with "findAll", so should be no problem counting hidden or unpublished pages.

You're right! I didn't know that. :) 

Link to comment
Share on other sites

11 hours ago, Sérgio Jardim said:

Instead of $pages->count you may prefer to use $pages->findOne, so you can search for pages hidden or unpublished. 

in fact this is wrong!

this is correct:

$pages->get(123); // returns also hidden and unpublished pages
$pages->find('id=123'); // returns a "PageArray", but only published + visible pages
$pages->findOne('id=123'); // returns a single "Page" object, also only published + visible pages

find and findOne can be modified with your selector like "include=hidden"...

  • Like 1
Link to comment
Share on other sites

I spoke too soon, I got bogged down. I notice that findOne() only finds one (obviously, unless I am missing something), and returns the same id each time. Is there a way to compare the form entry amongst all of the entries that have been created?

Link to comment
Share on other sites

42 minutes ago, louisstephens said:

findOne() only finds one

Why do you need more?

You check if the page exists, so one is enough.

Also adding the parent to the selector will make your check more accurate, there might be other pages with the same title but with a different parent.

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