Jump to content

Filter results of selector using select/checkbox?


antknight
 Share

Recommended Posts

Andrian the same Error: Call to a member function getInputfields() on a non-object (line

63 of C:\wamp\www\real-estate\site\templates\properties-listing.php)

this is my templates structure

First Parent is properties-type-listing.php template 

Second Parent is properties-categorie-listing.php template template that code is }

Child of properties-categorie-listing.php template is property-detail.php template that the fields i want to search

can you post your working code to try it

Soma yes i have images field in template property-detail.php

Link to comment
Share on other sites

Right - I see what you mean now - "the value which is selected". This is why I have this line:

$f->value = '';

This clears the selected values when displaying the filter form. I agree this doesn't seem efficient, but it works and no other way seems to.


 

Link to comment
Share on other sites

Try editing this line:

$myfields = array('id','title','body');

to include your images field and any others that are not page fields that should be included as filter select options

EDIT: Definitely - I have it working on several sections of a site I am working on :)

Link to comment
Share on other sites

Right - I see what you mean now - "the value which is selected". This is why I have this line:

$f->value = '';

This clears the selected values when displaying the filter form. I agree this doesn't seem efficient, but it works and no other way seems to.

Well this works:

$form = $modules->get("InputfieldForm");
foreach($page->child()->template->fields as $field) {
    $form->append($field->getInputfield($page->child()));
}
echo $form->render();

But would also have to clean values. :D

Link to comment
Share on other sites

Soma you mean to replace all other code from Adrian or to replace some part with this

$form = $modules->get("InputfieldForm");
foreach($page->child()->template->fields as $field) {
    $form->append($field->getInputfield($page->child()));
}
echo $form->render();
Link to comment
Share on other sites

Try this complete code. It is actually as I have it working except I removed the text search box in case you don't have a body field, or need that functionality. Of course you'll need to add an echo at the end. I use this as an .inc so it is not in this code block for me.

<?php

$filter_formcode = '';

if($input->post->filter_save) {
    // Search selector builder
    $search_string = '';
    $current_field = '';

    //print_r($input->post);

    foreach($input->post as $field_name => $field_result){

        if (is_array($field_result)){
            foreach($field_result as $f_value){

                if($current_field == $pages->get($f_value)->template){
                    $search_string .= '|' . (int)$f_value;
                }
                else{
                    $search_string .= ',' . $pages->get($f_value)->template . '=' . (int)$f_value;
                }
                $current_field = $pages->get($f_value)->template;

            }
        }
        /*else{ //This is only needed if you want the text search field which is an optional add on below

            if($field_name == 'body' && $field_result != ''){
                $search_string .= $field_name . '%=' . $field_result;
            }
        }*/
    }

    $search_string = trim($search_string,',');
}
else{
    $search_string = '';
}

error_log($search_string);

$message = '';

// Populate with the names of the fields you want to exclude OR include (see instructions below)
// Leave empty to output all the fields
$myfields = array('id','title','body','images');

$form = $modules->get('InputfieldForm');

$fields = $page->child()->getInputfields();

/*
//This adds a text search field if you want it
$field = $this->modules->get('InputfieldText');
$field->attr('id', 'Inputfield_body');
$field->attr('name', 'body');
$field->label = "Search term";
$form->append($field);
*/

// If array is not empty use it to filter the fields
if ($myfields){
    foreach($fields as $f){
        // Output all the fields minus the ones listed in the $myfields array
        // Instead, to output only the fields that are in the array, remove the (!) from the condition
        if (!in_array($f->name, $myfields)){
            $f->value = '';
            $form->append($f);
        }

    }
}
else{ // Else, include all the fields
    $form->append($fields);
}

// Add save button
$field = $this->modules->get('InputfieldSubmit');
$field->attr('id+name', 'filter_save');
$field->attr('value', 'Find');
$field->label = "find";
$form->append($field);


// Process the form
if($input->post->filter_save) {
    $form->processInput($input->post);
}

$display = 'block';

if($form->getErrors()) {
    $display = 'block';
}

$filter_formcode .= '

<div id="collapsed_form" style="display:'.$display.'">';

// render out form
$filter_formcode .= $form->render();

$filter_formcode .= '</div>';

$filter_formcode = strip_selected_tags_by_id_or_class('InputfieldPageAdd', $filter_formcode); //Strip out the option to add new item to page field even though it only appears to certain admin users

$filter_formcode .= '<script>
 $(document).ready(function() {
    $(\'select\').css({"max-width":"270px"});
    $(\'li\').removeClass("InputfieldColumnWidth");
    $(\'li\').removeClass("InputfieldColumnWidthFirst");
    $(\'li\').attr("style", "");
 });
</script>'; //set select to max width and remove floating selects so each one on own line
Link to comment
Share on other sites

Andrian i paste the code you post but: Error:     Call to undefined function strip_selected_tags_by_id_or_class()

(line 131 of C:\wamp\www\real-estate\site\templates\properties-listing.php)

Sorry for the confusion. Do what adrian said here, exclude the image and file fieds.
 

Soma not wory thank you very much and i am sure all the niewbe to read this will thank you too!!

Link to comment
Share on other sites

You can comment out that line if you want, or add this function somewhere:

function strip_selected_tags_by_id_or_class($class, $text)
{
   $regex = '#<(\w+)\s[^>]*(class|id)\s*=\s*[\'"](' . $class .
            ')[\'"][^>]*>.*</\\1>#isU';
   return(preg_replace($regex, '', $text));
}

I added that because my page fields have the ability to add a new item and I didn't want this visible on the front-end, even though it will only be visible to logged in users with rights to add, it is still not appropriate for a front-end filtering form. Chances are it won't be relevant in your situation anyway, so I would just delete the line.

Link to comment
Share on other sites

Sorry, did you see my note above about needing to echo it out?

I actually have all that code in a filter_form.inc file. Then wherever I want to use it, I just put:

include("./filter_form.inc");

echo $filter_formcode;
 

Does that sort it out ?

Link to comment
Share on other sites

Well no, not really. It looks like you need to add the name of the images field you are using to the $myfields array. You should also add any other fields that are not page fields. The skyscrapers site has a series of dropdowns that are page fields. Is this what you are looking for?

I have attached what I see with my code. To get the search term field you need to uncomment those two sections in my last version above and make sure that you change:

    $field->attr('id', 'Inputfield_body');
    $field->attr('name', 'body');

to match the name of the field you want to search for those keywords. If you don't want the keword search, you can leave as is.

It looks to me like the fields that are showing up in your screenshot are not Page fields.

EDIT: Alternatively if this is all seeming too hard, then you could download the files from the skyscraper example and use that code. The reason I like my option is that it is reusable throughout different sections on a site and on different sites - no need to define the filter search form for each usage scenario.

post-985-0-92769500-1369947941_thumb.jpg

Link to comment
Share on other sites

i am sory i wasnt clear, the search terms is not pagefields  is simple fields and is as follows

title

Property_Type
Property_Title
Property_Info
Property_Description
Land_Area
Living_Space
Rooms
Bedrooms
WC_Baths
Price
Images
 
how you believe i should do it?
Link to comment
Share on other sites

Well it sounds to me like you probably only want to be able to filter against things like land_area, living_space, number of rooms, number of bedrooms, number of baths, and price. I think you'll have to do something custom for this. I am still not sure if you have seen the search.php file from the skyscrapers demo, so I have attached it.

You should be able to modify that for your needs. In particular, look at the section starting with:

foreach(array('height', 'floors', 'year') as $key) {
 

Hope that gives you a starting point - sorry you went down the road of trying out my code - good for what it does, but won't work out of the box for your case.

search.php

Link to comment
Share on other sites

Adrian yes this is exactly what i want

filter against things like land_area, living_space, number of rooms, number of bedrooms, number of baths, and price. 

i have seen the search.php, i will try to make this work for me, thank you so much for all your help!! you are amazing!

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