• Hi all!

    I’m using the theme Realtr from Templatic.
    I’m making an estate site, and I wanted to know if anyone could help me – here’s my problem:

    Problem:

    In the right sidebar, I have a pricerange option, called “Pris Niveau” – it works fine as it is, but it has to many options.

    My vision:

    Instead of one drop-down with 30 options, I would like to have 2 textfields, so that the customers can enter the minimum and maximum pricerange – I just can’t make it work! Can you guys help me? please!

    Code-snippet

    <label><?php echo SEARCH_PRICE_RANGE_TEXT; ?> : </label>
              <select name="srch_price" id="srch_price" onchange="" class="fl select">
                <option value=""><?php echo SELECT_ALL_PRICE_TEXT; ?> <?php _e("in","templatic");?> <?php echo get_currency_sym();?> </option>
                <?php echo get_price_range_dl($_REQUEST['srch_price']);?>
              </select>

    Please!
    I really nees your help…

Viewing 13 replies - 1 through 13 (of 13 total)
  • Thread Starter kuckovic

    (@kuckovic)

    And here’s a link to my website, of course:
    https://t2.designa-homepages.dk/

    Moderator bcworkz

    (@bcworkz)

    You can alter the form any way you like, but you need to coordinate such changes with the page that accepts the request and builds a search query based on the field values.

    Right now, the accepting page takes a value from ‘srch_price’ which is a price range string and translates that into search terms. If you change this to two fields, the accepting page will now need to know to get both values and translate those into search terms.

    Another approach might be to make ‘srch_price’ a hidden field, add in your high and low fields, then onchange, use javascript to build the selected price range value of the hidden ‘srch_price’. When the form is submitted, hopefully the accepting page will not know that the selection was built with javascript and is not a pre-defined range. Whether this works will depend on how the accepting page translates the selection into search terms.

    Thread Starter kuckovic

    (@kuckovic)

    Hi Bcworkz,

    Can you please tell me how to change those fields?
    And whats the “accepting page”?

    Sorry for the inconvenience

    Aris

    Moderator bcworkz

    (@bcworkz)

    No inconvenience, it’s what I do.

    “Accepting page” A term I made up because I didn’t want to repeatedly type “the page that accepts the request”. In other words, this is the PHP page specified in the form’s action attribute and where the form’s POST request is sent.

    How to change those fields. I’m not sure what you’re wanting to change, I suspect everything. How to do this depends on which approach you prefer. One is to alter the PHP on the accepting page, this is the most straight forward approach. The other is a javascript approach. The success of this depends on how the accepting page currently deals with the form data right now. It is not guaranteed to work.

    Either way though, you need to remove the select/option field from your form and add in two text input fields. If using the javascript idea, also add a hidden input field named ‘srch_price’. You should also try to locate the PHP that actually processes this part of the form. This will either need to be altered, or at least investigated to determine if the javascript idea is even feasible.

    If I can look at this code, I can help you better. If there’s more than a dozen lines, please post it at pastebin.com and provide a link here. It may be a few days before I can respond again, please be patient.

    Thread Starter kuckovic

    (@kuckovic)

    Hi bcworkz!

    Thanks for your amazing help – REALLY appreciated!
    Okay, I have some codes for you here:

    The code from search.php (acceptpage I think):

    if($_REQUEST['srch_price'])
                    {
                        $is_search = 1;
                        $srch_price = $_REQUEST['srch_price'];
                        if(strstr($srch_price,'-'))
                        {
                            $srch_price_str = str_replace('-',' and ',$srch_price);
                            $srch_price_arr = $wpdb->get_col("select post_id from $wpdb->postmeta where meta_key like 'price' and meta_value between $srch_price_str");
                        }
                        elseif(strstr($srch_price,'+'))
                        {
                            $srch_price_str = str_replace('+','',$srch_price);
                            $srch_price_arr = $wpdb->get_col("select post_id from $wpdb->postmeta where meta_key like 'price' and meta_value >= $srch_price_str");
                        }
                           $all_pids_arr = array_intersect($all_pids_arr,$srch_price_arr);
                    }

    And the form:

    <label><?php echo SEARCH_PRICE_RANGE_TEXT; ?> : </label>
              <select name="srch_price" id="srch_price" onchange="" class="fl select">
                <option value=""><?php echo SELECT_ALL_PRICE_TEXT; ?> <?php _e("in","templatic");?> <?php echo get_currency_sym();?> </option>
                <?php echo get_price_range_dl($_REQUEST['srch_price']);?>
              </select>

    I really appreciate your help!

    Thread Starter kuckovic

    (@kuckovic)

    And I uploaded it on pastebin:

    https://pastebin.com/V0ECBPbR

    If it’s easier for you

    Moderator bcworkz

    (@bcworkz)

    Thanks for posting the code. It’s pretty much what I expected, either the PHP or javascript scheme would work just fine. The PHP approach is more straight forward, but if you update this code (assuming someone is issuing updates), your changes will be lost. The javascript approach is really a roundabout way of doing the same thing, but your code is usually safe from updates.

    Either way, add two text input fields and get rid of the select/option field in the form. To alter the PHP, instead of getting a single form value, get the two values from your new fields, and let’s say you assigned the values to $low and $high. Then you do: $srch_price_str = "$low and $high";. You can then get rid of all the if/elseif stuff except for the first query. Keep the array_intersect line as well. That’s all it takes, you should be good to go.

    If you would rather go the javascript route, it’s just what I suggested earlier, build a range string from the two new form fields and assign it to a hidden field. The PHP will never know the difference.

    Thread Starter kuckovic

    (@kuckovic)

    Hi bcworkz,

    Okay, here’s how I’ve done it so far.
    I don’t know if it’s the right approach – but you tell me ??

    The form:

    <label>Min. husleje</label>
              <input name="minimumhusl" id="minimumhusl" onchange="" class="textfield" type="text" />
              <label>Maks. husleje</label>
              <input name="maksimumhusl" id="maksimumhusl" onchange="" class="textfield" type="text" />

    As you see, I’ve made 2 fields.
    “Minimumhusl” is equal to “low” and “Maksimumhusl” is equal to “high”.
    And here is how I’ve done the “acceptpage”

    https://pastebin.com/mib7LUPG

    I’m not sure if I’ve done it the right way.
    It wont work when I upload it ??

    Moderator bcworkz

    (@bcworkz)

    Sorry about the confusion, this should be closer to what you need:

    if($_REQUEST['minimumhusl'] && $_REQUEST['maksimumhusl'])
        {
            $is_search = 1;
            $srch_price_str = $_REQUEST['minimumhusl'] . 'and' . $_REQUEST['maksimumhusl'];
            $srch_price_arr = $wpdb->get_col("select post_id from $wpdb->postmeta where meta_key like 'price' and meta_value between $srch_price_str");
            $all_pids_arr = array_intersect($all_pids_arr, $srch_price_arr);
        }

    Right now I have no way to test this. If it doesn’t work, it’s likely just a dumb syntax mistake on my part, as I’m doing this from memory. Hopefully you can track down any syntax errors. If not, I’ll again have access to my test installation in a week. I promise I’ll fix it then if you’re still stuck. Or maybe someone else reading this will see where I went wrong.

    There’s also a chance what I changed here could affect code farther along that I’m unaware of. This may be more likely, as I’m pretty confident my code is correct, it’s just that I can’t be absolutely sure right now.

    Thread Starter kuckovic

    (@kuckovic)

    HI bcworkz

    Well, now I get a “Can’t find a matching criteria” page when I search, so we are getting closer ??

    Still, if I search for a pricerange between 8200 and 8400 – I should get at least one result, but nothing comes up.

    You know what it can be?

    Moderator bcworkz

    (@bcworkz)

    Well, despite my earlier confidence, I already see an error in my latest code. The 'and' needs to be padded with spaces: ' and '. That may be the only problem, sorry about that.

    If you still get no results, the next step is to var_dump() various interim values used and ensure they contain the expected values.

    It’s also necessary once this is working to sanitize the input values to ensure they are integer values within a certain range to prevent any kind of SQL injection attack.

    Thread Starter kuckovic

    (@kuckovic)

    Still nothing ??
    I don’t know what could be wrong in the code.. :/

    Moderator bcworkz

    (@bcworkz)

    It’s got to be something stupid, but I’m hampered by not having access to the resources I need to track it down. (Even if you wanted to give me access, I cannot accept) It’ll take some patience to resolve this via the forum format, but I’m sure we can figure this out.

    I’d like to see the values of $srch_price_str from both my code and the original code for the drop down form. Insert this line directly above the first occurrence of $srch_price_arr = $wpdb->get_col(//more code... in both versions:
    echo '<pre>; var_dump($srch_price_str); die('</pre>');
    Submit a search query using both code versions, using the same high and low values in each case. The results should be identical. In what way they are not will be a good clue to where things went wrong.

    If it’s easier, you can have both HTML form field versions on a single form, then you only need to upload it once for all tests (there may be more tests too). Then you only need to swap the old and new PHP versions back and forth to run the tests.

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘Search price range’ is closed to new replies.