Jump to content

Render Selectors - Filter Search


nfil
 Share

Recommended Posts

hi there! 

I started with the basic PW template and now I have this site tree, it has the categories (apartments, houses etc...) then I have pages that will work as option for the articles, these options are presented in the article as a select input field type. For example objective field name is called option_objective in my article template.

root

Properties (articles-list-all)            

  - Apartments (articles-parent)                       

    -- apartment one (article) 

    -- apartment two

  - Houses                     

    -- house one

    -- house two

Objective (options-template)

  - Sell

  - Rent

Type of deal (options-template)

  - Commercial property

  - Private property

Other options (options-template)

  - other option one

-----------------------------------

Page name (template name)

To filter and render the results similar to skyscraper's site profile I looked at how selectors work and also at the skycrapper and Macrura's from the speaker's shop example. I can generate the options to select input fields in the frontend but I can't generate the results page. 

The following is a part of what I have at the moment.

I have this part to generate one of the article's option in the search_form.php



<option value=''>Any</option>
<?php
foreach($pages->get("/objective/")->children("id!=1594") as $objective) {
$selected = $objective->id == $input->whitelist->objective ? " selected='selected' " : '';
echo "<option$selected value='{$objective->id}'>{$objective->title}</option>";
}


Part of the search_results.php 



$matches = $pages->find("template=article, title|body|objective~=$q, limit=20");
$count = count($matches);

echo $out;


Thanks in advance for the help.

Link to comment
Share on other sites

I tried with the search.php from the basic profile site, also looked at the skyscraper's profile but now I'am a bit confused on how to generate results.


if($q = $sanitizer->selectorValue($input->get->q)) {

	// Send our sanitized query 'q' variable to the whitelist where it will be
	// picked up and echoed in the search box by the head.inc file.
	$input->whitelist('q', $q); 

	// Search the title, body and sidebar fields for our query text.
	// Limit the results to 50 pages. 
	// Exclude results that use the 'admin' template. 
	$matches = $pages->find("template=article, title|body|objective~=$q, limit=20");

	$count = count($matches); 

	if($count) {
		$out .= "<h2>$count page(s) found:</h2>";
		foreach($matches as $m)
		 {
		 	$firstimage = $m->images->first();
   		 	$thumb = $firstimage->size(120, 120);
			$out .= "<li><p><a href='{$m->url}'><img src='{$thumb->url}' alt='{$m->title}'>
			{$m->title}</a><br />{$m->summary}</p></li>";
		}

		$out .= "</ul>";

	} else {
		$out .= "<h2>No results.</h2>";
	}
} else {
	$out .= "<h2>Please insert a term</h2>";
}
Link to comment
Share on other sites

So where is it failing? 

Is your filter form really sending the search parameter "q" via the url?

We need to know the name of the select field that is sending the objective filter and how that is being passed to search_form.php

It might potentially be as simple as using : 

$input->get->objective

but it's impossible to tell without seeing more of your code.

Is your form being submitted (post) to search_form.php, or are you redirecting to a url like: mysite.com/?objective=xxxx (get)

  • Like 1
Link to comment
Share on other sites

Sending more code probably will add more confusion cause after testing it get the search results for the input text field and not for the selectors.

The name of the select field is option_objective

This is my article template: 

https://www.dropbox.com/s/xzee9294eid10sb/Screen%20Shot%202014-07-23%20at%2000.40.25.jpg

These are my option's pages although I in the previous snippet I sent the option page was named objective no its named option_objective like the select field:

 
This is the site's front end:
Link to comment
Share on other sites

Thanks for your patience Adrian! Don't know if it will add more confusion but here is my form, this is testing purposes so its a mess at the moment:

<aside id='edit-search'>
<?php
echo "<form ole='form' id='form-sidebar' class='form-search'" . "action='{$config->urls->root}search/' method='get'>";
?>

<header><h4>Pesquisa de imóveis</h4></header>

<?php
echo "<div class='form-group'>" . "<input type='text' name='q' id='search_query' value='" . htmlentities($input->whitelist('q'), ENT_QUOTES, 'UTF-8') . "'" . "</div>";
?> 

   <?php echo "<label>Objectivo</label>";
        echo "<div class='form-group'><select name='type'><option value=''>Indiferente</option>";
        foreach($pages->get("/option_objective")->children("id!=1594") as $objective) {
        $selected = $objective->id == $input->whitelist->objective ? " selected='selected' " : '';
        echo "<option$selected value='{$objective->id}'>{$objective->title}</option>";
    }
        echo "</select></div>";  
        echo "<label>Tipo de imóvel</label>";
        echo "<div class='form-group'><select name='type'><option value=''>Indiferente</option>";
        foreach($pages->find("template=article-parent") as $type_of_article) {
        $selected = $type_of_article->id == $input->whitelist->type_of_article ? " selected='selected' " : '';
        echo "<option$selected value='{$type_of_article->id}'>{$type_of_article->title}</option>"; 
    }
    echo "</select></div>";
    echo "<label>Estado do imóvel</label>";  
        echo "<div class='form-group'><select name='type'><option value=''>Indiferente</option>";
        foreach($pages->get("/option_new_old")->children("id!=1614") as $new_old) {
        $selected = $new_old->id == $input->whitelist->new_old ? " selected='selected' " : '';
        echo "<option$selected value='{$new_old->id}'>{$new_old->title}</option>";
    }
    echo "</select></div>";

    echo "<div class='form-group'>" . "<button type='submit' class='btn btn-default'>Pesquisar</button>" . "</div>" ;
  ?>
</form></aside>

The search-results.php

<?php

/**
 * Search template
 *
 */
$out = '';

if($q = $sanitizer->selectorValue($input->get->q)) {

	// Send our sanitized query 'q' variable to the whitelist where it will be
	// picked up and echoed in the search box by the head.inc file.
	$input->whitelist('q', $q); 

	// Search the title, body and sidebar fields for our query text.
	// Limit the results to 50 pages. 
	// Exclude results that use the 'admin' template. 
	$matches = $pages->find("template=article, title|body~=$q, limit=20");

	$count = count($matches); 

	if($count) {
		$out .= "<h2>$count página(s) encontrada(s):</h2>";
		foreach($matches as $m)
		 {
		 	$firstimage = $m->images->first();
   		 	$thumb = $firstimage->size(250, 0);
			$out .= "<li><p><a href='{$m->url}'><img src='{$thumb->url}' alt='{$m->title}'>
			{$m->title}</a><br />{$m->summary}</p></li>";
		}

		$out .= "</ul>";

	} else {
		$out .= "<h2>Não foram encontrados resultados.</h2>";
	}
} else {
	$out .= "<h2>Insira um termo/palavra para pesquisar</h2>";
}

// Note that we stored our output in $out before printing it because we wanted to execute
// the search before including the header template. This is because the header template 
// displays the current search query in the search box (via the $input->whitelist) and 
// we wanted to make sure we had that setup before including the header template. 

include("./head.inc");

echo "<div class='content-section' id='whitebg'>" . 
  "<div class='container'>" .
    "<div class='row'>" . "<div class='col-md-12 page-content'>";
echo $out; 
echo  "</div>" . "</div>" . "</div>";

include("./foot.inc");

Link to comment
Share on other sites

Thanks for all the details - good to see that your form is using "get". Sorry for maybe getting distracted by that - my fault.

In your form I see three select fields, all named "type" - should they be different filters?

Anyway, the key thing seems to be simply that you don't deal with the type field in search-results.php. You need to make use of $input->get->type and add it to the selector that finds the matches. Does that make sense?

  • Like 1
Link to comment
Share on other sites

yes adrian the form was pretty messed up   :undecided:  

<aside id='edit-search'>
<?php
echo "<form ole='form' id='form-sidebar' class='form-search'" . "action='{$config->urls->root}search/' method='get'>";
?>

<header><h4>Pesquisa de imóveis</h4></header>

<?php
echo "<div class='form-group'>" . "<input type='text' name='q' id='search_query' value='" . htmlentities($input->whitelist('q'), ENT_QUOTES, 'UTF-8') . "'" . "</div>";
?> 

    <p>
    <label for='search_option_objective'>Objectivo</label>
    <select id='search_option_objective' name='objective'>
        <option value=''>Any</option><?php
        foreach($pages->get("/option_objective/")->children() as $objective) {
            $selected = $objective->name == $input->whitelist->objective ? " selected='selected' " : ''; 
            echo "<option$selected value='{$objective->name}'>{$objective->title}</option>"; 
        }
        ?>

    </select>
    </p>

    <p>
    <label for='search_type_of_deal'>Tipo de negócio</label>
    <select id='search_type_of_deal' name='type_of_deal'>
        <option value=''>Any</option><?php
        foreach($pages->get("/option_type_of_deal/")->children() as $type_of_deal) {
            $selected = $type_of_deal->name == $input->whitelist->type_of_deal ? " selected='selected' " : ''; 
            echo "<option$selected value='{$type_of_deal->name}'>{$type_of_deal->title}</option>"; 
        }
        ?>

    </select>
    </p>

       <p>
    <label for='search_option_new_old'>Estado</label>
    <select id='search_option_new_old' name='new_old'>
        <option value=''>Any</option><?php
        foreach($pages->get("/option_new_old/")->children() as $new_old) {
            $selected = $new_old->name == $input->whitelist->new_old ? " selected='selected' " : ''; 
            echo "<option$selected value='{$new_old->name}'>{$new_old->title}</option>"; 
        }
        ?>

    </select>
    </p>

    </form></aside>

Form populated in the fronted

https://www.dropbox.com/s/4ffadiemn8nhg6o/Screen%20Shot%202014-07-23%20at%2001.31.14.jpg

Now I need to generate the search results page filtering the fields(options) in the article template in my tree:

Properties (articles-list-all)            
  - Apartments (articles-parent)                       
    -- apartment one (article) 
    -- apartment two
  - Houses                     
    -- house one
    -- house two
 
option_objective (options-template)
  - Sell
  - Rent
 
The problem is the search results the options won't filter the article's pages.
Link to comment
Share on other sites

Ok, so firstly, your search-form.php needs to access:

$input->get->objective
$input->get->status
$input->get->new_old

Then you need to build up the final selector to include these along with "q"

I have to head out now, so don't have time for a full example, but if you look at the sksyscraper search you'll see how it builds up the selector by concatenating the pieces together like: 

$selector .=

Not sure on your php knowledge, so in case you don't know the dot before the equal sign means to add to the last value of the variable.

To troubleshoot, try testing a final example selector and once you have that working and returning the results you need, work backwards to figure out how to build if from the component search elements.

I'll check in later and see how you are going.

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