Jump to content

Trying to expand search form


bwakad
 Share

Recommended Posts

Using the Skyscrapers, I wanted to see if i could add architect to the search form as a select field to search, like city is. So I just went along and copied the code from city.... and although it displays in the searchform, it does not show results... what do I miss?

In includes/search-form.php I added this code:

<!-- added architect -->
    <p>
    <label for='search_architect'>Architect</label>
    <select id='search_architect' name='architect'>
        <option value=''>Any</option><?php
        // generate the architect options, checking the whitelist to see if any are already selected
        foreach($pages->get("/architects/")->children() as $architect) {
            $selected = $architect->name == $input->whitelist->architect ? " selected='selected' " : '';
            echo "<option$selected value='{$architect->name}'>{$architect->title}</option>";
        }
        ?>
    </select>
    </p>

In search.php I added this code (before the code for city select):

$summary = array(
// added architect
    "architect" => "",    
    "city" => "",
    "hours" => "",
    "floors" => "",
    "year" => "",
    "keywords" => "",
    );

// added architect
// if a architect is specified, then we limit the results to having that architect as their parent
if($input->get->architect) {
    $architect = $pages->get("/architects/" . $sanitizer->pageName($input->get->architect));
    if($architect->id) {
        $selector .= "parent=$architect, ";
        $summary["architect"] = $architect->title;
        $input->whitelist('architect', $architect->name);
    }
}
Link to comment
Share on other sites

WillyC is right...that selector will not work. Architects (see architects pages, e.g.Albert Khan) are not parent pages; they have no children...so, your selector will not return anything...

Edited by kongondo
Link to comment
Share on other sites

But then how come it works for the cities?

The code is the same, except the name in the code is different, e.g.architect versus cities, architect versus city.

In the back-end both city-pages and architect-pages have a parent-page.

Or does it has something to do ith this code.

the first is from architect.php - second is from city.php

$content = renderSkyscraperList(findSkyscrapers("architects=$page"));
$content = renderSkyscraperList(findSkyscrapers("parent=$page"));
Link to comment
Share on other sites

Technically, the code is not the same; one if filtering by a field called "architects" the other by the parent. (i.e., the City)..

Hope the below explains this:

This is the function findSkyscrapers

function findSkyscrapers($selector) {

	$validSorts = getValidSkyscraperSorts();

	// check if there is a valid 'sort' var in the GET variables
	$sort = wire('sanitizer')->name(wire('input')->get->sort);

	// if no valid sort, then use 'title' as a default
	if(!$sort || !isset($validSorts[ltrim($sort, '-')])) $sort = 'title';

	// whitelist the sort value so that it is retained in pagination
	if($sort != 'title') wire('input')->whitelist('sort', $sort); 

	// expand on the provided selector to limit it to 10 sorted skyscrapers
	$selector = "template=skyscraper, limit=10, " . trim($selector, ", ");

	// check if there are any keyword searches in the selector by looking for the presence of 
	// ~= operator. if present, then omit the 'sort' param, since ProcessWire sorts by 
	// relevance when no sort specified.
	if(strpos($selector, "~=") === false) $selector .= ", sort=$sort";

	// now call upon ProcessWire to find the skyscrapers for us
	$skyscrapers = wire('pages')->find($selector); 

	// save skyscrapers for possible display in a map
	mapSkyscrapers($skyscrapers); 

	// set this runtime variable to the page so we can show the user what selector was used
	// to find the skyscrapers. the renderSkyscraperList function looks for it. 
	wire('page')->set('skyscraper_selector', $selector);

	return $skyscrapers; 
}

Expanding that...

For architect.php: We look in the field "architects"

//the code
findSkyscrapers("architects=$page")

//is actually saying...
$skyscrapers = wire('pages')->find($selector); 

//which can be expanded as...
$skyscrapers = wire('pages')->find("template=skyscraper, architects=$page, limit=10");//find pages whose field "architects" == this architect (e.g. John)

For city.php, we look for children pages of the page we are viewing, e.g. New York...i.e. the parent. It also says this in city.php

This just lists the current page's children, which are assumed to be skyscrapers

The code...

//the original code
findSkyscrapers("parent=$page")); 

//can be expanded as such...
$skyscrapers = wire('pages')->find("template=skyscraper, parent=$page, limit=10");//find pages whose parent is this page (e.g. New York)

Hope it now makes sense...  :)

Link to comment
Share on other sites

Here is a working version...give me two minutes... :-)

EDIT:

OK, here we go...

Edit your search_form.php - add the following code:

<p>
<label for='search_architect'>Architect</label>
  <select id='search_architect' name='architect'>
    <option value=''>Any</option><?php 
    // generate the Architect options, checking the whitelist to see if any are already selected
      foreach($pages->get("/architects/")->children() as $architect) {
	       $selected = $architect->name == $input->whitelist->architect ? " selected='selected' " : ''; 
	       echo "<option$selected value='{$architect->name}'>{$architect->title}</option>"; 
      }
	?>
  </select>
</p>
	

Edit your  search.php as you did above...but with the modification in the selector....and in the comments

// added architect
// if a architect is specified, then we limit the results to having that architect (architects field in those pages)
if($input->get->architect) {
    $architect = $pages->get("/architects/" . $sanitizer->pageName($input->get->architect));
    if($architect->id) {
        $selector .= "architects=$architect, ";//CHANGE YOUR SELECTOR AS SHOWN HERE
	$summary["architect"] = $architect->title;
	$input->whitelist('architect', $architect->name);
    }
}

Edit your CSS to style the new longer search form...

Smile...am not such a bad guy  :-X  :P  ;)

  • Like 2
Link to comment
Share on other sites

You guys are quick and amazing. Just one question for search-form.php, should this:

$selected = $architect->name == $input->whitelist->city ? " selected='selected' " : '';

not be whitelist->architect

???

Changed that. Your code works. Great help here!

Now I need to figure out a way to use the form as a filter - starting from the current page, I believe I already have seen it somewhere in the "Joss tutorial".

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