Jump to content

Feasibility - Sending Search Terms via Email & writing them to the Database


Neo
 Share

Recommended Posts

Dealing normally with simpler "brochure-style" websites, I am currently trying to figure out how to approach the following, more "advanced" problem in ProcessWire, so please bear with me:

I would like to build a database of cars, which needs to be searchable by type, model, brand, registration date, mileage etc.

For the database organisation, I already found kongondo's great post on "Approaches to categorizing site content". Will have to spend more time on the detailed organisation, while keeping things practical & manageable.

For the search feature, I had a look at the search functions of Ryan's excellent Skyscrapers Site Profile. This uses the GET method in combination with search.php and the rendering functions in functions.php to request database entries that match the specified terms.

Now the tricky part: I would like to send the search terms of every search request via email (PHPMailer) and ideally post them to the database as a new hidden page, which would require a POST request. I have created new pages before via the API based on form entries, so this should not be a problem. However, to my knowledge, you cannot POST and GET at the same time and from a security perspective, a GET request shouldn't change data on the server.

Maybe this sounds trivial to a more experienced developer, but I would appreciate your advice.

I am sure this is doable in ProcessWire (is there anything that isn't ?!?). ;) 

 

 

Link to comment
Share on other sites

1 hour ago, Neo said:

I would like to send the search terms of every search request via email (PHPMailer) and ideally post them to the database as a new hidden page

It sounds a little unusual (typically you would use an analytics service to track this kind of stuff) but quite doable. You would do it all in your Search template - I don't think there is any issue regarding GET/POST. You sanitize and check the search terms from $input and if they meet your criteria then send the email and create the page. Then output the search results.

It would slow down your search results a bit. If that is a problem you could look at using a queue but I don't know much about that.

  • Like 2
Link to comment
Share on other sites

Yeah this is doable I suppose but Robin does have a good point, this could add some time to your queries as they would be saving the data to the same database that you are querying. You may want to look into something so that you can track analytics on the terms that you are tracking. I have used piwik in the past for this and google analytics also has a really great method of logging this.

One other method if you just want a log would be to use the processwire $log variable to save a log of the term to a log file. https://processwire.com/api/variables/log/

If you want to save to a page you can do this (just stole this from an old post by soma so I don't have to type):

$p = new Page(); // create new page object
$p->template = 'page'; // set template name here
$p->parent = $pages->get('/search-terms/'); // set the parent
$p->name = 'mynewpage_url'; // give it a name used in the url for the page
$p->title = 'My New Page'; // set page title (not neccessary but recommended)
$p->save();

To use mail just read about wiremail

 

  • Like 2
Link to comment
Share on other sites

Thanks for your responses.

Yes @adrian, the idea to record and send search requests is unusual, but this will not be a high traffic site and there will be additional filters, who can access the search function.

To summarize your input:

1. Search: 

As @Robin S mentioned, can I use a simple GET request to handle the search terms from the form and then process input in a single search template (search.php)? I.e. Get search terms -> sanitize input -> save input to variables -> create PHPMailer object and send input variables -> Show search results on the front-end ?

2. Database Design: 

I had a closer look at  the car example of kongondo's post on "Approaches to categorizing site content", i.e. simple multiple categories.

So the objective is to have typical search requests like this:

"Show me all red, second-hand vans from Toyota with Diesel engine built in 2012 with a price below 15.000 EUR."

Instead of adding the required fields directly to my car-template.php, I would create pages with sub-pages as attribute categories and sub-categories in the tree, which will then be referenced in my car-template.php as Page Fieldtypes, correct? I assume this keeps the attributes more manageable if further sub-categories need to be added in the future?

So I would probably have a tree view similar like this: 

 

- Cars (car-template.php) -> this is where new vehicles will be added

- Make

     -- Toyota

     -- Volkswagen

     -- BMW

     ...

- Year

      -- 2000

      -- 2001

      -- 2002

      -- 2003

      ...

- Color

     -- Red

     -- White

     -- Blue

     ...

- Fuel

     -- Gas

     -- Diesel

     -- Electric

     ...

- Condition

     -- New

     -- Used

- Gear

     -- Automatic

     -- Manual

     -- Semi-automatic

- Body

    - Compact

    - Convertible

    - Coupe

    - Off-Road

    - Van

    ...

- etc.

   ... 

 

Now, there will probably be a couple of fields that won't be directly categories, such as price, mileage, description, images.

Price and mileage, however, should be searchable. 

I assume those can be added directly to car-template.php instead of creating individual pages?

 

Would appreciate your feedback if this makes sense.

  

 

 

  • Like 1
Link to comment
Share on other sites

8 hours ago, Neo said:

I assume those can be added directly to car-template.php instead of creating individual pages?

All your fields will be added directly to your template - Page fields and other fields. For the Page fields, it is the selectable options for the field that you will create as individual pages elsewhere in the tree. You could put these 'option' pages and their parents directly under the Home page, but I like to group them under a 'Selects' page to separate them from the rest of the site pages.

Home
  Selects
    Make
      Toyota
      Volkswagen
      ...
    Color
      ...

A couple of easy ways to create these pages:

1. Use adrian's great Page Field Select Creator module. This is good for when you want to include the ability to add pages (i.e. options for the select) via Pages > Add New because it will create dedicated templates for the option page and the parent page. The module creates the Page field for you.

2. When I don't need the use of the Add New feature and want to avoid unnecessary extra templates I use ImportPagesCSV with the "Paste in CSV Data" option. If all you need is the title then for example you would enter:

title
Toyota
Volkswagen
...

With this option you would create the Page field yourself and choose selectable pages, etc.

  • Like 4
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

  • Recently Browsing   0 members

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