• I’m working on a search function to find the difference between results, e.g. trying to find the result of best fit based on the input from a user form.

    I will try to summarise my intentions for this:

    User submits form of data and data is posted
    The search-function.php script is fired
    The search-function gets all of the relevant post_ids for the query
    The search-function then works out the difference between the user input and the post’s values
    The search-function will then return results order by the amount of difference they are from the user input.
    Posts are a custom post type called product. I’m using advanced custom fields to create the custom fields (which is what the script it querying to work out the difference).

    How would I correctly approach this script please?

    Do you require any more info?

    search-function.php

    //get an array of post_ids for live products/drivers e.g.
    
    $post_array = SELECT * post_id WHERE post_type = product AND post_status = publish FROM wp_posts
    
    //select all of the custom fields that much the post_id from the array, then work out the distance from the user input, then return them according to distance
    
    SELECT * WHERE post_id = $post_array, ABS($_POST[standard]-head_standard)+ABS($_POST[desired_shape]-shape)+ABS(($_POST[desired_height]*2)-(tracjectory_head_design+loft) as distance
    FROM post_meta
    ORDER BY distance ASC

    form.php

    <form action="search-function.php" class="" method="post" novalidate="">
    
            <div id="" class="content-area">
            <div id="content" class="site-content" role="main">
             <div id="" class="">
                   <label for="standard">
                        What is your playing standard?
                   </label>
                   <select name="standard" required="required">
                        <option value="1">
                             Novice
                        </option>
                        <option value="2">
                             Standard
                        </option>
                        <option value="3">
                             Expert
                        </option>
    
                   </select>
              </div>
    
              <div id="" class="">
                   <label for="carry">
                        What is your carry distance?
                   </label>
                   <select name="carry" required="required">
                        <option value="1">
                             Up to 215
                        </option>
                        <option value="2">
                             230 - 270
                        </option>
                        <option value="3">
                             270+
                        </option>
                   </select>
              </div>
    
              <div id="" class="">
                   <label for="shape">
                        What is your natural shot shape
                   </label>
                   <select name="shape" required="required">
                        <option value="1">
                             Hook
                        </option>
                        <option value="2">
                             Draw
                        </option>
                        <option value="3">
                             Straight
                        </option>
                        <option value="4">
                             Fade
                        </option>
                        <option value="5">
                             Slice
                        </option>
                   </select>
              </div>
    
              <div id="" class="">
                   <label for="desired_shape">
                        What is your desired shot shape
                   </label>
                   <select name="desired_shape" required="required">
                        <option value="1">
                             Draw
                        </option>
                        <option value="2">
                             Straight
                        </option>
                        <option value="3">
                             Fade
                        </option>
                   </select>
              </div>
    
              <div id="" class="">
                   <label for="height">
                        What is your current ball height?
                   </label>
                   <select name="height" id="field5" required="required">
                        <option value="1">
                             Low
                        </option>
                        <option value="2">
                             Mid
                        </option>
                        <option value="3">
                             High
                        </option>
                   </select>
              </div>
    
              <div id="" class="">
                   <label for="desired_height">
                        What is your desired ball height?
                   </label>
                   <select name="desired_height" id="field6" required="required">
                        <option value="1">
                             Low
                        </option>
                        <option value="2">
                             Mid
                        </option>
                        <option value="3">
                             High
                        </option>
                   </select>
              </div>
    
              <div id="" class="">
                   <input type="submit" value="Submit">
              </div>
         </form>
Viewing 1 replies (of 1 total)
  • Thread Starter philhudson91

    (@philhudson91)

    Updated code and approach ********

    ‘m working on a search function to find the difference between results, e.g. trying to find the result of best fit based on the input from a user form.

    I will try to summarise my intentions for this:

    User submits form of data and data is posted
    The search-function gets all of the relevant post_ids for the query
    The search-function then works out the difference between the user input and the post’s values
    The search-function will then return results order by the amount of difference they are from the user input.
    Posts are a custom post type called product. I’m using advanced custom fields to create the custom fields (which is what the script it querying to work out the difference).

    How would I correctly approach this script please?

    Do you require any more info?

    search function

    ‘$the_query = get_posts( ‘cat=2&post_type=product&fields=ids’ );
    if($the_query){
    foreach($the_query as $id) {
    $myrows = $wpdb->get_results(“SELECT $id, ABS($_POST[‘standard’]-head_standard)+ABS($_POST[‘desired_shape’]-shape)+ABS(($_POST[‘desired_height’]*2)-(tracjectory_head_design+loft) as distance FROM post_meta ORDER BY distance ASC”);
    echo $id;
    }
    }’

Viewing 1 replies (of 1 total)
  • The topic ‘WordPress search function to query database and work out distance between result’ is closed to new replies.