-
Posts
733 -
Joined
-
Last visited
-
Days Won
9
Everything posted by SamC
-
Hi, I got round to installing the tracy debugger. Pretty lost with it tbh, looks like a huge variety of options. Anyway, just testing it out, but when I click on the console, there's nowhere to type: Looking at some screenshots, seems there should be a black box in there. Not sure what I'm doing wrong here. Brand new install 3.0.42 with Tracy 4.3.2, using google chrome. [SOLVED - SEE EDIT BELOW] One other thing, I wanted to test the 'bd()' method to see what's actually in the whole array. So I can't get the console working (as above) so I typed it straight in my template and I get some output in the dumps recorder. The array is bigger than what it output though so I can't see it all. I used it like this in the template: <?php $json = file_get_contents($config->paths->templates . "/file.json"); json_decode($json, $assoc = true); bd($json); ?> ...which outputs: I've been reading on this page: https://processwire.com/blog/posts/introducing-tracy-debugger/#dumps-recorder-panel but still not getting it right. Any advice would be great, thanks. ==EDIT== Solved the second bit by using: <?php $json = json_decode(file_get_contents($config->paths->templates . "/file.json"), $assoc = true); bd($json); ?> Still no console though.
-
@Robin S that's an awesome response, thanks. I'm going to get the Tracy debugger installed as I haven't used it yet, then test how to make pages/fields with the API in a template rather than through the admin.
-
@ryanC this is a very helpful resource for image handling: https://processwire.com/api/fieldtypes/images/ CKeditor is a different story, it will strip out tags unless you tell it otherwise. I found trying to manipulate CKEditor to be a pain in the butt and I try to not use any inline stuff in there at all (no added classes/ids or anything), I've always found you can target HTML tags using CSS wherever they might be. I simply use it for bigger blocks of text that need header/paragraph combos and maybe an image inserted. You can search the forum for "ckeditor allowed styles" or "ckeditor stripping tags" (something along those lines) and there are a few posts about editing the config file to allow what you are asking for.
-
Hi @ryanC I have an images folder which I keep stuff like logo, things that aren't inserted via the admin (as a image file upload or whatever). What I use to get these images is: <img src='<?php echo $config->urls->templates . "/site-assets/images/logo.png"; ?> I don't know what you mean when you mention CKEditor stripping tags, or "Processwire field just gives me a broken link icon". The above is for images not uploaded via the system i.e. I haven't used a image upload field and the code in the template simply displays them. If you have a field which is an image upload field, and you add that to a template (say basic-page.php), then you create a page using that basic-page.php template. If you then upload an image via that field and click save, the images will reside in a path like: yoursite.com/site/assets/files/27 ...where the number at the end is the ID of the page where the image has been associated with (or uploaded to). You don't manually create these folders, the system does it for you. Hope that helps a little.
-
Hi, I might have to create the following website with parts of it coming from an external feed. Some pages about, contact, staff profiles etc... will be standard PW pages. However, the building listings will come from a JSON feed. What I 'think' I need to do is: 1) Get the feed, read the file, make it a JSON string - can only be accessed once every 24hrs 2) Use json_decode to create an object/or assoc array 3) Create (a) a template building-entry (b) fields for the template - either manually (because I know the required fields in advance or using info from the feed (with the PW API) 4) Add these fields to the template 5) Create pages using the data 6) Every 24hrs, the feed needs to be compared to the pages in the tree, if the building has been deleted from the feed, needs to be deleted from the tree. If a building has been changed/edited in the feed, these changes need to be reflected in the pages in the tree. ...so I basically end up with this lot in the tree: - buildings (parent - building-index.php) -- building 1 (child - building-entry.php) --- title --- desc --- area --- type -- building 2 (child - building-entry.php) --- title --- desc --- area --- type -- building 3 (child - building-entry.php) --- title --- desc --- area --- type The buildings themselves are not editable in PW, they will use a CRM to upload the details, all I get is the JSON feed. I wanted this approach then I can use PW for filtering/sorting etc. and I'm fairly comfortable with presenting the data to the end user once it's in the tree. I guess I'm not going to progress unless I make something more complex but this worries me somewhat being more of a designer. Been brochure style sites (with maybe a news/blog section) all the way until now. Any advice would be awesome, thanks.
-
Thanks @DaveP I've now found out it is a JSON feed so I'll try and work out how to do this. I'll start a new thread instead of hijacking this one.
-
That's awesome! Thanks @totoff
-
Nice! Thanks for sharing @arjen.
-
I'm glad this thread is here. I might have to do exactly the same things on a job I'm looking at. I imagined the pages (about, contact etc,..) being just PW pages but the feed (I don't know the type yet) will contain the data required for the actual properties. The feed will come from a cloud CRM system where the actual properties are uploaded to. Imagine the feed changes once a week, the PW pages have to reflect this. The property pages displayed in PW will not be edited in PW. So how do you go about getting this feed and making PW pages out of it? This I'm confused about.
-
@Sevarf2 this may help you:
-
<?php namespace ProcessWire; ?> <?php if($input->urlSegment1) { $segment1 = $sanitizer->pageName($input->urlSegment1); $segment1 .= ".name="; } ?> <?php if($input->urlSegment2) { $segment2 = $sanitizer->pageName($input->urlSegment2); } ?> <h1>Index of cars</h1> <?php $cars = $pages->find("template=car-entry," . $segment1 . $segment2); print_r($cars); foreach ($cars as $car): ?> <h2><?php echo $car->title; ?></h2> <p>MAKE: <a href="/cars/make/<?php echo $car->make->name; ?>"><?php echo $car->make->title; ?></a></p> <p>MODEL: <a href="/cars/model/<?php echo $car->model->name; ?>"><?php echo $car->model->title; ?></a></p> <p>YEAR: <a href="/cars/year/<?php echo $car->year->name; ?>"><?php echo $car->year->title; ?></a></p> <p>COLOUR: <a href="/cars/colour/<?php echo $car->colour->name; ?>"><?php echo $car->colour->title; ?></a></p> <p>SEATS: <a href="/cars/seats/<?php echo $car->seats->name; ?>"><?php echo $car->seats->title; ?></a></p> <p>TYPE: <a href="/cars/type/<?php echo $car->type->name; ?>"><?php echo $car->type->title; ?></a></p> <?php endforeach; ?> AT: /cars/year/2005 AT: /cars/seats/5
-
As usual @adrian you're spot on. Thanks now I can move on.
-
I don't have tracy debugger installed, I'll look into that. It's weird though because even manually making the selectors: Good condition small car MAKE: Ford MODEL: Ka YEAR: 2008 COLOUR: Black SEATS: 5 TYPE: Other // no results $cars = $pages->find("template=car-entry, seats=5"); // no results $cars = $pages->find("template=car-entry, year=2008"); // returns car above $cars = $pages->find("template=car-entry, make=ford"); // returns car above $cars = $pages->find("template=car-entry, model=ka"); // returns car above $cars = $pages->find("template=car-entry, colour=black"); I can't see the difference between the selectors, other than the first two are numbers.
-
So I've been working with categories: ...and advancing upon this as a way to get to learn URL segments. So mysite.com/cars/ outputs all cars: ...with the code being: // car-index.php <?php namespace ProcessWire; ?> <?php if($input->urlSegment1) { $segment1 = $sanitizer->pageName($input->urlSegment1); echo $segment1; } ?> <?php if($input->urlSegment2) { $segment2 = $sanitizer->pageName($input->urlSegment2); echo $segment2; } ?> <h1>Index of cars</h1> <?php $cars = $pages->find("template=car-entry," . $segment1 . "=" . $segment2); foreach ($cars as $car): ?> <h2><?php echo $car->title; ?></h2> <p>MAKE: <a href="/cars/make/<?php echo $car->make->name; ?>"><?php echo $car->make->title; ?></a></p> <p>MODEL: <a href="/cars/model/<?php echo $car->model->name; ?>"><?php echo $car->model->title; ?></a></p> <p>YEAR: <a href="/cars/year/<?php echo $car->year->name; ?>"><?php echo $car->year->title; ?></a></p> <p>COLOUR: <a href="/cars/colour/<?php echo $car->colour->name; ?>"><?php echo $car->colour->title; ?></a></p> <p>SEATS: <a href="/cars/seats/<?php echo $car->seats->name; ?>"><?php echo $car->seats->title; ?></a></p> <p>TYPE: <a href="/cars/type/<?php echo $car->type->name; ?>"><?php echo $car->type->title; ?></a></p> <?php endforeach; ?> The natural tree structure would be: site.com/make/mercedes/ site.com/model/e-class/ site.com/year/2005/ site.com/colour/red/ site.com/seats/5/ site.com/type/mpv/ ... but I got this working ok without needing URL segments. Just saying so you are aware that I am doing this approach on purpose even though it's not necessary. The code above shows that I added a /cars/ into the anchor tags, thus: site.com/cars/make/mercedes/ site.com/cars/model/e-class/ site.com/cars/year/2005/ site.com/cars/colour/red/ site.com/cars/seats/5/ site.com/cars/type/mpv/ ...I enabled URL segments in the car-index.php template and gave it a shot. So the links with text work: AT: site.com/cars/make/mercedes/ print_r($cars); // outputs ProcessWire\PageArray Object ( [hooks] => Array ( [PageArray::render] => MarkupPageArray->renderPageArray() in MarkupPageArray.module [PageArray::renderPager] => MarkupPageArray->renderPager() in MarkupPageArray.module ) [count] => 1 [items] => Array ( [0] => /cars/old-banger/ ) [total] => 1 [start] => 0 [limit] => 0 [selectors] => template=car-entry, make=mercedes, status<1024 ) ...but the years and seats don't? AT: site.com/cars/year/2005/ print_r($cars) // outputs ProcessWire\PageArray Object ( [hooks] => Array ( [PageArray::render] => MarkupPageArray->renderPageArray() in MarkupPageArray.module [PageArray::renderPager] => MarkupPageArray->renderPager() in MarkupPageArray.module ) [count] => 0 [total] => 0 [start] => 0 [limit] => 0 [selectors] => template=car-entry, year=2005, status<1024 ) Have I got a typo or something? I thought it was something to do with: $segment2 = $sanitizer->pageName($input->urlSegment2); ...but even just using: $segment2 = $input->urlSegment2; ...doesn't work either. Any ideas? Thanks. --EDIT-- adding another image to show how it's setup in admin.
-
This prints an ID of the page being referenced i.e. <h1>Index of all cars</h1> <?php $cars = $pages->find("template=car-entry"); foreach ($cars as $car): ?> <h2><?php echo $car->title; ?></h2> <p>MAKE: <?php echo $car->make; ?></p> <p>MODEL: <?php echo $car->model; ?></p> <p>YEAR: <?php echo $car->year; ?></p> <p>COLOUR: <?php echo $car->colour; ?></p> <p>SEATS: <?php echo $car->seats; ?></p> <p>TYPE: <?php echo $car->type; ?></p> <?php endforeach; ?> ...prints... Top of the range SUV MAKE: 1069 MODEL: 1078 YEAR: 1079 COLOUR: 1054 SEATS: 1057 TYPE: 1061 Instead... <h1>Index of all cars</h1> <?php $cars = $pages->find("template=car-entry"); foreach ($cars as $car): ?> <h2><?php echo $car->title; ?></h2> <p>MAKE: <?php echo $car->make->title; ?></p> <p>MODEL: <?php echo $car->model->title; ?></p> <p>YEAR: <?php echo $car->year->title; ?></p> <p>COLOUR: <?php echo $car->colour->title; ?></p> <p>SEATS: <?php echo $car->seats->title; ?></p> <p>TYPE: <?php echo $car->type->title; ?></p> <?php endforeach; ?> prints... Top of the range SUV MAKE: Land Rover MODEL: Range Rover YEAR: 2015 COLOUR: White SEATS: 5 TYPE: SUV Great guide @kongondo has been very helpful indeed this morning thanks.
-
Ok, I'm not so good at shutting down and 'turning off' when there's a problem to be solved. So my emails are sending now, I get the success message, but no emails! So I even went as far as messing about in php.ini to enable the sendmail, it's off by default in MAMP (free) but switchable via the UI in MAMP (paid). So I did this: http://www.blog.tripleroi.com/2012/05/solvedenabling-sendmail-on-localhost.html Tried again, no luck. Then noticed I had 16 emails in my SPAM folder! Lesson learned. Here's my rendition of contact.php, just a single file, using the MarkupGoogleRecaptcha module, works locally (validates form/validates recaptcha, and sends email correctly): // contact.php <?php namespace ProcessWire; wireIncludeFile("./vendor/vlucas/valitron/src/Valitron/Validator.php"); $captcha = $modules->get("MarkupGoogleRecaptcha"); $contactPageID = '1022'; $contactFormRecipient = 'my_email'; $name = $sanitizer->text($input->post->name); $email = $sanitizer->email($input->post->email); $message = $sanitizer->text($input->post->message); $v = new \Valitron\Validator(array( 'name' => $name, 'email' => $email, 'message' => $message ) ); $v->rule('required', ['name', 'email', 'message']); $v->rule('email', 'email'); if ($input->post->sendMe) { if ($v->validate()) { if ($captcha->verifyResponse() === true) { $message = " <html> <body> <p><b>From:</b></p> <p>{$name}</p> <p><b>Email:</b></p> <p>{$email}</p> <p><b>Message:</b></p> <p>{$message}</p> </body> </html> "; $mail = wireMail(); $mail->to($contactFormRecipient) ->from($email) ->subject('Contact form submission') ->bodyHTML($message) ->send(); if ($mail->send()) { $session->flashMessage = "<h2>Thank you for your message! I will get back to you shortly.</h2>"; $session->sent = true; $session->redirect($pages->get($contactPageID)->url); } else { $session->flashMessage = "<h2>Mail not sent. Error occured.</h2>"; } } else { $session->flashMessage = '<h2>Recaptcha must be complete.</h2>'; } } else { $session->flashMessage = '<h2>Please fill out the fields correctly.</h2>'; } } ?> <div class="body-row"> <div class="wrapper"> <?php if($session->flashMessage):?> <div class="alert <?php echo $session->sent ? 'alert-success' : 'alert-danger'?>" role="alert"> <?php echo $session->flashMessage;?> </div> <?php endif;?> <form id="contact-form" method="post"> <div class="<?php echo $v->errors('name') ? 'has-error' : ''?>"> <label for="name">Name (required)</label> <input class="form-control" name="name" id="name" type="text" value="<?php echo $sanitizer->text($input->post->name)?>"> </div> <div class="<?php echo $v->errors('email') ? 'has-error' : ''?>"> <label for="email">Email (required)</label> <input class="form-control" name="email" id="email" type="text" value="<?php echo $sanitizer->text($input->post->email)?>"> </div> <div class="<?php echo $v->errors('message') ? 'has-error' : ''?>"> <label for="message">Message (required)</label> <textarea class="form-control" name="message" id="message"><?php echo $sanitizer->text($input->post->message)?></textarea> </div> <div> <!-- Google Recaptcha code START --> <?php echo $captcha->render()?> <!-- Google Recaptcha code END --> </div> <div class="centered"> <button type="submit" name="sendMe" value="1">SEND MESSAGE</button> </div> </form> </div> </div> <?php $session->remove('flashMessage'); $session->sent = false; echo $captcha->getScript(); ?> Big thanks to @giannisok for the original ideas @danielsl maybe you could try this to see if you have more luck? It's been a great learning experience so far, so thanks all.
- 76 replies
-
- 1
-
Yeah, I was using: <?php if($session->flashMessage):?> <div class="alert <?=!$session->sent && (!$v->validate() || !$resp->isSuccess()) ? 'alert-danger' : 'alert-success'?>" role="alert"> <?php echo $session->flashMessage;?> </div> <?php endif;?> ...but of course, $resp is undefined (now I'm using the recaptcha module instead). Needs to be: <?php if($session->flashMessage):?> <div class="alert <?php echo $session->sent ? 'alert-success' : 'alert-danger'?>" role="alert"> <?php echo $session->flashMessage;?> </div> <?php endif;?> So, validation works, recaptcha works, but now the mail wont send - "Mail not sent. Error occured." Anyway, I'll look into it tomorrow, 21:30 here on Friday night, time to chill out for a bit
- 76 replies
-
I'm in the process of testing the MarkupGoogleRecaptcha module. When I submit my form, the middle section of my screen just goes blank, not quite what I had in mind.
- 76 replies
-
Maybe this? http://stackoverflow.com/questions/29056472/google-recaptcha-2-fatal-error-class-recaptcha-requestmethod-post-not-found I used composer to install my vendor packages so my \site\templates\vendor\ (on win 10) folder looks like this: I was hesitant at first and thought I could just download from github and put them in the right place. However, it was really quite easy to use composer and was certainly easier than manually managing this (especially with updating the packages). I then link to the required files in my contact.php like this: <?php namespace ProcessWire; // /site/templates/vendor/vlucas/valitron/src/Valitron/Validator.php wireIncludeFile("./vendor/vlucas/valitron/src/Valitron/Validator.php"); // /site/templates/vendor/google/recaptcha/src/ReCaptcha/ReCaptcha.php wireIncludeFile("./vendor/google/recaptcha/src/ReCaptcha/ReCaptcha.php"); $googleSiteKey = 'xxxxxx'; $googleSecretKey = 'xxxxxx'; //etc... I'm still using the version I posted earlier, the whole lot goes in a single contact.php template.
- 76 replies
-
Thanks @Robin S I did a little bit of re-arranging (and used implode). Works when I do this: // _search-form.php <?php namespace ProcessWire; ?> <?php // get all buildings $buildings = $pages->find("template=building-entry"); // get categories as pagearray of Ids sorted by title $buildingTypes = $pages->find("template=category-entry, sort=title"); // modify to standard array of Ids $buildingTypes = $buildingTypes->getArray(); //get unique towns into standard array $uniqueTowns = array_unique($buildings->explode("addressBuildingTown")); sort($uniqueTowns); // set up variables $minAreas = [ 500 => '500 sq ft', 1000 => '1,000 sq ft', 2000 => '2,000 sq ft', 3000 => '3,000 sq ft', 4000 => '4,000 sq ft', 5000 => '5,000 sq ft', 6000 => '6,000 sq ft', 7000 => '7,000 sq ft', 8000 => '8,000 sq ft', 9000 => '9,000 sq ft', 10000 => '10,000 sq ft', 50000 => '50,000 sq ft' ]; $maxAreas = [ 500 => '500 sq ft', 1000 => '1,000 sq ft', 2000 => '2,000 sq ft', 3000 => '3,000 sq ft', 4000 => '4,000 sq ft', 5000 => '5,000 sq ft', 6000 => '6,000 sq ft', 7000 => '7,000 sq ft', 8000 => '8,000 sq ft', 9000 => '9,000 sq ft', 10000 => '10,000 sq ft', 60000 => '60,000 sq ft', 100000 => '100,000 sq ft' ]; // Array to hold towns from GET $towns = []; if ($input->get->town) { $allowedTowns = array_values($uniqueTowns); $towns = $sanitizer->options($input->get->town, $allowedTowns); $input->whitelist("towns", $towns); } // sanitizing and adding to whitelist have to be here // or I get errors when trying to pre-populate the // form with previosuly submitted values // GET building type $allowedTypes = array_values($buildingTypes); $buildingType = $sanitizer->option($input->get->type, $allowedTypes); $input->whitelist("buildingType", $buildingType); // GET (sanitized) min area and add to whitelist $allowedMinAreas = array_keys($minAreas); $minArea = $sanitizer->option($input->get->minArea, $allowedMinAreas); $input->whitelist("minArea", $minArea); // GET max area $allowedMaxAreas = array_keys($maxAreas); $maxArea = $sanitizer->option($input->get->maxArea, $allowedMaxAreas); $input->whitelist("maxArea", $maxArea); ?> <form id='building_search' method='get' action='<?php echo $config->urls->root?>search/'> <h3>Building search</h3> <p> <label for='search_town'>Filter by town</label><br> <?php foreach ($uniqueTowns as $town) { $selected = ""; if ($input->get->town) { // in_array() expects parameter 2 to be array, null given $selected = $town == in_array($town, $input->whitelist("towns")) ? " checked" : ''; } echo "<input type='checkbox' name='town[]' value='{$town}'{$selected}>{$town}<br>"; } ?> </p> <p> <label for='search_type'>Building type</label> <select id='search_type' name='type'> <option value=''>Any</option> <?php foreach ($buildingTypes as $type) { $selected = $type == $input->whitelist("buildingType") ? "selected='selected'" : ''; echo "<option {$selected} value='{$type}'>{$type->title}</option>"; } ?> </select> </p> <p> <label for='search_min_area'>Min area</label> <select id='search_min_area' name='minArea'> <option value=''>No min</option> <?php foreach($minAreas as $value => $label) { $selected = $value == $input->whitelist("minArea") ? "selected='selected'" : ''; echo "<option {$selected} value='{$value}'>{$label}</option>"; } ?> </select> </p> <p> <label for='search_max_area'>Max area</label> <select id='search_max_area' name='maxArea'> <option value=''>No max</option> <?php foreach($maxAreas as $value => $label) { $selected = $value == $input->whitelist("maxArea") ? "selected='selected'" : ''; echo "<option {$selected} value='{$value}'>{$label}</option>"; } ?> </select> </p> <p><button type='submit' id='search_submit' name='submit' value='1'>Search</button></</p> </form> // search.php <?php namespace ProcessWire; ?> <?php // start building the selector // catchall selector for empty fields $theSelector = "template=building-entry"; // start building selectors $sorting = "sort=addressBuildingTown"; $townSelector = "addressBuildingTown"; $buildingTypeSelector = "category"; $areaSelector = "buildingArea"; // used to output 'filtered by' above results $summary = [ "towns" => "", "type" => "", "min" => "", "max" => "", ]; // if a town has been selected if ($towns) { $townSelectorPart = implode('|', $towns); $summary["towns"] = $townSelectorPart; $theSelector .= "," . $townSelector . "=" . $townSelectorPart; } // if a building type has been selected if ($buildingType) { $summary["type"] = $buildingType; $theSelector .= "," . $buildingTypeSelector . "=" . $buildingType; } // if min or max area has been selected if ($minArea || $maxArea) { // if min area selected if ($minArea) { $summary["min"] = $minArea; $minAreaSelectorPart = $areaSelector . ">={$minArea}"; $theSelector .= "," . $minAreaSelectorPart; } // if max area selected if ($maxArea) { $summary["max"] = $maxArea; $maxAreaSelectorPart = $areaSelector . "<={$maxArea}"; $theSelector .= "," . $maxAreaSelectorPart; } } ?> <?php $buildings = $pages->find($theSelector . "," . $sorting); foreach ($buildings as $building): ?> <h2><?php echo $building->title; ?></h2> <?php if ($building->addressBuildingName): ?> <p>BUILDING NAME: <?php echo $building->addressBuildingName; ?></p> <?php endif; ?> <p>ADDRESS: <?php echo $building->addressBuildingNumber . ' ' . $building->addressBuildingStreet . ', '; ?> <a href='#'><?php echo $building->addressBuildingTown; ?></a> </p> <p>TYPE: <a href="<?php echo $building->category->url; ?>"><?php echo $building->category->title; ?></a></p> <p>AREA: <?php echo $building->buildingArea; ?></p> <?php echo $building->buidingDescription; ?> <?php endforeach; ?> Makes more sense now ^ this is exactly what I want to do as well, nice one.
-
@Sephiroth I'll be reading this, thanks for sharing
-
I've got two files. _search-form.php renders a search form (funny that!) and search.php renders the results. I have a few pre-requisites: 1) The search form select box for towns must be made up of only towns which exist in a field ($addressBuildingTown), on a building page (building-entry), in the system. 2) The values in the selectors must remain after submission, so I need to use a whitelist to GET the current values. _search-form.php loads above search.php 1) _search-form.php sets up the values which are populated into the actual form (that the user sees), 2) It also gets the values from the URL, sanitizes them (compares with an allowed list), then adds to a whitelist 3) The whitelist is used for when search.php is loaded so the select lists/checkboxes show the last selected values The problems: 1) _search-form.php appears on the homepage. At this URL 'mysite.com' there are no values TO get in the first place which kicked up some errors about arrays being required by sanitizer, which leads onto 2... 2) I had to use 'if ($input->get->town)' so that part of code doesn't run when on a page which includes the search form, but has yet to be submitted. Is this not dodgy? The value used in this check is not sanitized. 3) search.php uses a variable called '$theSelector' which is constructed in _search.php. The whitelist works though, even the checkboxes which I was pleased with, took a while to work out how to get an array of checkbox values into the whitelist and check a value against them. So, my form 'works' but HAS to be above search.php or I get a bunch of errors which isn't very modular. What if I wanted to shift it to the bottom? if I did that, how would the '$theSelector' variable ever make it to search.php? Would just be undefined. Anyway, here's the code so far: // search-form.php <?php namespace ProcessWire; ?> <?php // get all buildings $buildings = $pages->find("template=building-entry"); // get categories as pagearray of Ids sorted by title $buildingTypes = $pages->find("template=category-entry, sort=title"); // modify to standard array of Ids $buildingTypes = $buildingTypes->getArray(); //get unique towns into standard array $uniqueTowns = array_unique($buildings->explode("addressBuildingTown")); sort($uniqueTowns); // set up variables $minAreas = [ 500 => '500 sq ft', 1000 => '1,000 sq ft', 2000 => '2,000 sq ft', 3000 => '3,000 sq ft', 4000 => '4,000 sq ft', 5000 => '5,000 sq ft', 6000 => '6,000 sq ft', 7000 => '7,000 sq ft', 8000 => '8,000 sq ft', 9000 => '9,000 sq ft', 10000 => '10,000 sq ft', 50000 => '50,000 sq ft' ]; $maxAreas = [ 500 => '500 sq ft', 1000 => '1,000 sq ft', 2000 => '2,000 sq ft', 3000 => '3,000 sq ft', 4000 => '4,000 sq ft', 5000 => '5,000 sq ft', 6000 => '6,000 sq ft', 7000 => '7,000 sq ft', 8000 => '8,000 sq ft', 9000 => '9,000 sq ft', 10000 => '10,000 sq ft', 60000 => '60,000 sq ft', 100000 => '100,000 sq ft' ]; ?> <?php // catchall selector for empty fields $theSelector = "template=building-entry"; // start building selectors $sorting = "sort=addressBuildingTown"; $townSelector = "addressBuildingTown"; $buildingTypeSelector = "category"; $areaSelector = "buildingArea"; // used to output 'filtered by' above results // unused at the moment $summary = [ "towns" => "", "type" => "", "min" => "", "max" => "", ]; // Array to hold towns from GET $towns = []; // is this right? if ($input->get->town) { $allowedTowns = array_values($uniqueTowns); $towns = $sanitizer->options($input->get->town, $allowedTowns); $input->whitelist("towns", $towns); } // GET building type $allowedTypes = array_values($buildingTypes); $buildingType = $sanitizer->option($input->get->type, $allowedTypes); $input->whitelist("buildingType", $buildingType); // GET (sanitized) min area and add to whitelist $allowedMinAreas = array_keys($minAreas); $minArea = $sanitizer->option($input->get->minArea, $allowedMinAreas); $input->whitelist("minArea", $minArea); // GET max area $allowedMaxAreas = array_keys($maxAreas); $maxArea = $sanitizer->option($input->get->maxArea, $allowedMaxAreas); $input->whitelist("maxArea", $maxArea); // if a town has been selected if ($towns) { $townSelectorPart = ""; foreach ($towns as $town) { $townSelectorPart .= $town . "|"; } $townSelectorPart = rtrim($townSelectorPart, "|"); $summary["towns"] = $townSelectorPart; $theSelector .= "," . $townSelector . "=" . $townSelectorPart; } // if a building type has been selected if ($buildingType) { $summary["type"] = $buildingType; $theSelector .= "," . $buildingTypeSelector . "=" . $buildingType; } // if min or max area has been selected if ($minArea || $maxArea) { // if min area selected if ($minArea) { $summary["min"] = $minArea; $minAreaSelectorPart = $areaSelector . ">={$minArea}"; $theSelector .= "," . $minAreaSelectorPart; } // if max area selected if ($maxArea) { $summary["max"] = $maxArea; $maxAreaSelectorPart = $areaSelector . "<={$maxArea}"; $theSelector .= "," . $maxAreaSelectorPart; } } ?> // render the actual form <form id='building_search' method='get' action='<?php echo $config->urls->root?>search/'> <h3>Building search</h3> <p> <label for='search_town'>Filter by town</label><br> <?php foreach ($uniqueTowns as $town) { $selected = ""; if ($input->get->town) { $selected = $town == in_array($town, $input->whitelist("towns")) ? " checked" : ''; } echo "<input type='checkbox' name='town[]' value='{$town}'{$selected}>{$town}<br>"; } ?> </p> <p> <label for='search_type'>Building type</label> <select id='search_type' name='type'> <option value=''>Any</option> <?php foreach ($buildingTypes as $type) { $selected = $type == $input->whitelist("buildingType") ? "selected='selected'" : ''; echo "<option {$selected} value='{$type}'>{$type->title}</option>"; } ?> </select> </p> <p> <label for='search_min_area'>Min area</label> <select id='search_min_area' name='minArea'> <option value=''>No min</option> <?php foreach($minAreas as $value => $label) { $selected = $value == $input->whitelist("minArea") ? "selected='selected'" : ''; echo "<option {$selected} value='{$value}'>{$label}</option>"; } ?> </select> </p> <p> <label for='search_max_area'>Max area</label> <select id='search_max_area' name='maxArea'> <option value=''>No max</option> <?php foreach($maxAreas as $value => $label) { $selected = $value == $input->whitelist("maxArea") ? "selected='selected'" : ''; echo "<option {$selected} value='{$value}'>{$label}</option>"; } ?> </select> </p> <p><button type='submit' id='search_submit' name='submit' value='1'>Search</button></</p> </form> // search.php <?php namespace ProcessWire; ?> <?php $buildings = $pages->find($theSelector . "," . $sorting); foreach ($buildings as $building): ?> <h2><?php echo $building->title; ?></h2> <?php if ($building->addressBuildingName): ?> <p>BUILDING NAME: <?php echo $building->addressBuildingName; ?></p> <?php endif; ?> <p>ADDRESS: <?php echo $building->addressBuildingNumber . ' ' . $building->addressBuildingStreet . ', '; ?> <a href='#'><?php echo $building->addressBuildingTown; ?></a> </p> // URL segments are next on the todo list for the following <p>TYPE: <a href="<?php echo $building->category->url; ?>"><?php echo $building->category->title; ?></a></p> <p>AREA: <?php echo $building->buildingArea; ?></p> <?php echo $building->buidingDescription; ?> <?php endforeach; ?> Hope I'm making sense here. Thanks for any advice.
-
Getting only unique values from every page in a PageArray
SamC replied to SamC's topic in General Support
Yep, it does, thanks! I'm still working on my buildings search site. Going ok but I'm in a situation now where I think my code needs to be re-arranged. I guess this is down to inexperience but I'll post it in a new thread, could do with some input from more experienced coders. -
Getting only unique values from every page in a PageArray
SamC replied to SamC's topic in General Support
I tried this instead of my code and got an error: Notice: Only variables should be passed by reference Not sure why though. --EDIT-- This seems to work though: $uniqueTowns = array_unique($buildings->explode('addressBuildingTown')); sort($uniqueTowns); -
@adrian loud and clear, thanks.