Jump to content

Recommended Posts

Posted

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.

Posted

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";
}
?>

 

Posted

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

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

Thanks everyone for the help. With all the suggestions and a few references, I was able to get it all working, event though I always seem to miss one key thing in the API documentation.

Posted

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?

Posted
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
Posted
findOne() only finds one, made my day 
You check if the page exists. If not = error, else= create page.
You can post your code when you have further questions.

 

 

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
×
×
  • Create New...